Official address: http://www.lingala.net/zip4j/(requires FQ)
Jar Package: Http://pan.baidu.com/s/145hwI
Demo Package: Http://pan.baidu.com/s/1vbFIB
One, compression
1. Standard compression
New ZipFile ("C:\\date.zip"new ArrayList (); Filestoadd.add (newnew zipparameters (); Parameters.setcompressionmethod (zip4jconstants.comp_deflate); Parameters.setcompressionlevel (Zip4jconstants.deflate_level_normal);
Set Password
Zipfile.addfiles (filestoadd, parameters);
2. AES Encryption
ZipFile ZipFile =New ZipFile ("C:\\ziptest\\test3.zip"); ArrayList Filestoadd = new ArrayList (); Filestoadd.add ( new File ("C:\\ziptest\\sample.txt" )); Zipparameters parameters = new Zipparameters (); Parameters.setcompressionmethod (zip4jconstants.comp_deflate);p Arameters.setcompressionlevel ( Zip4jconstants.deflate_level_normal); // set password Parameters.setencryptfiles (true);p Arameters.setencryptionmethod (Zip4jconstants.enc_method_aes); Parameters.setaeskeystrength (zip4jconstants.aes_strength_256);p Arameters.setpassword ("123456!")
3. Folder compression
New ZipFile ("C:\\ziptest\\test4.zip"); String Foldertoadd = "C:\\ziptest"new zipparameters (); Parameters.setcompressionmethod (zip4jconstants.comp_deflate); Parameters.setcompressionlevel (Zip4jconstants.deflate_level_normal); Zipfile.addfolder (folderToAdd, parameters);
4. Compress the files into the specified folder
New ZipFile ("C:\\ziptest\\test5.zip"new ArrayList (); Filestoadd.add (new File ("c:\\ziptest\\ Sample.txt "new zipparameters ();p Arameters.setcompressionmethod (zip4jconstants.comp_deflate); Parameters.setcompressionlevel (Zip4jconstants.deflate_level_normal);p arameters.setrootfolderinzip ("test2/" ); Zipfile.addfiles (filestoadd, parameters);
5. Add files to the zip
New ZipFile ("C:\\ziptest\\test1.zip"new zipparameters ();p Arameters.setcompressionmethod ( zip4jconstants.comp_deflate);p arameters.setfilenameinzip ("File 2.doc");p Arameters.setsourceexternalstream ( truenew FileInputStream ("c:\\ziptest\\ file 2.doc"); Zipfile.addstream (is, parameters); Is.close ();
6. Separating compressed files
New ZipFile ("C:\\ziptest\\test7.zip"new ArrayList (); Filestoadd.add (new File ("c:\\ziptest\\ Sample.txt "newTrue, 65536);
7. Create a ZIP stream
ArrayList Filestoadd =NewArrayList (); Filestoadd.add (New File ("C:\\ziptest\\sample.txt")); Zipoutputstream OutputStream =New Zipoutputstream (New FileOutputStream (New File ("C:\\ziptest\\test8.zip"))); Zipparameters parameters =NewZipparameters ();p Arameters.setcompressionmethod (zip4jconstants.comp_deflate);p Arameters.setcompressionlevel ( Zip4jconstants.deflate_level_normal);//Set Passwordparameters.setencryptfiles (True);p Arameters.setencryptionmethod (Zip4jconstants.enc_method_aes);p arameters.setaeskeystrength ( zip4jconstants.aes_strength_256);p Arameters.setpassword ("test123!"));for (int i = 0; I < filestoadd.size (); i++) {File File =(File) Filestoadd.get (i); Outputstream.putnextentry (file,parameters);if (File.isdirectory ()) {outputstream.closeentry (); continue;} InputStream InputStream = new fileinputstream (file); byte[] Readbuff = new byte[4096]; int readlen =-1; while ((Readlen = Inputstream.read (readbuff))! =-1) {outputstream.write (readbuff, 0, Readlen);} OutputS Tream.closeentry (); Inputstream.close ();} Outputstream.finish (); Outputstream.close ();
8. Delete files from zip
New ZipFile ("C:\\ziptest\\test1.zip"); Delete the specified file Zipfile.removefile ("Sample.txt"); Delete the first file null && zipfile.getfileheaders (). Size () > 0) {zipfile.removefile ((fileheader) Zipfile.getfileheaders (). Get (0else {System.out.println ("This cannot is demonstrated as ZIP file does not has a NY files left ");}
9. Get a list of files in zip
ZipFile ZipFile =New ZipFile ("C:\\ziptest\\test5.zip"); List fileheaderlist = Zipfile.getfileheaders (); for (int i = 0; i < fileheaderlist.size (); I++) {Fileheader fileheader = (Fileheader) fileheaderlist.get (i); System.out.println ("****file Details for:" + fileheader.getfilename () + "* * * * *" Fileheader.getfilename ()); SYSTEM.OUT.PRINTLN ("Compressed Size:" + Fileheader.getcompressedsize ()); SYSTEM.OUT.PRINTLN ("Uncompressed Size:" + Fileheader.getuncompressedsize ()); System.out.println ("CRC:" + FILEHEADER.GETCRC32 ()); System.out.println ("************************************************************" );}
Second, decompression
1. Unzip all Files
//Method one ZipFile ZipFile =New ZipFile ("C:\\ziptest\\test1.zip"); Zipfile.extractall ("C:\\ziptest1"); Method Two ZipFile zipFile2 = new ZipFile ("C:\\ziptest\\test2.zip"); if (zipfile2.isencrypted ()) {Zipfile2.setpassword ("test123!") );} List fileheaderlist = zipfile2.getfileheaders (); for (int i = 0; i < fileheaderlist.size (); i++) {Fileheader Fileheader = (fileheader) fileheaderlist.get (i); Zipfile2.extractfile (Fileheader, "c:\\ziptest2\\");}
2. Unzip all files to stream
ZipFile ZipFile =New ZipFile ("C:\\ziptest\\test2.zip");If(Zipfile.isencrypted ()) {Zipfile.setpassword ("test123!");} List fileheaderlist =Zipfile.getfileheaders ();for (int i = 0; I < fileheaderlist.size (); i++) {Fileheader Fileheader =(Fileheader) Fileheaderlist.get (i);if (fileheader! =Null{String Outfilepath = "c:\\ziptest3\\" + system.getproperty ("file.separator") +Fileheader.getfilename (); File OutFile =NewFile (Outfilepath);If(Fileheader.isdirectory ()) {Outfile.mkdirs ();Continue; } File Parentdir =Outfile.getparentfile ();if (!Parentdir.exists ()) {parentdir.mkdirs ();} Zipinputstream is =Zipfile.getinputstream (Fileheader); OutputStream OS =NewFileOutputStream (OutFile);int Readlen = -1byte["buff = new byte[ 4096while ((Readlen = Is.read (buff))! = -1) {Os.write (buff, 0 nullnull Fileheader.getfilename ());} else); }}
3. Extract individual files
New ZipFile ("C:\\ziptest\\test2.zip"); If (zipfile.isencrypted ()) { Zipfile.setpassword ("test123!") );} Zipfile.extractfile ("file. Doc", "c:\\ziptest4\\");
Source Text Address: http://rensanning.iteye.com/blog/1836727
Java uses zip4j to decompress "go"