Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8694479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:47:58+00:00 2026-06-13T00:47:58+00:00

I read How do I append files to a tar archive in java? ,

  • 0

I read How do I append files to a tar archive in java?, append files to an archive without reading/rewriting the whole archive and Add an entry to a tar file without overwriting its existing contents but no good answer was given. Moreover I do not have enough reputation to post comments. So I created a new question here.

Is there a way to append a file in a tar archive? I would like to replace a file if it exists already.

I have started to write the following method but when the file is added, it erases the archive content. I didn’t find any example on the apache compress website.

    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);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T00:47:59+00:00Added an answer on June 13, 2026 at 12:47 am

    Finally I succeeded to do it using this post.

    I created a copy of the tar archive and copied to entire content to it. Then I delete the old tar archive.

    public void appendFileInTarArchive(String tarPath, String tarFileName, String file2WriteName, String file2WriteContent) throws AuthenticationException, 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;
        }
    
        ArchiveStreamFactory asf = new ArchiveStreamFactory();
    
        File tempFile = new File(tarPath, "tmpTar.tar");
        tempFile.createNewFile();
    
        try {
            FileInputStream fis = new FileInputStream(tarFile);
            ArchiveInputStream ais = asf.createArchiveInputStream(ArchiveStreamFactory.TAR, fis);
    
            FileOutputStream fos = new FileOutputStream(tempFile);
            ArchiveOutputStream aos = asf.createArchiveOutputStream(ArchiveStreamFactory.TAR, fos);
    
            // copy the existing entries    
            ArchiveEntry nextEntry;
            while ((nextEntry = ais.getNextEntry()) != null) {
                aos.putArchiveEntry(nextEntry);
                IOUtils.copy(ais, aos);
                aos.closeArchiveEntry();
            }
    
            // create the new entry
            TarArchiveEntry entry = new TarArchiveEntry(file2WriteName);
            entry.setSize(fileToAdd.length());
            aos.putArchiveEntry(entry);
            IOUtils.copy(new FileInputStream(fileToAdd), aos);
            aos.closeArchiveEntry();
    
            aos.finish();
    
            ais.close();
            aos.close();
    
            // copies the new file over the old
            tarFile.delete();
            tempFile.renameTo(tarFile);
    
        } catch (ArchiveException e) {
            LOG.error(e.getMessage(), e);
        } catch (IOException e) {
            LOG.error(e.getMessage(), e);
        } finally {
            FileUtils.deleteQuietly(fileToAdd);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does Google read external Javascript Files that are included? If I were to append
I'm trying to read a .java file into a JTextArea and no matter what
Im trying to read a whole text file with a method i created. All
I want to update an existing tar file with newer files. At GNU, I
I tried to use java.io.FileReader to read some text files and convert them into
I am looking to read the contents of a file in Java. I have
Currently one my DAL methods uses the ExecuteXmlReader command to read and append to
I read a text file to get some info from it and later on
I have a small script we're using to read in a CSV file containing
We have a simple file browser on our intranet, built using ASP/vbscript. The files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.