Java Foundation----Use of >zip compression

Source: Internet
Author: User
Tags uncompress

Java provides read and write data streams in a compressed format. They are encapsulated into out-of-the-box IO classes to provide compression capabilities. Let's start with the use of compressed files in Java.

Directory Navigation:

    1. A brief description of compression
    2. Use of gzip compressed files
    3. Use of Zip compressed files
    4. Comparison of gzip with zip compression
    5. Friendship Link

A brief description of compression

First, there are compressed classes in Java:

    • Checkedinputstream getchecksum () generates checksums for any inputstream (not just decompression)
    • Checkedoutputstream getchecksum () generates checksums for any outputstream (not just decompression)
    • Deflateroutputstream the underlying class for compressing classes
    • Zipoutputstream a deflateroutputstream to compress the data into a ZIP file format
    • Gzipoutputstream a deflateroutputstream to compress data into gzip file format
    • Inflaterinputstream the base class for extracting classes
    • Zipinputstream a deflaterinputstream, unzip the data saved in the ZIP file format
    • Gzipinputstream a deflaterinputstream, unzip the data saved in the gzip file format

Second, some description of the compression library:

    • Compressed class libraries are processed in bytes rather than characters, and they are part of the InputStream and OutputStream inheritance hierarchies

Third, the use of zip compression Note:

    • For each file that you want to add to the archive, you must call Putnextentry () and pass it to a ZipEntry object.
    • The ZipEntry object contains a fully functional interface that allows you to get and set all the data that is acceptable on that particular entry (portal) in the zip file: First name, length before compression and compression, date, CRC checksum, data for extra fields, comments, The compression method and whether it is a directory entry, and so on.
    • Although Checkedinputstream and Checkedoutputstream also provide support for Adler32 and CRC32 checksums, the ZipEntry only supports CRC interfaces
    • To extract the files, Zipinputstream provides a getnextentry () method to return to the next zipentry under some conditions
    • You can use the ZipFile object to read the file. The object has a entries () method that can return a enumeration (enumeration) for ZipEntry.

Use of gzip compressed files

We use a simple program use case to expand today's usage of the compression class, the program structure is as follows

First, we create a gzipcompress class, used for the Gzip compression class test: firstly, the compressed file Method compress ():

Compressed file private static void compress () throws Exception {    BufferedReader in = new BufferedReader (New FileReader ("Compr Ess/test.txt ")));    Bufferedoutputstream out = new Bufferedoutputstream (new Gzipoutputstream (New FileOutputStream ("uncompress/test.gz")) );    int C;    while ((c = In.read ())! =-1) {        out.write (c);    }    In.close ();    Out.close ();}

Second, we create the Gzip decompression method: Uncompress ()

Unzip the file private static void Uncompress () throws Exception {    BufferedReader in = new BufferedReader (            new Inputstr Eamreader (New Gzipinputstream (New FileInputStream ("uncompress/test.gz")));    String str;    while ((str = in.readline ()) = null) {        System.out.println (str);    }    In.close ();}

Third, in the main method to run, the following results are obtained:

    • Console printing: Hello world.
    • Generate test.gz files under the Uncompress folder

Use of Zip compressed files

One, we create a zipcompress class for the Gzip compression class test: First, the compressed file Method compress (): Compressed Huhx.png,test2.txt, Test3.txt, test4.txt file

Private final static string[] resources = new string[] {"Huhx.png", "Test2.txt", "Test3.txt", "Test4.txt"};//compressed file privat    e static void Compress () throws Exception {FileOutputStream f = new FileOutputStream ("Uncompress/test.zip");    Checkedoutputstream csum = new Checkedoutputstream (f, New Adler32 ());    Zipoutputstream Zos = new Zipoutputstream (csum);    Bufferedoutputstream out = new Bufferedoutputstream (ZOS);    Zos.setcomment ("A test of Java zipping");    ZipEntry entry = null;        for (String resource:resources) {System.out.println ("Writing file:" + Resource);        BufferedReader in = new BufferedReader (New FileReader ("compress/" + Resource));        Entry = new ZipEntry (Resource);        Entry.setcomment (Resource + "comments");        Zos.putnextentry (entry);        int C;        while ((c = In.read ())! =-1) {out.write (c);        } in.close ();    Out.flush ();    } out.close (); System.out.println ("Checksum:" + csum.getchecksum (). GetValue ());} 

Second, we create the Zip decompression method: Uncompress1 ()

Unzip the file private static void Uncompress1 () throws Exception {    FileInputStream fi = new FileInputStream ("Uncompress/tes T.zip ");    Checkedinputstream csum = new Checkedinputstream (FI, New Adler32 ());    Zipinputstream in2 = new Zipinputstream (csum);    Bufferedinputstream bis = new Bufferedinputstream (in2);    ZipEntry ze;    while ((Ze = in2.getnextentry ()) = null) {        System.out.println ("Reading file:" + ze.getname ());    }    System.out.println ("Checksum:" + csum.getchecksum (). GetValue ());    Bis.close ();}

Third, we create a zip decompression another method: Uncompress2 ()

Unzip the file private static void Uncompress2 () throws Exception {    ZipFile ZF = new ZipFile ("Uncompress/test.zip");    Enumeration<?> e = zf.entries ();    while (E.hasmoreelements ()) {        ZipEntry entry = (zipentry) e.nextelement ();        System.out.println ("File:" + entry.getcomment ());    }    Zf.close ();}

Iv. run the Compress () and Uncompress1 () methods to obtain the following results:

V. Run the Compress () and Uncompress2 () methods to obtain the following results:

Comparison of gzip with zip compression
    • GZIP: If only a single data stream needs to be compressed (rather than a series of different data), then it might be the most appropriate choice.
    • ZIP: It makes it easy to save multiple files. There is even a separate class to simplify the reading of the zip file

Friendship Link
    • Test Project source code download access password 56C3

Http://www.cnblogs.com/huhx/p/javaCompress.html

Java Foundation----Use of >zip compression (RPM)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.