我想用 Java 将未知大小的大流写入 tar 文件。我知道阿帕奇有公共压缩库 http://commons.apache.org/compress/ which 处理 tar 文件 http://commons.apache.org/compress/tar.html,但他们要求我提前知道尺寸。考虑 Apache 自己的example http://commons.apache.org/compress/examples.html:
TarArchiveEntry entry = new TarArchiveEntry(name);
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();
在我写完之后我会知道条目的大小,但之前不会。有没有一种方法可以让我编写内容,然后在完成后将大小写入标题?
如果您只需要添加一个文本文件,那么您可以使用 setSize(long) 方法并设置条目的大小。
其中 data 是您想要压缩的字符串。
long size=data.getByte().length;
TarArchiveEntry entry = new TarArchiveEntry("sample.txt");
entry.setSize(size);
tarOutput.putArchiveEntry(entry);
tarOutput.write(contentOfEntry);
tarOutput.closeArchiveEntry();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)