如何创建具有最大文件大小限制的java zip存档 [英] how to create java zip archives with a max file size limit

查看:791
本文介绍了如何创建具有最大文件大小限制的java zip存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在java中编写一个算法(对于Android应用程序)来读取包含更多文件夹的文件夹,每个文件夹包含图像和音频文件,所以结构如下: mainDir / categorySubfolder / myFile1 .jpg

I need to write an algorithm in java (for an android app) to read a folder containing more folders and each of those containing images and audio files so the structure is this: mainDir/categorySubfolder/myFile1.jpg

我的问题是我需要将存档的大小限制为16mb,并且在运行时,根据需要创建尽可能多的存档包含我的主 mainDir 文件夹中的所有文件。

My problem is that I need to limit the size of the archive to 16mb and at runtime, create as many archives as needed to contain all my files from my main mainDir folder.

我在网上尝试了几个例子,我阅读了java文档,但是我无法理解并按照我需要的方式将它们整合在一起。有人之前做过这个或有链接或示例吗?

I tried several examples from the net and I read the java documentation but I can't manage to understand and put it all together the way I need it. Has someone done this before or has a link or an example for me?

我用递归方法解决了文件的读取问题,但我无法编写zip创建的逻辑。

I resolved the reading of the files with a recursive method but I can't write the logic for the zip creation.

我愿意接受建议或更好的工作示例。

I'm open for suggestions or better a working example.

推荐答案

zip4j 是一个很棒的库,可以创建多部分zip文件。

zip4j is a great library that can create multi-part zip files.

net.lingala.zip4j.core.ZipFile zipFile = new ZipFile("out.zip");
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
zipFile.createZipFileFromFolder("path/to/source/dir", parameters, true, maximum size);

您可以在他们的网站上找到更多示例。

You can find more examples on their web-site.

这篇关于如何创建具有最大文件大小限制的java zip存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆