Apache commons compress is a class library for compressing and decompressing files.
You can operate files in Ar, cpio, Unix dump, tar, zip, Gzip, xz, pack200 and Bzip2 formats with powerful functions.
Here, we will write two methods to compress files into ZIP files and decompress files from ZIP files using commons compress.
Directly paste the tool classCode:
Package CN. luxh. utils; import Java. io. bufferedinputstream; 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. io. outputstream; import Org. apache. commons. compress. archivers. archiveentry; import org.apache.commons.compress.archivers.zip. zip64mode; import Org. apache. commons. Compress.archivers.zip. ziparchiveentry; import org.apache.commons.compress.archivers.zip. ziparchiveinputstream; import org.apache.commons.compress.archivers.zip. ziparchiveoutputstream; /*** ZIP file tool class * @ author luxh */public class zipfileutil {/*** compresses the file into a ZIP format * @ Param files to be compressed * @ Param zipfilepath ZIP file path, for example, "d:/test/aa.zip"; */public static void compressfiles2zip (file [] files, string zipfilepa Th) {If (files! = NULL & files. length> 0) {If (isendswithzip (zipfilepath) {ziparchiveoutputstream zaos = NULL; try {file zipfile = new file (zipfilepath); zaos = new ziparchiveoutputstream (zipfile ); // use zip64 extensions for all entries where they are requiredzaos. setusezip64 (zip64mode. asneeded); // encapsulate each file with ziparchiveentry // write it to the compressed file with ziparchiveoutputstream for (File file: Files) {If (file! = NULL) {ziparchiveentry = new ziparchiveentry (file, file. getname (); zaos. putarchiveentry (ziparchiveentry); inputstream is = NULL; try {is = new bufferedinputstream (New fileinputstream (File); byte [] buffer = new byte [1024*5]; int Len =-1; while (LEN = is. read (buffer ))! =-1) {// write the buffer byte to ziparchiveentryzaos. write (buffer, 0, Len);} // writes all necessary data for this entry. zaos. closearchiveentry ();} catch (exception e) {Throw new runtimeexception (E);} finally {If (is! = NULL) is. close () ;}} zaos. finish () ;}catch (exception e) {Throw new runtimeexception (e) ;}finally {try {If (zaos! = NULL) {zaos. close () ;}} catch (ioexception e) {Throw new runtimeexception (e );}}}}} /*** decompress the ZIP file to the specified folder * @ Param zipfilepath ZIP file path, for example, "d:/test/aa.zip" * @ Param savefiledir, for example, "d:/test/" */public static void decompresszip (string zipfilepath, string savefiledir) {If (isendswithzip (zipfilepath) {file = new file (zipfilepath ); if (file. exists () {inputstream is = NULL; // can read zip Archi Vesziparchiveinputstream Zais = NULL; try {is = new fileinputstream (File); Zais = new ziparchiveinputstream (is); archiveentry = NULL; // read each file in the zip package // then write the file to the specified folder while (archiveentry = Zais. getnextentry ())! = NULL) {// get the file name string entryfilename = archiveentry. getname (); // construct the decompressed file storage path string entryfilepath = savefiledir + entryfilename; byte [] content = new byte [(INT) archiveentry. getsize ()]; Zais. read (content); outputstream OS = NULL; try {// write the decompressed file to the specified path file entryfile = new file (entryfilepath ); OS = new bufferedoutputstream (New fileoutputstream (entryfile); OS. write (content);} catch (ioexception e) {Throw new Ioexception (E);} finally {If (OS! = NULL) {OS. flush (); OS. close () ;}}} catch (exception e) {Throw new runtimeexception (E);} finally {try {If (Zais! = NULL) {Zais. Close ();} If (is! = NULL) {is. close () ;}} catch (ioexception e) {Throw new runtimeexception (e );}}}}} /*** the file name suffix * @ Param filename indicates the file name to be determined. * @ return indicates the ZIP file. True is returned. Otherwise, false */public static Boolean isendswithzip (string filename) is returned) {Boolean flag = false; If (filename! = NULL &&! "". Equals (filename. trim () {If (filename. endswith (". zip ") | filename. endswith (". zip ") {flag = true ;}} return flag ;}}
Test again:
Package CN. luxh. test; import Java. io. file; import Org. JUnit. test; import CN. luxh. utils. zipfileutil;/*** test zipfileutil compression and decompression methods * @ author luxh */public class zipfileutiltest {@ testpublic void testcompressfiles2zip () {// directory for storing the files to be compressed file srcfile = new file ("D:/test"); // The compressed zip file path string zipfilepath = "D: /Test2/test.zip "; if (srcfile. exists () {file [] files = srcfile. listfiles (); zipfileutil. compressfiles2zip (files, zipfilepath) ;}@ testpublic void testdecompresszip () {// path of the compressed package string zipfilepath = "D:/Test2/test.zip "; // string savefiledir = "D:/Test2/"; // call the decompression method zipfileutil. decompresszip (zipfilepath, savefiledir );}}