0

I have split zip files that I'm trying to merge using Java. But I get Unexpected end of ZLIB input stream error. Any thoughts on what I'm doing wrong?

    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, 0, len);
        }
    }

    inStream.close();

}
Ichigo Kurosaki
  • 3,765
  • 8
  • 41
  • 56
Joe
  • 1
  • Merging files using OutputStreams is a bad idea. You are better off reading the zip entries of both the files into an list and write those entries to an new zip file – Sync it Jan 18 '21 at 11:49
  • Put them into a list? can you give an example please? The split zip files are from a larger zip file that I've split using 7zip. I'm trying to combine them again in java. – Joe Jan 18 '21 at 12:28

1 Answers1

1

JDK's zip does not support split zip files. And moreover you cannot work with Input/Outpustreams when working with split zip files. And also when merging a split zip files a lot of headers in the zip file have to be updated. zip4j supports such a feature. Sample code:

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