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


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

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

    <dependency>
        <groupId>net.lingala.zip4j</groupId>
        <artifactId>zip4j</artifactId>
        <version>${zip4j.version}</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    使用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);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

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

    使用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());
            }
        }
    
    • 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

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

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

  • 相关阅读:
    本地化小程序运营 同城小程序开发
    用人工智能压缩图像的尝试和计算
    易基因|深度综述:m6A RNA甲基化在大脑发育和疾病中的表观转录调控作用
    Anaconda常用命令
    图像识别与处理学习笔记(四)贝叶斯决策和概率密度估计
    六级备考24天|CET-6|翻译技巧1&2|理解背诵|11:00~12:00+14:20~15:30
    问遍了身边的面试官朋友,我整理出这份 Java 集合高频面试题(2021年最新版)
    用CI/CD工具Vela部署Elasticsearch + C# 如何使用
    软考高级系统架构设计师系列论文十六:论软件系统测试及其应用
    php练习06
  • 原文地址:https://blog.csdn.net/u010180738/article/details/134497653