微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Java:尝试合并拆分的 zip 文件时 ZLIB 输入流意外结束

如何解决Java:尝试合并拆分的 zip 文件时 ZLIB 输入流意外结束

我尝试使用 Java 合并拆分的 zip 文件。但是我得到了意外结束的 ZLIB 输入流错误。对我做错了什么有任何想法吗?

    File bigZip = new File("bigZip.zip");
    
    List<String> zipList = Arrays.asList("src/14thmayreceipts.zip.001","src/14thmayreceipts.zip.002","src/14thmayreceipts.zip.003");
    Collections.sort(zipList);


    ZipOutputStream outputStream = new ZipOutputStream(new FileOutputStream(bigZip));
    for (String entry : zipList) {
        readWriteZip(outputStream,entry);
    }
    
    outputStream.close();

}

private static void readWriteZip(ZipOutputStream out,String fileName) throws IOException,EOFException  {

    File file = new File(fileName);
    ZipInputStream inStream = new ZipInputStream(new FileInputStream(file));
    byte[] buffer = new byte[1024];
    int len = 0;

    for (ZipEntry e; (e = inStream.getNextEntry()) != null; ) {
        out.putNextEntry(e);
        while ((len = inStream.read(buffer)) > 0) {
            out.write(buffer,len);
        }
    }

    inStream.close();

}

解决方法

JDK 的 zip 不支持拆分 zip 文件。此外,在使用拆分 zip 文件时,您无法使用输入/输出流。而且在合并拆分的 zip 文件时,必须更新 zip 文件中的许多标题。 zip4j 支持这样的功能。示例代码:

ZipFile zipFile = new ZipFile("splitZipFileThatHasToBeMerged.zip");
zipFile.mergeSplitZipFiles("mergedOutputZipFile.zip");

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。