Reference:http://www.open-open.com/lib/view/open1381641653833.html
Import java.util.zip.* in the Java API; The package contains all the related operations of Java for compressed files.
We can use the method in the package, combined with the relevant knowledge in Io, the file compression and decompression related operations.
ZipFile
Every compressed file in Java can be represented using ZipFile.
New File ("F:/zippath.zip"new zipfile (file); System.out.println ("The name of the compressed file is:" + zipfile.getname ());
Compress a single file
/**Compress a single file*/ Public Static voidZipFile (String filepath, string zippath) {Try{File File=NewFile (filepath); File ZipFile=NewFile (Zippath); InputStream input=Newfileinputstream (file); Zipoutputstream ZipOut=NewZipoutputstream (NewFileOutputStream (ZipFile)); Zipout.putnextentry (NewZipEntry (File.getname ())); inttemp = 0; while(temp = Input.read ())! =-1) {zipout.write (temp); } input.close (); Zipout.close (); } Catch(Exception e) {e.printstacktrace (); }}Application:
ZipFile ("D:/hello.txt", "D:/hello.zip");
Compress multiple files (folders)
/**compress multiple files at once, file in one folder*/ Public Static voidzipmultifile (String filepath, string zippath) {Try{File File=NewFile (filepath);//folder to be compressedFile ZipFile =NewFile (Zippath); InputStream input=NULL; Zipoutputstream ZipOut=NewZipoutputstream (NewFileOutputStream (ZipFile)); if(File.isdirectory ()) {file[] files=File.listfiles (); for(inti = 0; i < files.length; ++i) {Input=NewFileInputStream (Files[i]); Zipout.putnextentry (NewZipEntry (File.getname () + File.separator +files[i].getname ())); inttemp = 0; while(temp = Input.read ())! =-1) {zipout.write (temp); } input.close (); }} zipout.close (); } Catch(Exception e) {e.printstacktrace (); }}Application:
Zipmultifile ("F:/uu", "F:/zippath.zip");
Unzip a single file
/**Unzip (unzip a single file)*/ Public Static voidzipcontrafile (String Zippath, String outfilepath, string filename) {Try{File File=NewFile (Zippath);//compressed file path and file nameFile OutFile =NewFile (Outfilepath);//extracted path and file nameZipFile ZipFile =Newzipfile (file); ZipEntry entry= Zipfile.getentry (filename);//The extracted file nameInputStream input =Zipfile.getinputstream (entry); OutputStream Output=NewFileOutputStream (OutFile); inttemp = 0; while(temp = Input.read ())! =-1) {output.write (temp); } input.close (); Output.close (); } Catch(Exception e) {e.printstacktrace (); }}Application:
Zipcontrafile ("D:/hello.zip", "D:/eee.txt", "hello.txt");
Unzip multiple Files Zipinputstream class: When we need to extract multiple files, ZipEntry cannot be used.
If you want to manipulate more complex compressed files, we must use the Zipinputstream class.
/**Decompression (contains multiple files in a compressed file) can be used instead of the method above. * Zipinputstream class * When we need to extract multiple files, ZipEntry will not be able to use, * if you want to manipulate more complex compressed files, we must use the Zipinputstream class **/ Public Static voidzipcontramultifile (String zippath, String outzippath) {Try{File File=NewFile (Zippath); File OutFile=NULL; ZipFile ZipFile=Newzipfile (file); Zipinputstream Zipinput=NewZipinputstream (Newfileinputstream (file)); ZipEntry entry=NULL; InputStream input=NULL; OutputStream Output=NULL; while((Entry = Zipinput.getnextentry ())! =NULL) {System.out.println ("Decompress" + entry.getname () + "file"); OutFile=NewFile (Outzippath + file.separator +entry.getname ()); if(!Outfile.getparentfile (). exists ()) {Outfile.getparentfile (). mkdir (); } if(!outfile.exists ()) {Outfile.createnewfile (); } Input=Zipfile.getinputstream (entry); Output=NewFileOutputStream (OutFile); inttemp = 0; while(temp = Input.read ())! =-1) {output.write (temp); } input.close (); Output.close (); } } Catch(Exception e) {e.printstacktrace (); }}Application:
Zipcontramultifile ("F:/zippath.zip", "d:/"); Zipcontramultifile ("D:/hello.zip", "d:/");
[Java Basics] Compress and decompress files using Java.util.zip package