首页 > 解决方案 > 使用 JAVA 中的输入流解压缩加密的 zip 文件

问题描述

需要一个 Java 解决方案来使用 inputstream 解压缩一个巨大的文件(不适合内存)。在这种情况下,文件对象不可用。该文件受密码保护。使用 File 对象的解决方案将无济于事。

我已经尝试过 7Zip,但它不支持上述情况。

标签: javaunzippassword-protection

解决方案


当你使用流时,你不应该读取比需要更多的数据。你试过这个吗?

   public void unzip(InputStream is, Cipher cypher) throws IOException {
        ZipInputStream zis = new ZipInputStream(new CipherInputStream(is,cypher));
        ZipEntry zipEntry = zis.getNextEntry();
        byte[] buffer = new byte[1024];
        while (zipEntry != null) {
            File newFile = new File(zipEntry.getName());
            FileOutputStream fos = new FileOutputStream(newFile);
            int len;
            while ((len = zis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }
            fos.close();
            zipEntry = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
    }

推荐阅读