ZipException: error in opening zip file

rearden

I'm working on a method which will take a zipped file, unzip it, and return a new file/directory containing all the unzipped files. The goal is to then take that directory and extract an excel document from it and then convert it into a Workbook class I built (which is fully unit tested and works fine). The problem is that I'm getting the following exception:

java.util.zip.ZipException: error in opening zip file
at java.util.zip.ZipFile.open(Native Method)
at java.util.zip.ZipFile.<init>(ZipFile.java:215)
at java.util.zip.ZipFile.<init>(ZipFile.java:145)
at java.util.zip.ZipFile.<init>(ZipFile.java:159)
at com.atd.core.datamigrator.BulkImageUpload.createWorkbook(BulkImageUpload.java:54)
at com.atd.core.datamigrator.BulkImageUpload.importImages(BulkImageUpload.java:38)
at com.atd.core.datamigrator.BulkImageUpload.main(BulkImageUpload.java:236) 

Here is my code

private Workbook createWorkbook(File file) {
    File unZipedFile = unZip(file);
    File[] files = unZipedFile.listFiles();
    Workbook wBook = null;

    for (int i = 0; i < files.length; i++) {
        if (files[i].getName().contains(".xls")) {
            try {
                File f = files[i];
                ZipFile zip = new ZipFile(f);
                wBook = new Workbook(zip);
            } catch (IOException e) {
                e.printStackTrace();
            }
            break;
        }
    }
    return wBook;
}

private File unZip(File input) {
    File output = new File("unzippedFile");
    OutputStream out = null;
    try {
        ZipFile zipFile = new ZipFile(input);
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            File entryDestination = new File(output, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            ZipInputStream zis = new ZipInputStream(in);
            out = new FileOutputStream(entryDestination);
            out.write(zis.read());
            out.flush();
            out.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return output;
}

I know this is a problem with the unzip method because when I use File f = new File("some path") instead of using the unzipped file, it works fine.

Also, File I/O was never my strong point, so be nice :)

Jon Skeet

Okay, I now believe that this is the problem:

ZipInputStream zis = new ZipInputStream(in);
out = new FileOutputStream(entryDestination);
out.write(zis.read());
out.flush();
out.close();

You're creating a new file, and writing a single byte to it. That's not going to be a valid Excel file of any description. You're also failing to close streams using finally blocks, but that's a different matter. To copy the contents of one stream to another, you want something like:

byte[] buffer = new byte[8192];
int bytes;
while ((bytes = input.read(buffer)) > 0) {
    output.write(buffer, 0, bytes);
}

That said, you'd be better off using a 3rd party library to hide all of this detail - look at Guava and its ByteStreams and Files classes for example.

It's worth taking a step back and working out why you didn't spot this problem for yourself, by the way. For example, the first thing I'd have done would be to look at the directory where the files were unzipped, and try to open those files. Just seeing a bunch of 1-byte files would be a bit of a giveaway. When trying to diagnose an issue, it's vital that you can split a big problem into small ones, and work out which small one is at fault.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to correct the error 'java.util.zip.ZipException: error in opening zip file' invoking web service on WAS with Portal

From Dev

Android Studio 0.8.7: Error:Cause: error in opening zip file

From Dev

Error java.util.zip.ZipException: invalid entry size while copying image (.png) from 1 zip file to another

From Dev

PHP .zip file download error when opening in windows explorer

From Dev

Error opening zip file or JAR manifest missing : jrebel.jar

From Dev

Error opening zip file or JAR manifest missing : C:\Program

From Dev

Android studio - Failed to complete gradle execution - error in opening zip file

From Dev

Cordova Build Gradle Error while opening/extracting zip file

From Dev

PHP .zip file download error when opening in windows explorer

From Dev

Flyway Ant tries to open .sql file as a zip, fails with ZipException

From Dev

Error ':app:packageAllDebugClassesForMultiDex'. > java.util.zip.ZipException: duplicate entry:

From Dev

Error: java.util.zip.ZipException: duplicate entry

From Dev

java.util.zip.ZipException: duplicate entry error

From Dev

Google App Engine Eclipse Error: Error opening zip file or JAR manifest missing : /Applications/eclipse

From Dev

Google App Engine Eclipse Error: Error opening zip file or JAR manifest missing : /Applications/eclipse

From Dev

Android: ZipFile() java.util.zip.ZipException: File too short to be a zip file: 0

From Dev

SQLiteManager: Error in opening file?

From Dev

Error handling in file opening

From Dev

Opening a file with a name error

From Dev

PHP file opening error

From Dev

PHP file opening error

From Dev

Error, opening json file

From Dev

Espresso-android jar - 'Error in opening zip file' in android studio 8.x

From Dev

Cassandra: Error opening zip file or JAR manifest missing : D:\cassandra\lib\jamm-0.3.2

From Dev

Error opening zip file or JAR manifest missing : /usr/share/java/jayatanaag.jar

From Dev

Running grails from STS shows Error opening zip file or JAR manifest missing

From Dev

Android Studio 2.3 for mac: gradle sync failed, error in opening zip file

From Dev

Error opening file generated by epplus

From Dev

Error when opening saved file

Related Related

  1. 1

    How to correct the error 'java.util.zip.ZipException: error in opening zip file' invoking web service on WAS with Portal

  2. 2

    Android Studio 0.8.7: Error:Cause: error in opening zip file

  3. 3

    Error java.util.zip.ZipException: invalid entry size while copying image (.png) from 1 zip file to another

  4. 4

    PHP .zip file download error when opening in windows explorer

  5. 5

    Error opening zip file or JAR manifest missing : jrebel.jar

  6. 6

    Error opening zip file or JAR manifest missing : C:\Program

  7. 7

    Android studio - Failed to complete gradle execution - error in opening zip file

  8. 8

    Cordova Build Gradle Error while opening/extracting zip file

  9. 9

    PHP .zip file download error when opening in windows explorer

  10. 10

    Flyway Ant tries to open .sql file as a zip, fails with ZipException

  11. 11

    Error ':app:packageAllDebugClassesForMultiDex'. > java.util.zip.ZipException: duplicate entry:

  12. 12

    Error: java.util.zip.ZipException: duplicate entry

  13. 13

    java.util.zip.ZipException: duplicate entry error

  14. 14

    Google App Engine Eclipse Error: Error opening zip file or JAR manifest missing : /Applications/eclipse

  15. 15

    Google App Engine Eclipse Error: Error opening zip file or JAR manifest missing : /Applications/eclipse

  16. 16

    Android: ZipFile() java.util.zip.ZipException: File too short to be a zip file: 0

  17. 17

    SQLiteManager: Error in opening file?

  18. 18

    Error handling in file opening

  19. 19

    Opening a file with a name error

  20. 20

    PHP file opening error

  21. 21

    PHP file opening error

  22. 22

    Error, opening json file

  23. 23

    Espresso-android jar - 'Error in opening zip file' in android studio 8.x

  24. 24

    Cassandra: Error opening zip file or JAR manifest missing : D:\cassandra\lib\jamm-0.3.2

  25. 25

    Error opening zip file or JAR manifest missing : /usr/share/java/jayatanaag.jar

  26. 26

    Running grails from STS shows Error opening zip file or JAR manifest missing

  27. 27

    Android Studio 2.3 for mac: gradle sync failed, error in opening zip file

  28. 28

    Error opening file generated by epplus

  29. 29

    Error when opening saved file

HotTag

Archive