java文件压缩加密,使用流的方式

使用net.lingala.zip4j来进行文件加密压缩。

添加依赖net.lingala.zip4j包依赖,这里使用的是最新的包2.11.5版本。

<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>${zip4j.version}</version>
</dependency>

使用ZipFile加密压缩文件

 public static void main(String[] args) throws Exception{
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setEncryptFiles(true);
        zipParameters.setEncryptionMethod(EncryptionMethod.AES);
        // Below line is optional. AES 256 is used by default. 
        // You can override it to use AES 128. AES 192 is supported only for extracting.
        zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
        List<File> filesToAdd = Arrays.asList(
            new File("F:\\test1.txt"),
            new File("F:\\test2.txt")
        );
        ZipFile zipFile = new ZipFile("F:\\test.zip", "test".toCharArray());
        zipFile.addFiles(filesToAdd, zipParameters);
    }

执行程序,压缩后:
在这里插入图片描述
网上能找到的基本的的参考例子都是这个,但是我们真正使用的时候是不会从本地读取文件的方式的,是使用流的方式,入参,出参都应该是个流。官方文档也提供了使用流的方式压缩文件的。

使用ZipOutputStream加密压缩文件

public static void main(String[] args) throws Exception {
        String password = "test";
        List<File> filesToAdd = Arrays.asList(
            new File("F:\\test1.txt"),
            new File("F:\\test2.txt")
        );
        byte[] buff = new byte[4096];
        int readLen;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try(ZipOutputStream zipOutputStream = new ZipOutputStream(bos, password.toCharArray(), Charset.defaultCharset())) {
            for (File fileToAdd : filesToAdd) {
                ZipParameters parameters = new ZipParameters();
                parameters.setEncryptionMethod(EncryptionMethod.AES);
                parameters.setEncryptFiles(true);
                parameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
                parameters.setFileNameInZip(fileToAdd.getName());
                zipOutputStream.putNextEntry(parameters);
                try(InputStream inputStream = new FileInputStream(fileToAdd)) {
//                    zipOutputStream.write(inputStream.readAllBytes());
                    while ((readLen = inputStream.read(buff)) != -1) {
                        zipOutputStream.write(buff, 0, readLen);
                    }
                }
                zipOutputStream.closeEntry();
            }
        }
        try (FileOutputStream fileOutputStream = new FileOutputStream("F:\\test.zip")){
            fileOutputStream.write(bos.toByteArray());
        }
    }

这里说一下像ByteArrayOutputStream这种内存流是不用关闭的,这种的不使用的时候会自动回收了,只有FileOutputStream这种操作了磁盘,或者网络,IO的需要手动关闭,这里放到try块去,自动调用close方法。

官方文档:https://github.com/srikanth-lingala/zip4j

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值