java.util.zip 文件名/目录名中的句点有问题吗?

发布于 2024-10-14 15:04:19 字数 1579 浏览 5 评论 0原文

我想解压 iPhone 应用程序 .ipa 文件。 这实际上是正常解压的zip文件。 但其中实际的应用程序文件是一个以 .app 结尾的文件夹(因为所有 Mac 应用程序实际上都是以 .app 结尾的文件夹)。 现在这个时期似乎是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());
        }
    }
}

这是我的输出:

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

有人知道如何处理这些时期吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

平生欢 2024-10-21 15:04:19

首先,您应该使用 mkdirs(),而不是 mkdir()。

其次,zip 文件并不总是包含所有目录条目(或按正确的顺序排列它们)。最佳实践是在代码的两个分支中创建目录,因此添加:(

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

您应该添加一些检查以确保 getParent() 不为空等)。

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).

紫竹語嫣☆ 2024-10-21 15:04:19

我不认为时期是问题。查看您尝试输出的文件的绝对路径,并确保它指向正确的位置。

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.

盛夏已如深秋| 2024-10-21 15:04:19
if (entry.isDirectory()) {
            File file = new File(path + entry.getName());
....
} else {
            File f = new File(entry.getName());
....

创建目录时,传递的文件路径是path +entry.getName()
但在创建文件时,传递的文件路径是entry.getName()

将文件路径更改为path +entry.getName() 后,代码适用于句点文件名和普通文件名。 :)

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. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文