Write Tar File of Unknown (Large) Size in Java

前端 未结 2 1775
无人及你
无人及你 2021-02-19 04:34

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 kno

相关标签:
2条回答
  • 2021-02-19 04:46

    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();
    
    0 讨论(0)
  • 2021-02-19 04:47

    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.

    0 讨论(0)
提交回复
热议问题