Javaでtarアーカイブにファイルを追加するにはどうすればよいですか?を読みました。、アーカイブ全体を読み書きせずにファイルをアーカイブに追加し、既存のコンテンツを上書きせずにtarファイルにエントリを追加しますが、適切な回答は得られませんでした。さらに、コメントを投稿するのに十分な評判がありません。そこで、ここで新しい質問を作成しました。
tarアーカイブにファイルを追加する方法はありますか?すでに存在するファイルを置き換えたいのですが。
次のメソッドを書き始めましたが、ファイルを追加するとアーカイブの内容が消去されます。apachecompressのWebサイトで例は見つかりませんでした。
    static final Logger LOG = Logger.getLogger(ShellClient.class);
public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws IOException {
    if (tarPath == null || tarFileName == null || tarFileName.isEmpty()) {
        LOG.warn("The path or the name of the tar archive is null or empty.");
        return;
    }
    final File tarFile = new File(tarPath, tarFileName);
    final File fileToAdd = new File(tarPath, file2WriteName);
    FileUtils.write(fileToAdd, file2WriteContent);
    if (file2WriteName == null || file2WriteName.isEmpty()) {
        LOG.warn("The name of the file to append in the archive is null or empty.");
        return;
    }
    TarArchiveOutputStream aos = null;
    OutputStream out = null;
    try {
        out = new FileOutputStream(tarFile);
        aos = (TarArchiveOutputStream) new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.TAR, out);
        // create a new entry
        final TarArchiveEntry entry = new TarArchiveEntry(fileToAdd);
        entry.setSize(fileToAdd.length());
        // add the entry to the archive
        aos.putArchiveEntry(entry);
        InputStream is = new FileInputStream(fileToAdd);
        final int byteCopied = IOUtils.copy(is, aos);
        if (LOG.isDebugEnabled()) {
            LOG.debug(byteCopied+" bytes inserted in the tar archive from "+fileToAdd);
        }
        is.close();
        aos.closeArchiveEntry();
        aos.finish();
        aos.flush();
        aos.close();
        out.flush();
        out.close();
    } catch (ArchiveException e) {
        LOG.error(e.getMessage(), e);
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(aos);
        IOUtils.closeQuietly(out);
        FileUtils.deleteQuietly(fileToAdd);
    }
}