使用 Java 压缩文件:有限制吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6738773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 17:04:48  来源:igfitidea点击:

Zip files with Java: Is there a limit?

javazipbackupgziptar

提问by caarlos0

I'm creating a backup routine for my application with Java. However, when the zip file is over 4GB, or has more than 65,000 files (approximately), the zip file is corrupted.

我正在使用 Java 为我的应用程序创建一个备份例程。但是,当 zip 文件超过 4GB 或包含超过 65,000 个文件(大约)时,zip 文件已损坏。

I'm also testing the Apache Commons Compression for compacting to tar.gz, but it has file name limit of 100 characters. I was wanting to test this API compressing to zip, but I wonder what exactly is the problem with the java zip.

我也在测试 Apache Commons Compression 以压缩到 tar.gz,但它的文件名限制为 100 个字符。我想测试这个压缩为 zip 的 API,但我想知道 java zip 到底有什么问题。

So, the real question is: I'm doing something wrong, it is limit of Java Zip implementation, or is the limit for the Zip format itself?

所以,真正的问题是:我做错了什么,是 Java Zip 实现的限制,还是 Zip 格式本身的限制?

Thanks.

谢谢。

回答by MRAB

Quoting from Wikipedia:

引自维基百科

The original ZIP format had a 4 GiB limit on various things (uncompressed size of a file, compressed size of a file and total size of the archive), as well as a limit of 65535 entries in a ZIP archive.

原始 ZIP 格式对各种内容(文件的未压缩大小、文件的压缩大小和存档的总大小)有 4 GiB 的限制,并且 ZIP 存档中的条目限制为 65535。

and about ZIP64:

关于 ZIP64:

Java's built-in java.util.zip does not support it as of September 2010, but it has been added to OpenJDK and is planned for inclusion in Java 7.

Java 的内置 java.util.zip 于 2010 年 9 月不支持它,但它已添加到 OpenJDK 并计划包含在 Java 7 中。

回答by NPE

It's a bug that's reported fixed in Java 7: http://bugs.sun.com/view_bug.do?bug_id=4681995

这是一个在 Java 7 中被报告修复的错误:http: //bugs.sun.com/view_bug.do?bug_id=4681995

One of the commenters on that tickets mentions TrueZIPas a workaround.

该票的一位评论者提到TrueZIP作为一种解决方法。

回答by mikera

There's a 4GB file size limit on standard Zip files.

标准 Zip 文件有 4GB 的文件大小限制。

See the wikipedia entry on zip filesfor some more info...... apparently you can get much much larger files if you use ZIP64 format.

有关更多信息,请参阅有关 zip 文件维基百科条目……显然,如果您使用 ZIP64 格式,您可以获得更大的文件。

p.s. if you find yourself trying to back up more than 4GB of data at a time, perhaps you should be considering a different approach? Maybe something that takes a versioned filesystem snapshot would be more appropriate?

ps 如果您发现自己尝试一次备份超过 4GB 的数据,也许您应该考虑采用不同的方法?也许需要版本化文件系统快照的东西更合适?

回答by user1712200

Yes, there is a limit. If you go through the entries like

是的,有一个限制。如果你通过像这样的条目

        int count  = 0;
        for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements(); ) {
            final ZipEntry ze = e.nextElement();
            count++;
        }

it will count up until 65535 entries and no more.

它将一直计数到 65535 个条目,并且不会更多。