zoukankan      html  css  js  c++  java
  • 读取zip加密包里面文件的内容

    1.添加依赖

    <dependency>
        <groupId>net.lingala.zip4j</groupId>
        <artifactId>zip4j</artifactId>
        <version>1.3.2</version>
    </dependency>
    
    

    2.编写工具类

    public class UnZipUtils {
    
            /**
             * 解密并解压zip文件,提取message文件中信息数据
             *
             * @param zipFile  原始文件路径
             * @param password 解压文件密码(可以为空)
             */
            public static JSONArray unZip(File zipFile, String password) throws Exception {
                JSONArray jsonArray = new JSONArray();
                ZipFile zFile = new ZipFile(zipFile); // 首先创建ZipFile指向磁盘上的.zip文件
                zFile.setFileNameCharset("GBK");
    
                if (zFile.isEncrypted()) {
                    zFile.setPassword(password.toCharArray()); // 设置密码
                }
    
                List<FileHeader> headerList = zFile.getFileHeaders();
                for (FileHeader fileHeader : headerList) {
                        BufferedReader reader = null;
                        StringBuffer sbf = new StringBuffer();
                        ZipInputStream inputStream = zFile.getInputStream(fileHeader);
    
                        try {
                            reader = new BufferedReader(new InputStreamReader(inputStream));
                            String tempStr;
                            while ((tempStr = reader.readLine()) != null) {
                                sbf.append(tempStr);
                            }
                            reader.close();
    
                            String str = sbf.toString();
                            str = str.replace("}", "},");
                            str = str.substring(str.indexOf("{"), str.lastIndexOf("}") + 1);
                            str = "[" + str + "]";
    
                            jsonArray = JSON.parseArray(str);
                        } finally {
                            if (reader != null) {
                                reader.close();
                            }
                            return jsonArray;
                        }
                }
                return jsonArray;
            }
        }
    
    
  • 相关阅读:
    win10下安装为知笔记的markdown插件
    最近一段时间的工作状态
    C++中的取余与取模
    来新项目后,最心虚的一个夜晚
    g++添加支持c++11的选项
    Linux ssh远程登陆方式:密码与公钥
    判断脚本加载完成
    解决ie6下最小高度问题
    display:inline-block的深入理解
    ff下button按钮上的文字垂直居中
  • 原文地址:https://www.cnblogs.com/XueTing/p/15710715.html
Copyright © 2011-2022 走看看