Write Tar File of Unknown (Large) Size in Java
Asked Answered
A

2

15

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?

Alternation answered 8/5, 2012 at 14:52 Comment(4)
Check out TrueZIP, which supports tar files nicely.Scarrow
@MattBall From the documentation: Because the TAR file format needs to know each entry's length in advance, entries from an unknown source are actually written to temp files and copied to the underlying TarArchiveOutputStream upon a call to their OutputStream.close() method. This is undesirable for a large file.Alternation
Yup. AFAIK, there really isn't any other way around it. You've got to know the size in advance. If that's such a problem, then use something more modern than TARs.Scarrow
If you're in the position to choose, using ZIP rather than TAR might be a feasible way. That's what I did ;)Sims
A
1

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.

Alternation answered 9/5, 2012 at 13:4 Comment(2)
Nope, sorry about that.Alternation
Thanks for the answer, I ended up using zip instead; worked for me and was hassle-free ;)Sims
E
2

If you have only one text file to be added then you can use setSize(long) method and set the size of the entry.

where data is string which you want to be tarred.

long size=data.getByte().length;
TarArchiveEntry entry = new TarArchiveEntry("sample.txt");
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();
Engobe answered 13/9, 2016 at 6:48 Comment(0)
A
1

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.

Alternation answered 9/5, 2012 at 13:4 Comment(2)
Nope, sorry about that.Alternation
Thanks for the answer, I ended up using zip instead; worked for me and was hassle-free ;)Sims

© 2022 - 2024 — McMap. All rights reserved.