由于Java自带的ZipOutPutStream不支持生成带解压密码的压缩包,所以选择了普遍使用的zip4j。
引入依赖
1 2 3 4 5 6
| <dependency> <groupId>net.lingala.zip4j</groupId> <artifactId>zip4j</artifactId> <version>2.11.5</version> </dependency>
|
demo
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| package com.exam; import net.lingala.zip4j.io.outputstream.ZipOutputStream; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.model.enums.AesKeyStrength; import net.lingala.zip4j.model.enums.CompressionMethod; import net.lingala.zip4j.model.enums.EncryptionMethod; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Arrays; import java.util.List; public class ZipOutputStreamExample { public static void main(String[] args) { try { zipOutputStreamExample(new File("G:\\out\\result2.zip"), Arrays.asList(new File("G:\\out\\新建文本文档1.txt"), new File("G:\\out\\新建文本文档1 - 副本.txt"), new File("G:\\out\\新建文本文档1 - 副本 (2).txt"), new File("G:\\out\\新建文本文档1 - 副本 (3).txt")), "1234".toCharArray(), CompressionMethod.DEFLATE, true, EncryptionMethod.ZIP_STANDARD, AesKeyStrength.KEY_STRENGTH_256); } catch (IOException e) { e.printStackTrace(); } } public static void zipOutputStreamExample(File outputZipFile, List<File> filesToAdd, char[] password, CompressionMethod compressionMethod, boolean encrypt, EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength) throws IOException { ZipParameters zipParameters = buildZipParameters(compressionMethod, encrypt, encryptionMethod, aesKeyStrength); byte[] buff = new byte[4096]; int readLen; try(ZipOutputStream zos = initializeZipOutputStream(outputZipFile, encrypt, password)) { for (File fileToAdd : filesToAdd) { if (zipParameters.getCompressionMethod() == CompressionMethod.STORE) { zipParameters.setEntrySize(fileToAdd.length()); } zipParameters.setFileNameInZip(fileToAdd.getName()); zos.putNextEntry(zipParameters); try(InputStream inputStream = new FileInputStream(fileToAdd)) { while ((readLen = inputStream.read(buff)) != -1) { zos.write(buff, 0, readLen); } } zos.closeEntry(); } } } private static ZipOutputStream initializeZipOutputStream(File outputZipFile, boolean encrypt, char[] password) throws IOException { FileOutputStream fos = new FileOutputStream(outputZipFile); if (encrypt) { return new ZipOutputStream(fos, password); } return new ZipOutputStream(fos); } private static ZipParameters buildZipParameters(CompressionMethod compressionMethod, boolean encrypt, EncryptionMethod encryptionMethod, AesKeyStrength aesKeyStrength) { ZipParameters zipParameters = new ZipParameters(); zipParameters.setCompressionMethod(compressionMethod); zipParameters.setEncryptionMethod(encryptionMethod); zipParameters.setAesKeyStrength(aesKeyStrength); zipParameters.setEncryptFiles(encrypt); return zipParameters; } }
|
代码部分来源于zip4j
最终效果:
