Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write Tar File of Unknown (Large) Size in Java

Tags:

java

tar

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?

like image 968
Mike Goodspeed Avatar asked May 08 '12 14:05

Mike Goodspeed


People also ask

How do I create an archive tar?

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'.

Does tar change file size?

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.

Is tarball compressed?

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 .


1 Answers

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.

like image 114
Mike Goodspeed Avatar answered Sep 18 '22 16:09

Mike Goodspeed