linux how to add a file to a specific folder within a zip file linux how to add a file to a specific folder within a zip file linux linux

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:

zip -g xxx.zip folder/file

Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.


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/bashpython -c 'import zipfile as zf, sysz=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.