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;
            }
        }
    
    
  • 相关阅读:
    进程和线程的概述
    注意两个词汇的区别:并行和并发
    WebRTC MCU( Multipoint Conferencing Unit)服务器调研
    (译)WebRTC实战: STUN, TURN, Signaling
    关于图数据库查询语言:Cypher
    Neo4j安装后的密码修改
    XYC2016上半年工作笔记整理
    WebRTC技术调研
    在Django中使用Neo4j
    传统企业做互联网的困局
  • 原文地址:https://www.cnblogs.com/XueTing/p/15710715.html
Copyright © 2011-2022 走看看