There are several programs, both command line and GUI based that will allow you to view and extract the contents of an archive. But sometimes, you will need to modify an existing archive file, such as add files to archive, delete an existing file or renaming a file.
Most of the GUI applications, like Ark support adding and deleting files using the drag and drop. This is accomplished by extracting the archive in memory and performing the modification operation. The archive is then re-archived from memory and saved to the disk. A GUI interface may not always be available and you may want to modify the archive from command line without extracting it completely.
You always do have the option of extracting the archive to the hard disk first. You can then modify the contents and re-create the archive from the modified content. This requires space on the disk and extra time for extraction and archiving.
The archives are usually the combination of two different process: archiving and compression. There are a couple of caveats that you should be aware of while modifying the archives.
compressed archive: You can directly modify an archive but not a compressed archive. Modifying the compressed file without uncompressing it first will corrupt the file. For example, this means that you can modify a .tar file, but not a .tar.gz file. The .tar.gz should first be uncompressed to .tar.
file position: There is really no way to “replace” a file inside the archive while also maintaining its position. You can always extract the whole archive to a temp location, modify or replace the file and then archive it back up. But we are only dealing with the scenario of doing the operations in place without extracting the entire archive.
Add files to archive
Assuming that you have a archive with .tar extension, you can use the -r (or –append) option of the tar command to add/append a new file to the end of the archive.
bash$ tar rvf /path/to/archive.tar /path/to/newfile.txt
You can use the -v option to have a verbose output to verify the operation. The other option that can be used with the tar command is -u (or –update).
The update refreshes the files that are newer than the ones in the archive, so it works slightly different than the –append (or -r) option but provides the same functionality for the addition of a new file.
bash$ tar uvf /path/to/archive.tar /path/to/newfile.txt
Another feature of the archive is that it allows for multiple files with the same name. If you append a file with the same name, then the file is just appended to the end of the archive without deleting the previous one. But when you extract the archive, it will extract the latest version (by its order inside the archive) of the file.
The zip archive can be modified using the zip command. The -r option of the zip command allows you to adding files.
bash$ zip -rv zipfile.zip newfile.txt newfile1.txt
where the zipfile.zip is the name of an existing zip file and the newfile.txt is the file that you want to add to the zip archive.
In case of the jar files, you can use the jar command. The –update (or -u) option allows you to add or update files into a jar archive.
bash$ jar -uvf jarfile.jar newfile.txt
Again, you can add multiple files in a single command just like the zip command.
Delete files from archive
You can use the –delete option of the tar command to delete existing files from inside an archive.
bash$ tar -dvf archive.tar filename.txt
You can use the –delete (or -d) option of the zip command to delete files from the archive. The zip command take multiple arguments for file names and supports regular expressions in the argument. This allows for deletion of multiple files with a single command.
bash$ zip -d zipfile.zip filename.doc \*.txt
The above command will delete the file named filename.doc and also all the files with an extension of .txt.
Unfortunately there is no option with the jar command to delete the files. But jar files are essentially zip files with .jar extension, hence you can use the zip command to delete the files, just as you would to a zip file.
bash$ zip -d jarfile.jar file1.txt file2.txt
Renaming files inside the archive essentially amounts to deleting the existing file and adding the new file. This will also cause the file to lose its position inside the tar archives.