获取压缩包中的文本字符串。

2020-06-08 17:05:03 浏览数 (1)

业务如下

通过指定位置压缩包解析公钥,和密文,解析客户信息,不需要解压,那是我手动解压看效果的。 ps:中文可能会产生乱码,调一下编码。

代码如下

代码语言:javascript复制
  public static void main(String[] args) throws Exception {
        new fileCheck().readZipFile("/Users/cuixiaoyan/Downloads/嘉士利科技有限公司.zip");
    }

    /**
     * 读取文件校验
     * @param filePath
     * @throws Exception
     */
    public String readZipFile(String filePath) throws Exception {
        //获取文件输入流
        FileInputStream input = new FileInputStream(filePath);
        //获取ZIP输入流(一定要指定字符集Charset.forName("GBK")否则会报java.lang.IllegalArgumentException: MALFORMED)
        ZipInputStream zipInputStream = new ZipInputStream(new BufferedInputStream(input), Charset.forName("GBK"));
        //定义ZipEntry置为null,避免由于重复调用zipInputStream.getNextEntry造成的不必要的问题
        ZipEntry ze = null;
        //公钥
        String publicKey = "";
        //密文
        String cipher = "";
        //循环遍历
        while ((ze = zipInputStream.getNextEntry()) != null) {
            //读取
            BufferedReader br = new BufferedReader(new InputStreamReader(zipInputStream, Charset.forName("GBK")));
            String line;
            while ((line = br.readLine()) != null) {
                if (ze.getName().equals("publicKey.txt")) {
                    publicKey = line  = "n";
                }
                if (ze.getName().equals("cipher.txt")) {
                    cipher  = line  = "n";
                }
            }
        }
        //获取明文
        String clear = rSAUtilPbulicKey.decryptByPublicKey(publicKey, cipher);
        zipInputStream.closeEntry();
        input.close();
        return clear;
    }

0 人点赞