java Extracting Zip file

Tags: , ,



I’m looking for a way to extract Zip file. So far I have tried java.util.zip and org.apache.commons.compress, but both gave a corrupted output.

Basically, the input is a ZIP file contain one single .doc file.

java.util.zip: Output corrupted. org.apache.commons.compress: Output blank file, but with 2 mb size.

So far only the commercial software like Winrar work perfectly. Is there a java library that make use of this?

This is my method using java.util library:

public void extractZipNative(File fileZip)
{
    ZipInputStream zis;
    StringBuilder sb;
    try {
        zis = new ZipInputStream(new FileInputStream(fileZip));
        ZipEntry ze = zis.getNextEntry();

        byte[] buffer = new byte[(int) ze.getSize()];

        FileOutputStream fos = new FileOutputStream(this.tempFolderPath+ze.getName());

        int len;
        while ((len=zis.read(buffer))>0)
        {
            fos.write(buffer);
        }
        fos.flush();
        fos.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally 
    {
        if (zis!=null) 
        {
            try { zis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

Many thanks, Mike

Answer

It is an error in my code. I need to specify the offset and len of bytes write.



Source: stackoverflow