0

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

1
  • 3
    May not be a Zip file Commented Apr 5, 2013 at 8:45

5 Answers 5

2

I think your input may be compressed by some "incompatible" zip program like 7zip. Try investigating first if it can be unpacked with a classical WinZip or such.

Javas zip handling is very well able to deal with zipped archives that come from a "compatible" zip compressor.

1
  • I add the code used to extract. It might all just come down to my java method afterall.
    – MikeNQ
    Commented Apr 5, 2013 at 9:41
2

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

0
1

it works for me

    ZipFile Vanilla = new ZipFile(new File("Vanilla.zip")); //zipfile defined and needs to be in directory
    Enumeration<? extends ZipEntry> entries = Vanilla.entries();// all (files)entries of zip file

    while(entries.hasMoreElements()){//runs while there is files in zip
        ZipEntry entry = entries.nextElement();//gets name of file in zip
        File folderw =new File("tkwgter5834");//creates new directory
        InputStream stream = Vanilla.getInputStream(entry);//gets input
        FileInputStream inpure= new FileInputStream("Vanilla.zip");//file input stream for zip file to read bytes of file
        FileOutputStream outter = new FileOutputStream(new File(folderw +"//"+ entry.toString())); //fileoutput stream creates file inside defined directory(folderw variable) by file's name
        outter.write(inpure.readAllBytes());// write into files which were created 
        outter.close();//closes fileoutput stream
    }
1
  • Please, provide some context to your answer. It will help other users to understand the answer easily. Commented Apr 11, 2021 at 6:23
0

Have you tried jUnrar? Perhaps it might work: https://github.com/edmund-wagner/junrar

If that doesn't work either, I guess your archive is corrupted in some way.

1
  • Winrar extraction work though. I will give your suggestion a try, thanks
    – MikeNQ
    Commented Apr 5, 2013 at 9:12
0

If you know the environment that you're going to be running this code in, I think you're much better off just making a call to the system to unzip it for you. It will be way faster than anything that you implement in java.

I wrote the code to extract a zip file with nested directories and it ran slowly and took a lot of CPU. I wound up replacing it with this:

    Runtime.getRuntime().exec(String.format("unzip %s -d %s", archive.getAbsolutePath(), basePath));

That works a lot better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.