[ Pobierz całość w formacie PDF ]
.GZIPOutputStreamA DeflaterOutputStream thatcompresses data into the GZIP file format.InflaterInputStreamBase class for decompressionclasses.ZipInputStreamAn InflaterInputStream thatdecompresses data that has been stored in the Zip file format.GZIPInputStreamAn InflaterInputStream thatdecompresses data that has been stored in the GZIP file format.Although there are many compressionalgorithms, Zip and GZIP are possibly the most commonly used.Thus you caneasily manipulate your compressed data with the many tools available for readingand writing these formats.Simple compression with GZIPThe GZIP interface is simple and thus isprobably more appropriate when you have a single stream of data that you want tocompress (rather than a container of dissimilar pieces of data).Here’s anexample that compresses a single file://: c11:GZIPcompress.java// Uses GZIP compression to compress a file// whose name is passed on the command line.import java.io.*;import java.util.zip.*;public class GZIPcompress {// Throw exceptions to console:public static void main(String[] args)throws IOException {BufferedReader in =new BufferedReader(new FileReader(args[0]));BufferedOutputStream out =new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz")));System.out.println("Writing file");int c;while((c = in.read()) != -1)out.write(c);in.close();out.close();System.out.println("Reading file");BufferedReader in2 =new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz"))));String s;while((s = in2.readLine()) != null)System.out.println(s);}} ///:~The use of the compression classes isstraightforward—you simply wrap your output stream in aGZIPOutputStream or ZipOutputStream and your input stream in aGZIPInputStream or ZipInputStream.All else is ordinary I/Oreading and writing.This is an example of mixing the char-orientedstreams with the byte-oriented streams: in uses the Readerclasses, whereas GZIPOutputStream’s constructor can accept only anOutputStream object, not a Writer object.When the file is opened,the GZIPInputStream is converted to a Reader.[ Add Comment ]Multifile storage with ZipThe library that supports the Zip formatis much more extensive.With it you can easily store multiple files, andthere’s even a separate class to make the process of reading a Zip fileeasy.The library uses the standard Zip format so that it works seamlessly withall the tools currently downloadable on the Internet.The following example hasthe same form as the previous example, but it handles as many command-linearguments as you want.In addition, it shows the use of theChecksum classes to calculate and verify thechecksum for the file.There are two Checksum types:Adler32 (which is faster) andCRC32 (which is slower but slightly moreaccurate).[ Add Comment ]//: c11:ZipCompress.java// Uses Zip compression to compress any// number of files given on the command line.import java.io.*;import java.util.*;import java.util.zip.*;public class ZipCompress {// Throw exceptions to console:public static void main(String[] args)throws IOException {FileOutputStream f =new FileOutputStream("test.zip");CheckedOutputStream csum =new CheckedOutputStream(f, new Adler32());ZipOutputStream out =new ZipOutputStream(new BufferedOutputStream(csum));out.setComment("A test of Java Zipping");// No corresponding getComment(), though.for(int i = 0; i < args.length; i++) {System.out.println("Writing file " + args[i]);BufferedReader in =new BufferedReader(new FileReader(args[i]));out.putNextEntry(new ZipEntry(args[i]));int c;while((c = in.read()) != -1)out.write(c);in.close();}out.close();// Checksum valid only after the file// has been closed!System.out.println("Checksum: " +csum.getChecksum()
[ Pobierz całość w formacie PDF ]