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?

Mike Goodspeed
  • 213
  • 3
  • 10
  • Check out [TrueZIP](http://truezip.java.net/), which [supports tar files nicely](http://truezip.java.net/truezip-driver/truezip-driver-tar/index.html). – Matt Ball May 08 '12 at 15:04
  • 1
    @MattBall From [the documentation](http://truezip.java.net/truezip-driver/truezip-driver-tar/apidocs/de/schlichtherle/truezip/fs/archive/tar/TarOutputShop.html): 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. – Mike Goodspeed May 08 '12 at 15:09
  • 1
    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. – Matt Ball May 08 '12 at 15:31
  • If you're in the position to choose, using ZIP rather than TAR might be a feasible way. That's what I did ;) – Mene Jul 01 '16 at 20:41

2 Answers2

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();
Kumar Utkarsh
  • 161
  • 1
  • 8
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.

Mike Goodspeed
  • 213
  • 3
  • 10