Assert.notNull(in);
File newArchive = FileUtils.createRandomTempFile(".tar.gz");
newArchive.deleteOnExit();
TarArchiveOutputStream newArchiveOut =
new TarArchiveOutputStream(new GZIPOutputStream(new FileOutputStream(newArchive)));
newArchiveOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
TarArchiveInputStream archiveIn = new TarArchiveInputStream(new GZIPInputStream(in));
String pathToReplace = null;
try {
// copy the existing entries
for (ArchiveEntry nextEntry = null; (nextEntry = archiveIn.getNextEntry()) != null;) {
if (nextEntry.getName().endsWith(name)) {
pathToReplace = nextEntry.getName();
}
newArchiveOut.putArchiveEntry(nextEntry);
IOUtils.copy(archiveIn, newArchiveOut);
newArchiveOut.closeArchiveEntry();
}
if (pathToReplace == null) {
throw new IllegalStateException("Could not find file " + name + " in the given archive.");
}
TarArchiveEntry newEntry = new TarArchiveEntry(pathToReplace);
newEntry.setSize(newContent.length());
newArchiveOut.putArchiveEntry(newEntry);
IOUtils.copy(new ByteArrayInputStream(newContent.getBytes()), newArchiveOut);
newArchiveOut.closeArchiveEntry();
return newArchive;
} finally {
newArchiveOut.finish();
newArchiveOut.flush();
StreamUtils.close(archiveIn);
StreamUtils.close(newArchiveOut);
}
}