【发布时间】:2012-04-04 07:34:40
【问题描述】:
我需要将配置文件添加到现有的 tar 文件中。我正在使用 apache.commons.compress 库。以下代码 sn -p 正确添加了条目,但会覆盖 tar 文件的现有条目。
public static void injectFileToTar () throws IOException, ArchiveException {
String agentSourceFilePath = "C:\\Work\\tar.gz\\";
String fileToBeAdded = "activeSensor.cfg";
String unzippedFileName = "sample.tar";
File f2 = new File(agentSourceFilePath+unzippedFileName); // Refers to the .tar file
File f3 = new File(agentSourceFilePath+fileToBeAdded); // The new entry to be added to the .tar file
// Injecting an entry in the tar
OutputStream tarOut = new FileOutputStream(f2);
TarArchiveOutputStream aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream("tar", tarOut);
TarArchiveEntry entry = new TarArchiveEntry(fileToBeAdded);
entry.setMode(0100000);
entry.setSize(f3.length());
aos.putArchiveEntry(entry);
FileInputStream fis = new FileInputStream(f3);
IOUtils.copy(fis, aos);
fis.close();
aos.closeArchiveEntry();
aos.finish();
aos.close();
tarOut.close();
}
在检查 tar 时,仅找到“activeSensor.cfg”文件,并且发现 tar 的初始内容缺失。 “模式”是否设置不正确?
【问题讨论】:
标签: java