Reading data from multiple zip files and combining them to one

蹲街弑〆低调 提交于 2019-12-05 23:19:33

Create ZipOutputStream once and pass it to readZip() method, like:

public static void main(String args[]) {
    ZipOutputStream outStream = new ZipOutputStream(new FileOutputStream(sourceFilePath));
    readZip(outStream , targetFilePath1);
    readZip(outStream , targetFilePath2);
    readZip(outStream , targetFilePath3);
    readZip(outStream , targetFilePath4);
}

Then you have an error dealing with copying the data from one zip to another... You need to copy each file in the zip file like this:

static void readZip(ZipOutputStream outStream, String targetFile)
        throws Exception {
    ZipInputStream inStream = new ZipInputStream(new FileInputStream(
            targetFile));
    byte[] buffer = new byte[1024];
    int len = 0;

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

}

Every time you call new ZipOutputStream, it creates a new empty file, and wipes out everything you have written to it before. You have to create the stream outside of readZip, and pass it in to each call rather than creating a new stream every time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!