简体   繁体   中英

ZipException when unzipping file in Java

I am totally fresh in handling zip file using Java, and I have encountered a strange situation.

Here is the method I am using for unzip:

public void unzip(File zipFile, File rootDir) throws IOException
{
    ZipFile zip = new ZipFile(zipFile);
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();

    while(entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) { // if its a directory, create it
            continue;
        }

        if (!f.exists()) {
            f.getParentFile().mkdirs();
            f.createNewFile();
        }

        /*BufferedInputStream bis = new BufferedInputStream(zip.getInputStream(entry)); // get the input stream
        BufferedOutputStream bos = new BufferedOutputStream(new java.io.FileOutputStream(f));
        while (bis.available() > 0) {  // write contents of 'is' to 'fos'
            bos.write(bis.read());
        }
        bos.close();
        bis.close();*/

        InputStream is = zip.getInputStream(entry);
        OutputStream os = new java.io.FileOutputStream(f);
        byte[] buf = new byte[4096];
        int r ;
        while ((r = is.read(buf)) != -1) {
            os.write(buf, 0, r);
        }
        os.close();
        is.close();
    }   
}

However, a IOException has been thrown and the message is:

INFO | jvm 1 | 2012/11/30 01:58:05 | java.util.zip.ZipException: error in opening zip file

INFO | jvm 1 | 2012/11/30 01:58:05 | at java.util.zip.ZipFile.open(Native Method)

INFO | jvm 1 | 2012/11/30 01:58:05 | at java.util.zip.ZipFile.(ZipFile.java:127)

INFO | jvm 1 | 2012/11/30 01:58:05 | at java.util.zip.ZipFile.(ZipFile.java:143)

Could anyone help me on this?

Thanks a lot.

Update:

I am using Linux as a testing environment. The permission for the unzip directory is drwxr-xr-x –

Update 02:

By adopting the suggestion from @heikkim,

I have just tried to use unzip commend in linux, trying to unzip my file manually. I have the following message:

Archive: TMA_Template.zip caution: zipfile comment truncated warning [TMA_Template.zip]: zipfile claims to be last disk of a multi-part archive; attempting to process anyway, assuming all parts have been concatenated together in order. Expect "errors" and warnings...true multi-part support doesn't exist yet (coming soon). error [TMA_Template.zip]: missing 6366880279 bytes in zipfile (attempting to process anyway) error [TMA_Template.zip]: attempt to seek before beginning of zipfile (please check that you have transferred or created the zipfile in the appropriate BINARY mode and that you have compiled UnZip properly)

Could you try with this method:

private void unzip() throws IOException {
    int BUFFER = 2048;
    BufferedOutputStream dest = null;
    BufferedInputStream is = null;
    ZipEntry entry;
    ZipFile zipfile = new ZipFile("latest.zip");
    Enumeration e = zipfile.entries();
    (new File(root)).mkdir();
    while (e.hasMoreElements()) {
        entry = (ZipEntry) e.nextElement();
        //outText.setText(outText.getText() + "\nExtracting: " + entry);
        if (entry.isDirectory()) {
            (new File(root + entry.getName())).mkdir();
        } else {
            (new File(root + entry.getName())).createNewFile();
            is = new BufferedInputStream(zipfile.getInputStream(entry));
            int count;
            byte data[] = new byte[BUFFER];
            FileOutputStream fos = new FileOutputStream(root + entry.getName());
            dest = new BufferedOutputStream(fos, BUFFER);
            while ((count = is.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }
            dest.flush();
            dest.close();
            is.close();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM