Java : Unexpected end of ZLIB input stream while attempting to merge split zip files

193 Views Asked by At

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();

}
1

There are 1 best solutions below

0
Srikanth Reddy Lingala On

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");