简体   繁体   中英

java Extracting Zip file

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

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.

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

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
    }

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.

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.

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