Java compression and decompression zip, Jar, gzip files

Source: Internet
Author: User

We often use the compression software such as WinZip to compress files for easy transmission. In Java also provides the file compression to reduce the amount of data transmitted when the class, it is convenient to compress the file into the form of zip, JAR, gzip, Gzip is mainly in the Linux system compressed files.

The following is mainly about the zip format of the compressed file, and the jar, gzip compressed file is a similar use.

Zip is a very common form of compression, in Java to achieve ZIP compression is mainly used in Java.util.zip this package inside the class. There are mainly ZipFile, Zipoutputstream, Zipinputstream and ZipEntry. Zipoutputstream is used to compress files, Zipinputstream and ZipFile are used to extract files, in the process of compression and decompression, ZipEntry will be used. In the Java Zip Archive, each sub-file is a ZipEntry object.

Compressed files:

ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.nio.charset.Charset;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipOutputStream; Public classZipoutputstreamtest { Public Static voidMain (String args[])throwsIOException {test1 ();    Test2 (); }         Public Static voidTest1 ()throwsIOException {zipoutputstream Zos=NewZipoutputstream (NewFileOutputStream ("D:\\testzip.zip"), Charset.forname ("GBK")); //instantiate a ZipEntry object named Ab.txtZipEntry entry =NewZipEntry ("Ab.txt"); //Setting CommentsZos.setcomment ("Zip test for Single file"); //The generated ZipEntry object is added to the compressed file, and then the content written to the compressed file is placed inside the ZipEntry object .zos.putnextentry (entry); InputStream is=NewFileInputStream ("D:\\ab.txt"); intLen = 0;  while(len = Is.read ())! =-1) Zos.write (len);        Is.close ();    Zos.close (); }         Public Static voidTest2 ()throwsIOException {File inFile=NewFile ("D:\\test"); Zipoutputstream Zos=NewZipoutputstream (NewFileOutputStream ("D:\\test.zip"), Charset.forname ("GBK")); Zos.setcomment ("Multi-file Processing"); ZipFile (InFile, Zos,"");    Zos.close (); }         Public Static voidZipFile (File inFile, Zipoutputstream Zos, String dir)throwsIOException {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=NewZipEntry (entryName);            Zos.putnextentry (entry); InputStream is=NewFileInputStream (InFile); intLen = 0;  while(len = Is.read ())! =-1) Zos.write (len);        Is.close (); }    }    }

Unzip the file:

ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportJava.nio.charset.Charset;ImportJava.util.zip.ZipEntry;ImportJava.util.zip.ZipFile;ImportJava.util.zip.ZipInputStream; Public classZipinputstreamtest { Public Static voidMain (String args[])throwsIOException {File file=NewFile ("D:\\test.zip");//Compress FilesZipFile ZipFile =NewZipFile (file);//instantiating ZipFile, each zip archive can be represented as a zipfile//instantiate the Zipinputstream object of a zip archive, and use the class's Getnextentry () method to get each ZipEntry object in turnZipinputstream Zipinputstream =NewZipinputstream (NewFileInputStream (file), Charset.forname ("GBK")); ZipEntry ZipEntry=NULL;  while((ZipEntry = Zipinputstream.getnextentry ())! =NULL) {String fileName=Zipentry.getname (); File Temp=NewFile ("d:\\unpacktest\\" +fileName); if(!Temp.getparentfile (). exists ()) Temp.getparentfile (). Mkdirs (); OutputStream OS=NewFileOutputStream (temp); //get the specific ZipEntry input stream by ZipFile's getInputStream methodInputStream is =Zipfile.getinputstream (ZipEntry); intLen = 0;  while(len = Is.read ())! =-1) Os.write (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.