zip4j带密码压缩文件

            
				net.lingala.zip4j
				zip4j
				2.2.8
			
public class ZipUtils {
    private ZipUtils() {
    }

    public static ZipFile zip(List fileList, String zipPath, String pwd) {
        try {
            ZipParameters para = new ZipParameters();
            //设置压缩方式,默认是COMP_DEFLATE
            para.setCompressionMethod(CompressionMethod.DEFLATE);
            //设置压缩级别,默认为0(即不压缩)
            para.setCompressionLevel(CompressionLevel.NORMAL);
            // 是否密码
            para.setEncryptFiles(true);
            // 加密方式
            para.setEncryptionMethod(EncryptionMethod.ZIP_STANDARD);
            ZipFile zipFile = new ZipFile(zipPath);
            zipFile.setPassword(pwd.toCharArray());
            Charset charset = Charset.forName("UTF-8");
            zipFile.setCharset(charset);
            zipFile.addFiles(fileList, para);
            return zipFile;
        } catch (Exception e) {
            log.error("压缩文件失败", e);
        }
        return null;
    }

    public static void main(String[] args) {
        List srcFiles = new ArrayList<>();
        srcFiles.add(new File("D:\\tmp\\1\\1.xls"));
        srcFiles.add(new File("D:\\tmp\\1\\2.xls"));
        ZipFile zipFile=zip(srcFiles,"D:\\tmp\\1\\1.zip","1234567");
        zipFile.getFile().delete();
    }
}

 

你可能感兴趣的:(JAVA)