13

I would like to unzip recursively some archive .zip. I use java.util.zip and I can't use an other library.

My code :

public static void unzip(String file) {

    try {
        File fSourceZip = new File(file);
        String zipPath = file.substring(0, file.length() - 4);
        File temp = new File(zipPath);
        temp.mkdir();
        System.out.println(zipPath + " created");


        ZipFile zipFile = new ZipFile(fSourceZip);
        Enumeration e = zipFile.entries();

        while (e.hasMoreElements()) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            File destinationFilePath = new File(zipPath, entry.getName());

            destinationFilePath.getParentFile().mkdirs();

            if (entry.isDirectory()) {
                continue;
            } else {
                System.out.println("Extracting " + destinationFilePath);

                BufferedInputStream bis = new BufferedInputStream(
                        zipFile.getInputStream(entry));

                int b;
                byte buffer[] = new byte[1024];


                FileOutputStream fos = new FileOutputStream(
                        destinationFilePath);
                BufferedOutputStream bos = new BufferedOutputStream(fos,
                        1024);

                while ((b = bis.read(buffer, 0, 1024)) != -1) {
                    bos.write(buffer, 0, b);
                }

                bos.flush();
                bos.close();
                bis.close();
            }
            if (entry.getName().endsWith(".zip")) {
                // found a zip file, try to open
                unzip(destinationFilePath.getAbsolutePath());
            }
        }

    } catch (IOException ioe) {
        System.out.println("IOError :" + ioe);
    }
}

But I have some error with some archive :

Exception in thread "main" java.lang.IllegalArgumentException: MALFORMED
    at java.util.zip.ZipCoder.toString(ZipCoder.java:58)
    at java.util.zip.ZipFile.getZipEntry(ZipFile.java:567)
    at java.util.zip.ZipFile.access$900(ZipFile.java:61)
    at java.util.zip.ZipFile$ZipEntryIterator.next(ZipFile.java:525)
    at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:500)
    at java.util.zip.ZipFile$ZipEntryIterator.nextElement(ZipFile.java:481)
    at zip.ReadingArchive.unzip(ReadingArchive.java:36)
    at zip.ReadingArchive.unzip(ReadingArchive.java:82)
    at zip.ReadingArchive.unzip(ReadingArchive.java:82)
    at main.Main.main(Main.java:13)

I have this problem because there is .odp in my archive. How I can say only uses .zip, not other files ? How I can resolve this problem ?

Thanks !

user2998243
  • 181
  • 1
  • 2
  • 12
  • Print the current file name, is there something special(non latin characters)? Try to open the file which causes the error with some other program, maybe it is broken. – Daniel Nov 16 '13 at 00:53
  • how do you know that problem is the .odp file. – Nitin Dandriyal Nov 16 '13 at 02:30
  • Isn't a problem whith a special caractere. I tested with lots of archive file, and each time he throws the exception is when it tries to read the .odp file. – user2998243 Nov 16 '13 at 08:08

1 Answers1

24

I've just fixed it by specifying alternative (non UTF-8) charset:

Charset CP866 = Charset.forName("CP866");
ZipFile zipFile = new ZipFile(zipArchive, CP866);

In your case, you need to specify another charset. Try, CP437, for instance.

Slava Semushin
  • 14,904
  • 7
  • 53
  • 69