How do I append files to a .tar archive in Java?

*爱你&永不变心* 提交于 2019-12-20 04:37:17

问题


I would like to create a tar archive in Java. I have files which are constantly being created and I'd like a worker thread to take a reference to those files from a queue and copy them into the archive.

I tried using Apache Compression library's TarArchiveOutputStream to do this, but I do not wish to keep the archive open for the entire duration of the program (since unless i finalize the archive, it can become corrupted - so i'd rather append to it in batches), and I haven't found a good way to append to an existing tar archive with their library (They do have the "ChangeSetPerformer" class, but it basically just creates a new tar and needs to copy the old one entirely, which isn't good for me, performance wise).

I also need the library to not have a low limit for the size of the archive (i.e. 4g or so is not enough), and i'd rather avoid having to actually compress the archive.

Any suggestions would be greatly appreciated! thank you.


回答1:


You run here in a limitation of tar: http://en.wikipedia.org/wiki/Tar_(file_format)#Random_access

because of that it is hard to add or remove single files without copying the whole archive.




回答2:


I use a library called Java Tar: http://www.trustice.com/java/tar/

It's worked for me. In that package, look for:

http://www.gjt.org/javadoc/com/ice/tar/TarArchive.html#writeEntry(com.ice.tar.TarEntry, boolean)

Which lets you add an entry to the file without using a stream at the user level. I don't know about file size - but it would be a simple matter to test this aspect.



来源:https://stackoverflow.com/questions/9770492/how-do-i-append-files-to-a-tar-archive-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!