5

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)

Hei
  • 513
  • 3
  • 13
  • 29
  • related to file permission ? – Swagatika Nov 30 '12 at 09:39
  • Jan: I have created another zip file and tested, still the same exception. Swagatika: I am not quite familiar with handling zip file using Java, my testing environment is Linux and I have checked with the permission, the parent directory shows me drwxr-xr-x – Hei Nov 30 '12 at 09:44
  • is there a causedBy in the stacktrace? – bvanvelsen - ACA Group Nov 30 '12 at 09:46
  • 1
    check this question, similar issue http://stackoverflow.com/questions/325202/java-util-zip-zipexception-error-in-opening-zip-file – Toni Toni Chopper Nov 30 '12 at 09:47
  • Is the Exception the same if you remove the file? I'm asking this because the file just might not found. – Kai Nov 30 '12 at 09:47
  • Have you verified that the file can not be inflated with some other program such as **gunzip**? – heikkim Nov 30 '12 at 11:30

1 Answers1

0

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();
        }
    }
}
Perneel
  • 3,317
  • 7
  • 45
  • 66
  • Thanks for the suggestion, but unfortunately, the exact same exception has been thrown. – Hei Nov 30 '12 at 10:12