Java提供的 zip util 沒有支援加密的zip格式, 若有需要可使用 Zip4j 提供的函式來進行, 功能完整, 也有範例
►壓縮檔案
http://www.lingala.net/zip4j/
►壓縮檔案
►壓縮含蜜瑪檔案try { //檔案若已存在則會加入,若存在檔非zip格式則會拋出異常 ZipFile zipFile = new ZipFile("c:\\my.zip"); ArrayList filesToAdd = new ArrayList(); filesToAdd.add(new File("c:\\sample.txt")); filesToAdd.add(new File("c:\\myvideo.avi")); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); // set compression method to deflate compression // DEFLATE_LEVEL_FASTEST - Lowest compression level but higher speed of compression // DEFLATE_LEVEL_FAST - Low compression level but higher speed of compression // DEFLATE_LEVEL_NORMAL - Optimal balance between compression level/speed // DEFLATE_LEVEL_MAXIMUM - High compression level with a compromise of speed // DEFLATE_LEVEL_ULTRA - Highest compression level but low speed parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); zipFile.addFiles(filesToAdd, parameters); } catch (ZipException e) { e.printStackTrace(); }
►壓縮檔案分割try { ZipFile zipFile = new ZipFile("c:\\my.zip"); ArrayList filesToAdd = new ArrayList(); filesToAdd.add(new File("c:\\ZipTest\\sample.txt")); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD);//Zip4jConstants.ENC_METHOD_AES parameters.setPassword("test123!"); zipFile.addFiles(filesToAdd, parameters); } catch (ZipException e) { e.printStackTrace(); }
►解壓縮所有檔案try { ZipFile zipFile = new ZipFile("c:\\my.zip"); ArrayList filesToAdd = new ArrayList(); filesToAdd.add(new File("c:\\sample.txt")); ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 壓縮檔若已存在則拋出異常 zipFile.createZipFile(filesToAdd, parameters, true, 10485760); } catch (ZipException e) { e.printStackTrace(); }
►解壓縮指定檔案try { ZipFile zipFile = new ZipFile("c:\\my.zip"); if (zipFile.isEncrypted()) { zipFile.setPassword("password"); } zipFile.extractAll("c:\\ZipTest"); } catch (ZipException e) { e.printStackTrace(); }
►壓縮檔案內移除資料try { ZipFile zipFile = new ZipFile("c:\\my.zip"); if (zipFile.isEncrypted()) { zipFile.setPassword("test123!"); } //指定檔案名稱 zipFile.extractFile("Ronan_Keating_-_In_This_Life.mp3", "c:\\ZipTest\\"); zipFile.extractFile("FolderToAdd\\myvideo.avi", "c:\\ZipTest\\"); //巡迴方式 List fileHeaderList = zipFile.getFileHeaders(); for (int i = 0; i < fileHeaderList.size(); i++) { FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); zipFile.extractFile(fileHeader, "c:\\ZipTest\\"); } //解壓縮並變更檔名 UnzipParameters unzipParameters = new UnzipParameters(); unzipParameters.setIgnoreDateTimeAttributes(true); unzipParameters.setIgnoreAllFileAttributes(true); zip.extractFile("test.txt", "\sdcard", unzipParameters, "新檔名"); } catch (ZipException e) { e.printStackTrace(); }
►壓縮檔內資訊try { ZipFile zipFile = new ZipFile("c:\\my.zip"); // 指定檔 zipFile.removeFile("myvideo.avi"); // 從 FileHeader 方式移除 if (zipFile.getFileHeaders() != null && zipFile.getFileHeaders().size() > 0) { zipFile.removeFile((FileHeader)zipFile.getFileHeaders().get(0)); } else { System.out.println("This cannot be demonstrated as zip file does not have any files left"); } } catch (ZipException e) { e.printStackTrace(); }
try { ZipFile zipFile = new ZipFile("c:\\my.zip"); List fileHeaderList = zipFile.getFileHeaders(); for (int i = 0; i < fileHeaderList.size(); i++) { FileHeader fileHeader = (FileHeader)fileHeaderList.get(i); // FileHeader contains all the properties of the file System.out.println("****File Details for: " + fileHeader.getFileName() + "*****"); System.out.println("Name: " + 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("************************************************************"); } } catch (ZipException e) { e.printStackTrace(); }
http://www.lingala.net/zip4j/