Stiskanje datoteke z uporabo DeflaterOutputStream
Ta razred izvaja filter izhodnega toka za stiskanje podatkov v formatu stiskanja 'deflate'. Uporablja se tudi kot osnova za druge vrste kompresijskih filtrov, kot je GZIPOutputStream. Pomembne metode:- Vnesite vhodno datoteko 'datoteka 1' v FileInputStream za branje podatkov.
- Vzemite izhodno datoteko 'file 2' in jo dodelite FileOutputStream. To bo pomagalo zapisati podatke v 'file2'.
- Dodelite FileOutputStream DeflaterOutputStream za stiskanje podatkov.
- Zdaj preberite podatke iz FileInputStream in jih zapišite v DeflaterOutputStream. Stisnil bo podatke in jih poslal v FileOutputStream, ki shrani stisnjene podatke v izhodno datoteko.
- Datoteka z imenom 'file2' zdaj vsebuje stisnjene podatke in iz te datoteke moramo pridobiti originalne dekompresirane podatke.
- Dodelite stisnjeno datoteko 'file2' FileInputStream. To pomaga pri branju podatkov iz 'file2'.
- FileOutputStream dodelite izhodno datoteko 'file3'. To bo pomagalo pri zapisovanju nestisnjenih podatkov v 'file3'.
- Zdaj preberite nestisnjene podatke iz InflaterInputStream in jih zapišite v FileOutputStream. To bo nestisnjene podatke zapisalo v 'datoteko3'.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.Deflater; import java.util.zip.DeflaterOutputStream; class zip { public static void main(String[] args) throws IOException { //Assign the original file : file to //FileInputStream for reading data FileInputStream fis=new FileInputStream('file1'); //Assign compressed file:file2 to FileOutputStream FileOutputStream fos=new FileOutputStream('file2'); //Assign FileOutputStream to DeflaterOutputStream DeflaterOutputStream dos=new DeflaterOutputStream(fos); //read data from FileInputStream and write it into DeflaterOutputStream int data; while ((data=fis.read())!=-1) { dos.write(data); } //close the file fis.close(); dos.close(); } }
Raztegovanje datoteke z uporabo InflaterInputStream
Ta razred implementira tokovni filter za razklapljanje podatkov v formatu stiskanja 'deflate'. Uporablja se tudi kot osnova za druge dekompresijske filtre, kot je GZIPInputStream. Pomembne metode:import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.zip.InflaterInputStream; //Uncompressing a file using an InflaterInputStream class unzip { public static void main(String[] args) throws IOException { //assign Input File : file2 to FileInputStream for reading data FileInputStream fis=new FileInputStream('file2'); //assign output file: file3 to FileOutputStream for reading the data FileOutputStream fos=new FileOutputStream('file3'); //assign inflaterInputStream to FileInputStream for uncompressing the data InflaterInputStream iis=new InflaterInputStream(fis); //read data from inflaterInputStream and write it into FileOutputStream int data; while((data=iis.read())!=-1) { fos.write(data); } //close the files fos.close(); iis.close(); } }
Ustvari kviz