best way to extract a file from a zip file and put it back into the zip file after editing

18,689

Solution 1

  1. Extract a file from a zip file:

    unzip file.zip file/you/want/to/extract/the_file.txt

  2. Modify the_file.txt

  3. 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.

Share:
18,689

Related videos on Youtube

Qiang Li
Author by

Qiang Li

Updated on June 04, 2022

Comments

  • Qiang Li
    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
    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
    holygeek about 13 years
    Yes 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
    Andrew Barber over 11 years
    User 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
    Shay over 11 years
    it's very simple to mount the file in a script do the changes and unmount it.
  • Andrew Barber
    Andrew Barber over 11 years
    It'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
    Shay over 11 years
    maybe this will help someone else with similar problems
  • hakre
    hakre almost 2 years
    my 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.

Related