best way to extract a file from a zip file and put it back into the zip file after editing
Solution 1
Extract a file from a zip file:
unzip file.zip file/you/want/to/extract/the_file.txt
Modify the_file.txt
Put it back:
zip file.zip file/you/want/to/extract/the_file.txt
That should get you started.
Solution 2
I don't have any zip files handy to test with. but continuing on with holygeek's solution, I don't think you have to even save it to disk, but run it thru some pipes.
unzip -p file.zip /path/2/your/file.txt \
| sed 's/x/y/g' \
| zip file.zip /path/2/your/file.txt -
Note the last '-' is critical, it tells zip to read its data from stdin.
This is from the zip -h2 (full help), under example streams.
I hope this helps.
Related videos on Youtube
Qiang Li
Updated on June 04, 2022Comments
-
Qiang Li almost 2 years
I am wondering if anyone can help me with a SHELL script to do the following job:
extract a file from a zip file and put it back in after some editing, e.g. using
sed
to do some replacement.I want to avoid extracting all contents of the zip file into a temporary folder. It could be possible that the zip file contains files with the same name as the one I want to replace with editing. In such case, I want to specify the full path of such file inside the zip file.
Is there any good way to do this task?
Many thanks.
-
Qiang Li about 13 years+1 and thanks! The first unzip does not actually remove the file from the zip file. And the second zip actually overwrite the original content. Correct? Is there a way in the first step to remove (only) the target file from the zip file?
-
holygeek about 13 yearsYes it does not remove the file from the zip file. To remove the file from the zip file you'll have to do a separate step: zip -d file.zip file/to/delete.txt, I looked this up in the zip man page, the -d or --delete option.
-
Andrew Barber over 11 yearsUser was looking for a shell script. You can't see it, but there's a deleted post where someone already suggested FUSE and OP said it was not an option.
-
Shay over 11 yearsit's very simple to mount the file in a script do the changes and unmount it.
-
Andrew Barber over 11 yearsIt's also very simple to do what the other answers do. Even simpler, in fact. And simplicity was not the point, anyway; as I said, the OP said FUSE was simply not an option. His words, "I don't have the luxury to install FUSE"
-
Shay over 11 yearsmaybe this will help someone else with similar problems
-
hakre almost 2 yearsmy
zip -h2
only specifies it will zip stdin, and in my case it creates an entry named "-" within the zip file. stackoverflow.com/questions/16710341/… shows how to rename.