- Add file to deep inside a zip file
- 3 Answers 3
- Use Python’s zipfile library?
- how to add new files into an archive file (tar, gz, zip) in linux (also delete files)
- Add files to archive
- tar archive
- zip archive
- jar archive
- Delete files from archive
- tar archive
- zip archive
- jar archive
- linux how to add a file to a specific folder within a zip file
Add file to deep inside a zip file
Say I want to add the file file.txt to foo.zip , I could just do zip -u foo.zip file.txt . However inside the zip-file there already exist a folder with the path foo.zip/very/many/paths/ (relatively to the zip-file). How would I add file.txt to the zip-file so it’s location inside the zip-file would be foo.zip/very/many/paths/file.txt ? I could create the directory structure needed first, but isn’t there an easier way? I would normally do it like this:
$ ls file.txt foo.zip $ mkdir very $ mkdir very/many $ mkdir very/many/paths $ cp file.txt very/many/paths $ zip -u foo.zip very/many/paths/file.txt $ rm -rf very
I don’t know if this can be done any easier but at least you can shorten multiple mkdir commands with mkdir -p very/many/paths which creates any parent directories as needed. 😉
If you have to do it often, just create a bash script that automatise it. Taking arguments your archive and the path and file in the archive like updatezip very/many/path/file.txt file.txt
7z has an option for this 7z a yourfile.7z -sivery/many/paths/file.txt < file.txt but unfortunately, when I try this with zip, it results in an error. So this feature only works with 7z not with zip/tar :(
Possible duplicate of Add a file to a different path (but the question hasn’t received an upvoted or accepted answer).
3 Answers 3
Use Python’s zipfile library?
~/wrk/tmp$ ls test.zip ls: cannot access test.zip: No such file or directory
Ok. There is no ‘test.zip’ right now.
~/wrk/tmp$ python -c 'import zipfile,sys ; zipfile.ZipFile(sys.argv[1],"a").write(sys.a rgv[2],sys.argv[3])' test.zip /etc/motd text/motd
Let’s add ‘/etc/motd’ as ‘text/motd’ to the nonexisting zipfile.
~/wrk/tmp$ ls -l test.zip -rw-r--r-- 1 yeti yeti 473 Mar 23 09:51 test.zip
The zipfile library was nice enough to create ‘test.zip’.
~/wrk/tmp$ unzip -lv test.zip Archive: test.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 357 Stored 357 0% 2014-03-20 15:47 ff1b8b7f text/motd -------- ------- --- ------- 357 357 0% 1 file
..and it seems to contain what I wated.
Let’s check it by unzipping it to stdout.
~/wrk/tmp$ unzip -p test.zip text/motd Linux aurora 3.2.0-0.bpo.4-amd64 #1 SMP Debian 3.2.54-2~bpo60+1 x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law.
~/wrk/tmp$ python -c 'import zipfile,sys ; zipfile.ZipFile(sys.argv[1],"a").write(sys.argv[2],sys.argv[3])' test.zip /etc/issue otherdir/issue ~/wrk/tmp$ ls -l test.zip -rw-r--r-- 1 yeti yeti 605 Mar 23 09:52 test.zip (yeti@aurora:1)~/wrk/tmp$ unzip -lv test.zip Archive: test.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 357 Stored 357 0% 2014-03-20 15:47 ff1b8b7f text/motd 28 Stored 28 0% 2012-09-21 22:52 f9c3990c otherdir/issue -------- ------- --- ------- 385 385 0% 2 files ~/wrk/tmp$ unzip -p test.zip otherdir/issue Debian GNU/Linux 6.0 \n \l ~/wrk/tmp$ _
This post and this website contains affiliate links. See my disclosure about affiliate links.
how to add new files into an archive file (tar, gz, zip) in linux (also delete files)
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 the archive, delete an existing file in the archive or rename a file in the archive.
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 can always do it the long way. First extracting the archive to the hard disk and 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.
Most of the widely used archive formats 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
tar 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.
zip archive
The zip archive can be modified using the zip command. The -r option of the zip command allows you to add new files to the archive.
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 and newfile1.txt are the files that you want to add to the zip archive.
jar 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
tar 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
zip archive
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.
jar archive
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. However, this will also cause the file to lose its position inside the tar archive. If the order of the files is important, then you should extract the archive and rename the file on the disk first.
linux how to add a file to a specific folder within a zip file
If you need to add the file to the same folder as in the original directory hierarchy, then you just need to add the full path to it:
Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.
if i wanna put yyy.txt into xxx.apk’s assests’ folder, firstly i must creat assests folder,then move yyy.txt into it. And then excute zip -g xxx.apk assests/yyy.txt
If you want to have the good hierarchy without copying files into a newly created dir, you can create a symbolic link : ln -s long_path_on_disk folder_in_zip. Then, zip -g xxx.zip folder_in_zip/file
To elaborate on @Ignacio Vazquez-Abrams answer from a year ago, you can use a lower level library, such as the one that comes with Python:
#!/bin/bash python -c ' import zipfile as zf, sys z=zf.ZipFile(sys.argv[1], "a") z.write(sys.argv[2], sys.argv[3]) z.close() ' myfile.zip source/dir/file.txt dir/in/zip/file.txt
This will open myfile.zip and add source/dir/file.txt from the file system as dir/in/zip/file.txt in the zip file.
Info-ZIP cannot do this. You will need to write a script or program in a language that has lower-level access to zip files.
if i wanna put yyy.txt into xxx.apk’s assests’ folder, firstly i must creat assests folder,then move yyy.txt into it. And then excute zip -g xxx.apk assests/yyy.txt
Or you can generate an artificial zip entry that puts it in assets/ , then combine it with the data from the file and add it to the zip file.
I have expended a little @»that other guy» solution
Go to console, press ctrl+x,ctrl+e, paste there
( cat /tmp/zip-extend && chmod +x /tmp/zip-extend
then run /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml
cd /tmp touch first_file.txt zip my_zip.zip first_file.txt unzip -l my_zip.zip mkdir -p your/existing touch your/existing/file_to_add.xml /tmp/zip-extend my_zip.zip your/existing/file_to_add.xml directory_in_zip/file_to_add.xml unzip -l my_zip.zip cd -
Archive: my_zip.zip Length Date Time Name --------- ---------- ----- ---- 0 2013-12-17 15:24 first_file.txt 0 2013-12-17 15:24 directory_in_zip/file_to_add.xml --------- ------- 0 2 files