tzippy
tzippy

Reputation: 6638

java.util.zip has problems with periods in filenames / directorynames?

I want to unzip an iPhone app .ipa file. This is actually zip file that extracts normally. But the actual app file in it is a folder with the ending .app ( as all mac applications are actually folders with the ending .app). Now the period seems to be a problem for java.util.zip.

public static void main(String[] args) throws IOException {
    ZipFile zipFile = new ZipFile("file.zip");
    String path = "";

    Enumeration files = zipFile.entries();

    while (files.hasMoreElements()) {
        ZipEntry entry = (ZipEntry) files.nextElement();
        if (entry.isDirectory()) {
            File file = new File(path + entry.getName());
            file.mkdir();
            System.out.println("Create dir " + entry.getName());
        } else {
            File f = new File(entry.getName());
            FileOutputStream fos = new FileOutputStream(f); //EXception occurs here
            InputStream is = zipFile.getInputStream(entry);
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = is.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            fos.close();
            System.out.println("Create File " + entry.getName());
        }
    }
}

This is my output:

Exception in thread "main" java.io.FileNotFoundException: Payload/SMA Jobs.app/06-magnifying-glass.png (No such file or directory)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
    at Main.main(Main.java:27)
    enter code here

Anyone knows how to handle those periods?

Upvotes: 1

Views: 1022

Answers (3)

Karan
Karan

Reputation: 1

if (entry.isDirectory()) {
            File file = new File(path + entry.getName());
....
} else {
            File f = new File(entry.getName());
....

While creating directory, file path passed is path + entry.getName() but while creating file, file path passed is entry.getName()

After changing file path to path + entry.getName(), code works for period file names and normal file names. :)

Upvotes: 0

jtahlborn
jtahlborn

Reputation: 53694

First of all, you should use mkdirs(), not mkdir().

second, zip files don't always include all the directory entries (or have them in the right order). the best practice is to make the directories in both branches of the code, so add:

    } else {
        File f = new File(entry.getName());
        f.getParent().mkdirs();

(you should add some checking to make sure getParent() is not null, etc).

Upvotes: 2

jzd
jzd

Reputation: 23629

I don't think the period is the problem. Look at the absolute path of the file you are trying to output and make sure it is pointing to the correct place.

Upvotes: 0

Related Questions