Mac 上的 java.util.zip.* 问题(提取)

发布于 2024-10-22 04:25:36 字数 2999 浏览 1 评论 0 原文

我编写了一个程序,并创建了文档(条目)的导出功能。 过程: 用户收藏了文档。存在2-3种策略 (BibTex、RIS、HTML)用户可以选择导出他的文档。 对于每个策略,都会生成一个新的 .zip 文件,其中包含所有文档。 创建的 .zip 存档将通过电子邮件发送给用户。

对我来说(Windows)它效果很好。我可以毫无问题地提取这些档案。 但是我的一个使用 Mac 的朋友在提取它们时出现错误,我不知道为什么。

这里是重要的代码:

for ( String strategy : strategies ) {
// Coderedundanz
// Jede Strategie benötigt eigene Parameter
if (strategy.equals("BibTex")) {
    _zipName = "ezdl_export_bibtex";
    _fileExtension = ".bib";
    _strategy = csf.bibTex;
}
else if (strategy.equals("RIS")) {
    _zipName = "ezdl_export_ris";
    _fileExtension = ".ris";
    _strategy = csf.ris;
}
else if (strategy.equals("HTML")) {
    _zipName = "ezdl_export_html";
    _fileExtension = ".html";
    _strategy = csf.html;
}
else {
    _zipName = _zipExtension = "";
    _fileExtension = "";
    _strategy = null;
}

// Gibt es eine korrekte Strategie?
if ( !_zipName.equals("") && !_fileExtension.equals("") && _strategy != null) {
    // 1. .zip Datei generieren
    // 2. Für jedes TextDocument eine eigene Datei erstellen
    // 3. Datei in die .zip Datei einfügen
    // 4. .zip Datei schließen und in die E-Mail hinzufügen
    File file = File.createTempFile(_zipName + _zipExtension, ".tmp");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
    out.setLevel(6);
    for ( TextDocument document : documents ) {
        out.putNextEntry(new ZipEntry( document.getOid() + _fileExtension));

        String temp = _strategy.print ( (TextDocument) document).asString().toString();

        out.write( temp.getBytes() );
        out.closeEntry();
    }

    out.finish();
    out.close();

    PreencodedMimeBodyPart part_x = new PreencodedMimeBodyPart("base64");
    part_x.setFileName(_zipName + _zipExtension);
    part_x.setContent(new String(Base64Coder.encode( getBytesFromFile (file))), "text/plain");
    multi.addBodyPart(part_x);

    if (file.exists())
        file.delete();

您会看到为每个策略创建了自己的存档。 程序循环遍历文档(TextDocument) 使用 _strategy.print 你会得到一个字符串作为输出。

正如我所说......对我来说它工作得很好,但在 Mac 上却不行。 有什么区别吗?我猜..zip 是 .zip。 或者我应该为 Mac 创建 tarball (.tar.gz)?

编辑:

serena:tmp3 alex$ unzip ezdl_export_bibtex.zip 
Archive:  ezdl_export_bibtex.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
note:  ezdl_export_bibtex.zip may be a plain executable, not an archive
unzip:  cannot find zipfile directory in one of ezdl_export_bibtex.zip or
        ezdl_export_bibtex.zip.zip, and cannot find 

这是一个屏幕:http://img3.imageshack.us /i/ziperror.png。它显示错误:“无法取消存档 - 错误 - 1 - 不允许操作”

我还将代码更改为:

out.write( temp.getBytes() );
out.flush();
out.closeEntry();

但仍然是同样的问题。

I made a program and I created an export function for documents (entries).
The procedure: The user has favorited documents. There exist 2-3 strategies
(BibTex,RIS,HTML) a user can choose to export his documents.
For each strategy a new .zip file is being generated with all documents inside.
The created .zip archives are sent to the user via email.

For me ( Windows ) it works great. I can extract those archives without any problems.
But a friend of my, who is using Mac, gets errors while extracting them and I do not know why.

Here the important code:

for ( String strategy : strategies ) {
// Coderedundanz
// Jede Strategie benötigt eigene Parameter
if (strategy.equals("BibTex")) {
    _zipName = "ezdl_export_bibtex";
    _fileExtension = ".bib";
    _strategy = csf.bibTex;
}
else if (strategy.equals("RIS")) {
    _zipName = "ezdl_export_ris";
    _fileExtension = ".ris";
    _strategy = csf.ris;
}
else if (strategy.equals("HTML")) {
    _zipName = "ezdl_export_html";
    _fileExtension = ".html";
    _strategy = csf.html;
}
else {
    _zipName = _zipExtension = "";
    _fileExtension = "";
    _strategy = null;
}

// Gibt es eine korrekte Strategie?
if ( !_zipName.equals("") && !_fileExtension.equals("") && _strategy != null) {
    // 1. .zip Datei generieren
    // 2. Für jedes TextDocument eine eigene Datei erstellen
    // 3. Datei in die .zip Datei einfügen
    // 4. .zip Datei schließen und in die E-Mail hinzufügen
    File file = File.createTempFile(_zipName + _zipExtension, ".tmp");
    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file));
    out.setLevel(6);
    for ( TextDocument document : documents ) {
        out.putNextEntry(new ZipEntry( document.getOid() + _fileExtension));

        String temp = _strategy.print ( (TextDocument) document).asString().toString();

        out.write( temp.getBytes() );
        out.closeEntry();
    }

    out.finish();
    out.close();

    PreencodedMimeBodyPart part_x = new PreencodedMimeBodyPart("base64");
    part_x.setFileName(_zipName + _zipExtension);
    part_x.setContent(new String(Base64Coder.encode( getBytesFromFile (file))), "text/plain");
    multi.addBodyPart(part_x);

    if (file.exists())
        file.delete();

You see for each strategy an own archive is being created.
The program loops through the documents (TextDocument)
and with _strategy.print you get a String as output.

As I said.. for me it works great, but not on Mac.
Are there any differences? I guess.. .zip is .zip.
Or should I create tarballs (.tar.gz) for Mac?

EDIT:

serena:tmp3 alex$ unzip ezdl_export_bibtex.zip 
Archive:  ezdl_export_bibtex.zip
  End-of-central-directory signature not found.  Either this file is not
  a zipfile, or it constitutes one disk of a multi-part archive.  In the
  latter case the central directory and zipfile comment will be found on
  the last disk(s) of this archive.
note:  ezdl_export_bibtex.zip may be a plain executable, not an archive
unzip:  cannot find zipfile directory in one of ezdl_export_bibtex.zip or
        ezdl_export_bibtex.zip.zip, and cannot find 

Here is a screen: http://img3.imageshack.us/i/ziperror.png. It shows the error: "Unable to unarchive - Error - 1 - Operation not permitted"

I also changed my code to:

out.write( temp.getBytes() );
out.flush();
out.closeEntry();

But still the same problem.

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

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

发布评论

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

评论(2

メ斷腸人バ 2024-10-29 04:25:36

尽管它不能直接解决您的问题,但您可以使用 zip 文件的完整性rel="nofollow noreferrer">-t 命令行选项。

$ unzip -t java-puzzlers.zip | tail -1
No errors detected in compressed data of java-puzzlers.zip.

此外,您可以检查父目录的权限,沿着路径向上移动,直到发现问题。

$ ls -ld ..
drwxr-xr-x@ 26 trashgod  staff  884 Jan 17  2010 ..
$ ls -ld ../..
drwx------+ 23 trashgod  staff  782 Dec 17 17:15 ../..

附录:如果它“与编码有关”,我总是从 Joel Spolsky 经常引用的 开始关于该主题的文章。这个答案也可能有帮助。

Although it doesn't address your problem directly, you can verify the integrity of the zip file using the -t option on the command line.

$ unzip -t java-puzzlers.zip | tail -1
No errors detected in compressed data of java-puzzlers.zip.

In addition, you can examine the parent directory's permissions, walking up the path until you see a problem.

$ ls -ld ..
drwxr-xr-x@ 26 trashgod  staff  884 Jan 17  2010 ..
$ ls -ld ../..
drwx------+ 23 trashgod  staff  782 Dec 17 17:15 ../..

Addendum: If it "has something to do with encoding," I always start with Joel Spolsky's oft-cited article on the subject. This answer may be helpful, too.

聊慰 2024-10-29 04:25:36

尝试在 closeEntry() 调用之前在输出流上调用 flush()

Try calling flush() on the output stream prior to the closeEntry() call.

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