Android 将文件附加到 zip 文件而无需重写整个 zip 文件?
如何将文件附加到现有 zip 文件?我已经有了可以创建 zip 文件的代码,除了一个大问题之外,它工作得很好。现在的工作方式是,用户拍摄一堆照片,最后,所有照片都会添加到一个 zip 文件中,如果拍摄足够多的照片,这可能需要相当长的时间。 :-( 所以我在想,我有一个非常好的和有效的解决方案。在拍摄照片时,我会在拍摄后立即将每张新照片添加到 zip 文件中。然后当他们完成拍照时,完成 zip 文件,使其可用并导出:-)
问题是,我无法将文件添加到现有 zip 文件中。 :-( 这是我到目前为止所得到的。另外,请记住,这只是一个概念证明,我确实明白为 for 循环的每次迭代重新初始化所有内容是非常愚蠢的。循环的每次迭代都是应该代表正在添加的另一个文件,这很可能是很长一段时间后,甚至可能是一个小时后,这就是为什么我每次迭代都会重置所有内容,因为如果我能让这个工作正常,应用程序将在添加文件之间关闭。 ,那么我实际上会放弃 for 循环并将这段代码放入一个每次拍摄照片时都会调用的函数中:-)
try {
for(int i=0; i < _files.size(); i++) {
//beginning of initial setup stuff
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile,false);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
out.setLevel(0); //I added this because it makes it not compress the data
//at all and I hoped that it would allow the zip to be appended to
//end of initial setup stuff
//beginning of old for loop
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
//end of for old loop
//beginning of finishing stuff
out.close();
//end of finishing stuff
}
} catch(Exception e) {
Log.e("ZipCreation", "Error writing zip", e);
e.printStackTrace();
}
另外,我已经尝试过,
FileOutputStream dest = new FileOutputStream(_zipFile,true);
如果您注意到,我将append设置为true,这实际上会将数据附加到现有文件中。有趣的是,它实际上确实将数据附加到原始文件中,但是,在我的计算机上提取文件后,最后写入的文件就是提取的所有文件,这很糟糕。 :-( 那么有没有什么方法可以开始编写 zip 文件,然后添加到它并完成 zip 文件?我什至考虑过可能采用 ZipOutputStream 并修改它以适合我需要的模型从逻辑上来说应该是可能的?:-)
提前感谢您的帮助! :-D-
贾里德
How can I append files to an existing zip file? I already have the code that can create a zip file and it works great except for one big problem. The way it works now, the user takes a bunch of pictures, and at the end, all the pictures get added to a zip file, which can take quite a while if you take enough pictures. :-( So I'm thinking, I have a very good and efficient solution. As the pictures are taken, I will simply add the each new picture to the zip file right after it's taken. Then when they're done taking pictures, finish up the zip file so it's usable and export it. :-)
The problem is, I can not get it to add files to an existing zip file. :-( Here's what I have so far. Also, please keep in mind, this is just a proof of concept, I do understand that re-initializing everything for every iteration of the for loop is very dumb. Each iteration of the loop is supposed to represent another file being added which will most likely be a long time later, maybe even an hour later, which is why I have everything resetting each iteration, because the app will be shut down between adding files. If I can get this working, then I will actually ditch the for loop and put this code into a function that gets called every time a picture gets taken. :-)
try {
for(int i=0; i < _files.size(); i++) {
//beginning of initial setup stuff
BufferedInputStream origin = null;
FileOutputStream dest = new FileOutputStream(_zipFile,false);
ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest));
byte data[] = new byte[BUFFER];
out.setLevel(0); //I added this because it makes it not compress the data
//at all and I hoped that it would allow the zip to be appended to
//end of initial setup stuff
//beginning of old for loop
Log.v("Compress", "Adding: " + _files[i]);
FileInputStream fi = new FileInputStream(_files[i]);
origin = new BufferedInputStream(fi, BUFFER);
ZipEntry entry = new ZipEntry(_files[i].substring(_files[i].lastIndexOf("/") + 1));
out.putNextEntry(entry);
int count;
while ((count = origin.read(data, 0, BUFFER)) != -1) {
out.write(data, 0, count);
}
origin.close();
//end of for old loop
//beginning of finishing stuff
out.close();
//end of finishing stuff
}
} catch(Exception e) {
Log.e("ZipCreation", "Error writing zip", e);
e.printStackTrace();
}
Also, I have experimented around with
FileOutputStream dest = new FileOutputStream(_zipFile,true);
If you notice, I set append to true, which will actually append the data to an existing file. And what's interesting is, it actually does append the data to the original file, however, after the file gets extracted on my computer, the last file written is all that gets extracted, which is bad. :-( So is there some way to start writing a zip file, and then later, add on to it, and finish up the zip file? I've even thought about possibly taking ZipOutputStream and modifying it to fit this model that I need. It should logically be possible somehow? :-)
Thanks in advance for the help! :-D
-Jared
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,感谢您的所有建议,但我能够让它像我想要的那样工作......它可以完成,您可以在关闭文件后添加文件,只要您保存您的位置! :-D
以下是我如何让它正常工作:
另外,请记住,这不是我必须执行的唯一代码。我还必须制作自己的 ZipOutputStream 副本,以便可以公开我在 ZipOutputStreamNew 类中创建的以下函数......
以及
在大多数情况下,所有这一切都会像正常一样开始编写,然后,它不会像平常一样关闭,而是备份这两个变量,然后在下一次迭代中,它只恢复这些变量。
如果您对这一切有任何疑问,请告诉我,但我知道它会起作用,它所要做的就是保存它所在的位置。 :-D
再次感谢大家的帮助! :-)
应 Raj 的要求,这里是 ZipOutputStreamNew 的源代码:
Ok, thanks for all your suggestions, but I was able to get it working like I wanted.... it CAN be done, you CAN add files after closing the file, as long as you save your place!!! :-D
Here's how I was able to get it going working:
Also, keep in mind, this is not the only code I had to do. I also had to make my own copy of ZipOutputStream so that I could expose the following functions that I created within my ZipOutputStreamNew class....
as well as
For the most part, all this does, is it starts writing like normal, then, instead of closing like normal, it backs up those two variables, and then for the next iteration, it restores just those variables.
Let me know if you have any questions about all this, but I knew it would work, all it has to do is save where it was. :-D
Thanks again for all the help everybody! :-)
At Raj's request, here is the source code for ZipOutputStreamNew:
我相信目前的 API 还无法做到这一点。
您可以将数据附加到任何文件,但这并不意味着您最终会得到正确的文件格式。
.zip
文件与.tar
文件不同,压缩要求对文件的处理(文件位置、EOF 等)施加限制。如果您考虑文件格式的结构(取自维基百科此处 )你就会明白为什么仅仅附加不起作用。有一个名为 TrueZip 的库可以工作,尽管我不知道它是否支持 android。看看另一个类似问题中的答案:
使用 Java 将文件附加到 zip 文件 。
另外,作为解决方法,您可以创建单独的
.zip
文件并将其附加为 tarball (此处的文件格式)。压缩可能稍微差一些,但就时间效率而言会好得多。根据评论(和可能的解决方案)进行更新
您可以单独添加每个
ZipEntry
并让ZipOutputStream
对象保持打开状态,只要您仍在拍照。不过,我可以看到这种方法的风险,因为应用程序在拍照时出现任何问题(强行关闭、电池耗尽等)都可能导致整个文件无法使用。您需要确保使用正确的 try/catch/finally 块来关闭文件,并在发生onClose()
和等事件时调用
,但想法如下:closeZip()
onDestroy()I believe it can't be done right now with the current API.
You can append data to any file, but that does not mean that you will end up with the right file format. A
.zip
file is not like a.tar
file, and the compression requires imposes restrictions to the handling of the files (file positions, EOF, etc.). If you consider the structure of the file format (taken from wikipedia here) you will understand why just appending does not work.There is a library called TrueZip that could work, although I do not know if it supports android. Take a look at this answer in another similar question:
Appending files to a zip file with Java .
Also, as a workaround, you could create individual
.zip
files and append them as a tarball (file format here). Compression might be slighty worst, but it would be much better in terms of time efficiency.Update based on the comments (and possible solution)
You could separate the addition to each
ZipEntry
and leave theZipOutputStream
object open as long as you are still taking pictures. I can see risks with that approach, though, as any problem with the app while still taking pictures (a force close, run out of battery, etc) may render the whole file unusable. You will need to make sure to use the right try/catch/finally blocks to close the file and callcloseZip()
upon events such asonClose()
andonDestroy()
, but the idea would be the following: