Java ZIP file compression and decompression

Source: Internet
Author: User

Zips are a very simple compression form. In Java, zips are mainly used in the java.util.zip package. Mainly include zipfile, zipoutputstream, zipinputstream, and zipentry. Zipoutputstream is used to compress files. zipinputstream and zipfile are used to decompress files. zipentry is used during compression and decompression. In a Java zip compressed file, each subfile is a zipentry object. The following is modified on the basis of the http://www.open-open.com/bbs/view/1320568080343

Compression:

Import Java. io. bufferedinputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import java.util.zip. zipentry; import java.util.zip. zipoutputstream; public class zipoutputstreamtest {public static void main (string ARGs []) throws ioexception {// test1 (); Test2 ();} public static void test1 () throws ioexception {zip Outputstream ZOS = new zipoutputstream (New fileoutputstream ("d :\\ testzip.zip"); // compile a zipentry object named AB .txt zipentry = new zipentry ("abcc.txt "); // set ZOS annotations. setcomment ("Zip test for a single file"); // Add the generated zipentry object to the compressed file, the content written to the compressed file will be placed in ZOS in the zipentry object. putnextentry (entry); inputstream is = new fileinputstream ("D: \ AB .txt"); int Len = 0; while (LEN = is. read ())! =-1) ZOS. write (LEN); is. close (); ZOS. close ();} public static void Test2 () throws ioexception {file infile = new file ("D: \ test "); zipoutputstream ZOS = new zipoutputstream (New fileoutputstream ("D: \ test.zip"); ZOS. setcomment ("Multi-file processing"); zipfile (infile, ZOS, ""); ZOS. close ();} public static void zipfile (File infile, zipoutputstream ZOS, string DIR) throws ioexception {If (infile. isdirectory () {file [] files = Infile. listfiles (); For (File file: Files) zipfile (file, ZOS, Dir + infile. getname () + "\");} else {string entryname = NULL; If (! "". Equals (DIR) entryname = dir + infile. getname (); elseentryname = infile. getname (); zipentry entry = new zipentry (entryname); ZOS. putnextentry (entry); bufferedinputstream is = new bufferedinputstream (New fileinputstream (infile); // File Buffer byte [] datas = new byte [2048]; int Len = 0; while (LEN = is. read (datas ))! =-1) ZOS. Write (datas, 0, Len); is. Close ();}}}

extract:

Import Java. io. bufferedoutputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. inputstream; import java.util.zip. zipentry; import java.util.zip. zipfile; import java.util.zip. zipinputstream; public class zipinputstreamtest {public static void main (string ARGs []) throws ioexception {file = new file ("D: \ test.zip"); // Compression File zipfile = new zipfile (File); // instantiate zipfile. Each zip compressed file can be represented as a zipfile // instantiate the zipinputstream object of a zip compressed file, you can use the getnextentry () method of this class to obtain zipinputstream = new zipinputstream (New fileinputstream (File); zipentry = NULL; while (zipentry = zipinputstream. getnextentry ())! = NULL) {string filename = zipentry. getname (); file temp = new file ("D: \ unpacktest \" + filename); If (! Temp. getparentfile (). exists () temp. getparentfile (). mkdirs (); bufferedoutputstream OS = new bufferedoutputstream (New fileoutputstream (temp); // File Buffer // outputstream OS = new fileoutputstream (temp ); // use the getinputstream method of zipfile to obtain the input stream inputstream = zipfile of the specific zipentry. getinputstream (zipentry); byte [] datas = new byte [2048]; int Len = 0; while (LEN = is. read (datas ))! =-1) OS. Write (datas, 0, Len); OS. Close (); is. Close () ;}zipinputstream. Close ();}}

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.