我需要将配置文件添加到现有tar文件。我正在使用apache.commons.compress库。以下代码片段正确添加条目,但会覆盖tar文件的现有条目。向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();
}
在检查焦油,只有“activeSensor.cfg”的文件被发现,焦油的初始内容不翼而飞。 “模式”设置不正确?
@biggusjimmusThanks – 2012-04-04 08:32:21
谢谢。我做了上述改变。 tar内容被保留,但新条目(activeSensor.cfg)不会被添加到焦油中。我是否将传递给TarArchiveEntry构造函数的路径或参数搞乱了? – 2012-04-04 08:44:51
在我去这里搜索之前,我正在使用这种方法。似乎导致条目超出tar归档的末尾,所以我会得到一个错误,但我的新文件不会在那里。 – froggythefrog 2017-05-11 02:09:30