如何将一个巨大的 zip 文件分割成多个卷?
当我通过 java.util.zip.* 创建 zip 存档时,有没有办法将生成的存档拆分为多个卷?
假设我的整个存档的文件大小
为24 MB
,我想将其拆分为 3 个文件,每个文件限制为 10 MB。
有没有具有此功能的 zip API? 或者还有其他好的方法来实现这一目标吗?
谢谢 托尔斯滕
When I create a zip Archive via java.util.zip.*
, is there a way to split the resulting archive in multiple volumes?
Let's say my overall archive has a filesize
of 24 MB
and I want to split it into 3 files on a limit of 10 MB per file.
Is there a zip API which has this feature? Or any other nice ways to achieve this?
Thanks
Thollsten
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
检查: http:// saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=38&t=004618
我不知道有任何公共 API 可以帮助您做到这一点。
(尽管如果您不想以编程方式执行此操作,则有像 WinSplitter 这样的实用程序可以执行此操作)
我还没有尝试过,但是,使用 ZippedInput/OutputStream 时的每个 ZipEntry 都有压缩的大小。 您可以在创建压缩文件时粗略估计其大小。 如果您需要 2MB 的压缩文件,那么您可以在条目的累积大小达到 1.9MB 后停止写入文件,为清单文件和其他 zip 文件特定元素占用 0.1MB。
简而言之,您可以在 ZippedInputStream 上编写一个包装器,如下所示:
上面的程序只是方法的提示,无论如何都不是最终的解决方案。
Check: http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=38&t=004618
I am not aware of any public API that will help you do that.
(Although if you do not want to do it programatically, there are utilities like WinSplitter that will do it)
I have not tried it but, every ZipEntry while using ZippedInput/OutputStream has a compressed size. You may get a rough estimate of the size of the zipped file while creating it. If you need 2MB of zipped files, then you can stop writing to a file after the cumulative size of entries become 1.9MB, taking .1MB for Manifest file and other zip file specific elements.
So, in a nutshell, you can write a wrapper over the ZippedInputStream as follows:
The above program is just a hint of the approach and not a final solution by any means.
如果目标是让输出与 pkzip 和 winzip 兼容,我不知道有任何开源库可以做到这一点。 我们对我们的一个应用程序有类似的要求,我最终编写了我们自己的实现(与 zip 标准兼容)。 如果我记得的话,对我们来说最困难的事情是我们必须动态生成单独的文件(大多数 zip 实用程序的工作方式是创建大 zip 文件,然后返回并稍后分割它 - 这要容易得多花了大约一天的时间来编写,并花了两天的时间来调试。
zip 标准解释了文件格式的样子,如果您不害怕卷起袖子,那么这绝对是可行的。自己实现一个 zip 文件生成器,但您可以使用 Java 的 Deflator 类来生成压缩数据的段流。您必须自己生成文件和节标头,但它们只是字节 - 一旦您深入研究,这没什么难的。这
是 zip 规范 - K 部分包含您具体要查找的信息,但你还需要阅读 A、B、C 和 F 如果你正在处理非常大的文件(我们是),你还必须了解 Zip64 的内容 - 但对于 24 MB,你是。美好的。
如果您想深入尝试 - 如果您遇到问题,请回帖,我会看看是否可以提供一些指导。
If the goal is to have the output be compatible with pkzip and winzip, I'm not aware of any open source libraries that do this. We had a similar requirement for one of our apps, and I wound up writing our own implementation (compatible with the zip standard). If I recall, the hardest thing for us was that we had to generate the individual files on the fly (the way that most zip utilities work is they create the big zip file, then go back and split it later - that's a lot easier to implement. Took about a day to write and 2 days to debug.
The zip standard explains what the file format has to look like. If you aren't afraid of rolling up your sleeves a bit, this is definitely doable. You do have to implement a zip file generator yourself, but you can use Java's Deflator class to generate the segment streams for the compressed data. You'll have to generate the file and section headers yourself, but they are just bytes - nothing too hard once you dive in.
Here's the zip specification - section K has the info you are looking for specifically, but you'll need to read A, B, C and F as well. If you are dealing with really big files (We were), you'll have to get into the Zip64 stuff as well - but for 24 MB, you are fine.
If you want to dive in and try it - if you run into questions, post back and I'll see if I can provide some pointers.
就其价值而言,我喜欢在任何地方使用try-with-resources。 如果您喜欢这种设计模式,那么您会喜欢这个。 此外,如果条目大于所需的部件尺寸,这还解决了空部件的问题。 在最坏的情况下,您至少将拥有与条目一样多的部分。
在:
输出:
注意:我正在使用日志记录和 Apache Commons FilenameUtils,但请随意使用工具包中的内容。
For what it's worth, I like to use try-with-resources everywhere. If you are into that design pattern, then you will like this. Also, this solves the problem of empty parts if the entries are larger than the desired part size. You will at least have as many parts as entries in the worst case.
In:
Out:
Note: I'm using logging and Apache Commons FilenameUtils, but feel free to use what you have in your toolkit.
下面的代码是我的解决方案,根据所需的大小将目录结构中的 zip 文件拆分为块。 我发现以前的答案很有用,所以想用类似但更简洁的方法做出贡献。 该代码适合我的特定需求,并且我相信还有改进的空间。
Below code is my solution to split zip file in directory structure to chunks based on desired size. I found the previous answers useful so, wanted to contribute with similar but little more neat approach. This code is working for me for my specific needs, and I believe there is room for improvement.
这是我的解决方案:
控制台输出:
Here's my solution:
And the console output: