I'd like to write a large stream of unknown size to a tar file in Java. I know that Apache has the commons compress library which handles tar files, but they require me to know the size in advance. Consider Apache's own example:
TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();
I will know the size of the entry after I write it, but not before. Isn't there a way that I can write the content and when I finish the size is then written to the header?
To create an archive with tar, use the '-c' (“create”) option, and specify the name of the archive file to create with the '-f' option. It's common practice to use a name with a '. tar' extension, such as 'my-backup. tar'.
Tar, when it comes to compression has a compression ratio of 50%, which means it compresses efficiently. Drastically reduces the size of packaged files and folders. Tar does not alter the features of files and directories.
Answer: Most Unix software on the net is distributed in the form of a tarball. This just means that all the files have been packed into a tar file, which has been compressed with gzip to save space. The file name thus ends up having extension .
I ended up taking jtar and modifying it so you don't have to specify the size until after you write. It seems there is no package that does this natively... I set up an issue that requests this change. Unfortunately, it required a lot of ugly code and creating my own RandomAccessFileInputStream(), which is unsuitable for uploading a patch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With