Unzipping a multi-part zip file volumes using Java

2019-04-09 14:34发布

问题:

I need to unzip a set of files that are a zip archive. This isn't a set of zip files, this is one big zip file that has been broken up into multiple zip files based on a size requirement.

For example if you have a 2.5MB zip file and your mail system only supports 1MB files, you can ask Zip to create 3 files of at most 1MB.

So it creates a.zip.001, a.zip.002, a.zip.003 ... different libraries name them differently but essentially they all work the same way.

How do you unzip this in java? It doesn't look like the compression libs in std supports this.

Thanks.

回答1:

Try to concatenate all the files into a single file and then extract the single file. Something like:

    File dir = new File("D:/arc");
    FileOutputStream fos = new FileOutputStream(new File(
            "d:/arc/archieve-full.zip"));
    FileInputStream fis = null;
        Set<String> files = new TreeSet<String>();
        for (String fname : dir.list()) {
            files.add(fname);
        }
        for (String fname : files) {
        try {
            fis = new FileInputStream(new File(dir.getAbsolutePath(), fname));
            byte[] b = new byte[fis.available()];
            fis.read(b);
            fos.write(b);
        } finally {
            if (fis != null) {
                fis.close();
            }
            fos.flush();
        }
    }
    fos.close();
    ZipFile zipFile = new ZipFile("d:/arc/archieve-full.zip");
    /*extract files from zip*/

Update: used a TreeSet to sort the file names, as dir.list() doesn't guarantee alphabetical order.