Package cn. luxh. utils; 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 * @ Author Luxh */ Public class ZipFileUtil { /** * Compressing a file into zip format * @ Param files the file to be compressed * @ Param zipFilePath: zip file path after compression, for example, "D:/test/aa.zip "; */ Public static void compressFiles2Zip (File [] files, String zipFilePath ){ 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 required Zaos. setUseZip64 (Zip64Mode. AsNeeded ); // Encapsulate each file using ZipArchiveEntry // Use ZipArchiveOutputStream to write to the compressed file For (File file: files ){ If (file! = Null ){ ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry (file, file. getName ()); Zaos. putArchiveEntry (zipArchiveEntry ); InputStream is = null; Try { Is = new FileInputStream (file ); Byte [] buffer = new byte [1, 1024*5]; Int len =-1; While (len = is. read (buffer ))! =-1 ){ // Write the buffer byte to ZipArchiveEntry Zaos. 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: the path of the extracted file, for example, "D:/test /" */ Public static void decompressZip (String zipFilePath, String saveFileDir ){ If (isEndsWithZip (zipFilePath )){ File file = new File (zipFilePath ); If (file. exists ()){ InputStream is = null; // Can read Zip archives ZipArchiveInputStream zais = null; Try { Is = new FileInputStream (file ); Zais = new ZipArchiveInputStream (is ); ArchiveEntry 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 extracted file to the specified path File entryFile = new File (entryFilePath ); OS = 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. Zip file name is used as the suffix. * @ Param fileName the file name to be determined * @ Return: return true for a zip file; otherwise, return false. */ Public static boolean isEndsWithZip (String fileName ){ Boolean flag = false; If (fileName! = Null &&! "". Equals (fileName. trim ())){ If (fileName. endsWith (". ZIP") | fileName. endsWith (". zip ")){ Flag = true; } } Return flag; } } 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 { @ Test Public void testCompressFiles2Zip (){ // Directory for storing the files to be compressed File srcFile = new File ("D:/test "); // Zip file path after compression String zipFilePath = "d:/test2/test.zip "; If (srcFile. exists ()){ File [] files = srcFile. listFiles (); ZipFileUtil. compressFiles2Zip (files, zipFilePath ); } } @ Test Public void testDecompressZip (){ // Path of the compressed package String zipFilePath = "d:/test2/test.zip "; // The directory where the extracted files are stored String saveFileDir = "d:/test2 /"; // Call the decompression method ZipFileUtil. decompressZip (zipFilePath, saveFileDir ); } } |