69

I sometimes need to make changes to a .zip or .jar file, so I usually move the file to /tmp, extract all the files with unzip, edit a few files, and then re-zip up the files. This works, but it can be tedious. Is there a utility or shell script that I can use to edit a file inside of a zip file without explicitly calling unzip and zip (even if it's just a wrapper around these commands)?

7
  • Just curious if the answer works on .jar files? (I didn't test it there.)
    – beroe
    Sep 24, 2013 at 2:31
  • @beroe It should since those use the zip compression algorithm. That was actually my main motivation for looking for a solution because I had .war files deployed on an app server that I didn't feel like re-packaing up and re-deploying just to modify a single file.
    – austin
    Sep 24, 2013 at 14:41
  • Great. I am going to try to fix the function so it preserves directory structure inside the archive. Currently I think it only works on files at root level, but for my purposes, subfolders are more useful.
    – beroe
    Sep 24, 2013 at 14:45
  • @beroe That's pretty cool. Before asking this, I was going to code up a python script for launching a psudo-shell "inside" the zip file to execute arbitrary commands. I'd be interested in what you come up with.
    – austin
    Sep 24, 2013 at 14:47
  • OK, added another solution to support sub-folders, and it works in limited testing.
    – beroe
    Sep 24, 2013 at 19:37

8 Answers 8

25

Do you know the name of the file in the archive before unzipping it? You could make a function to unzip to /tmp, edit, and refresh the zip:

zipedit(){
    echo "Usage: zipedit archive.zip file.txt"
    unzip "$1" "$2" -d /tmp 
    vi /tmp/$2 && zip -j --update "$1"  "/tmp/$2" 
}

As it says, usage is:

zipedit myarchive.zip myfile.txt

This unpacks the named file from the archive, saves it to /tmp, edits it in vi then adds it back to the archive, while "junking" the path. Add to your .bash_profile, assuming bash...

EDIT: Below is a version which works with subfolders inside the archive... Note, do not use a slash before the name of the folder (i.e. use myfolder/file.txt not /myfolder/file.txt). If you edit a file that didn't already exist in the archive, it will create it for you. Also not sure if it will work with the absolute path to the zip file. Best stick with relative.

zipedit(){
    echo "Usage: zipedit archive.zip folder/file.txt"
    curdir=$(pwd)
    unzip "$1" "$2" -d /tmp 
    cd /tmp
    vi "$2" && zip --update "$curdir/$1"  "$2" 
    # remove this line to just keep overwriting files in /tmp
    rm -f "$2" # or remove -f if you want to confirm
    cd "$curdir"
}

Thanks for the question. I'll probably end up using this one too!

Another edit: Untested, but I read that vim and emacs will both edit jar files directly?

1
  • 1
    I can confirm that vim works splendid for editing zip-files from the command line on Linux, thanks alot for the tip! Apr 24, 2014 at 18:08
81

Vim supports transparently editing files inside zip files. Just execute:

vim file.zip

and you will be shown a list of files inside zip archive. Choose the one you want to edit, change what you want, and exit with :x

If vim responds with:

Cannot make changes, 'modifiable' is off

.. just run :set modifiable or :set ma (source: https://stackoverflow.com/questions/5745506/vim-modifiable-is-off)

8
  • 2
    By the way, default zip support in vim only allows editing one level ZIPs. If you need to edit ZIPs inside ZIPs, you should use this vim plugin.
    – jesjimher
    May 14, 2014 at 8:27
  • 2
    Same with emacs.
    – Mike
    Sep 2, 2015 at 12:45
  • 8
    This should be marked as the right answer Feb 18, 2016 at 14:37
  • 3
    life saver. really cool feature of vim, and quite unexpected too Aug 9, 2016 at 8:15
  • 1
    Also, if you get the buftype issue, issue the comman :set bt= and then you can save.
    – JonShipman
    Apr 20, 2020 at 13:25
6

Short answer: NO.

If it's a wrapper, you are calling these commands. Anyway, the best I can think of is to open the file using file-roller, if you are in an X environment, that might work with a simple double click, depending on your setup. You can then double click on the compressed file to open it and then you can edit it:

$ file-roller b3.zip 

When you save your edited file, you should get this dialog:

You could make a script for this also, but that gets complicated if you have compressed archives that contain multiple files. Let me know if that's what you need and I might be able to cook something up.

enter image description here

3

Short pedantic answer; no. If you think about compression, you're using redundancy to shorten the files inside, so any edit changes the whole file within the archive, possibly the archive.

If you're being less theoretical, more practical, more "I don't want to have to manually unzip/zip" there are tools that you can use. ark on Linux is one I've used. You could also mount the archive with fuse-zip, though that's probably more work than a temp file.

2
  • 1
    don't know why someone gave -1. +1 for fuse-zip.
    – myrdd
    Feb 11, 2019 at 17:33
  • and here is another :) Dec 7, 2021 at 17:32
1

Directory Opus 12 file manager will allow you to browse the Zip, then drag and drop the edited file from another folder into it and overwrite the file you want to edit, and it will do it all on the fly. Very easy

0

According to the man page for zip (online version for easy reference: http://linux.die.net/man/1/zip), the zip command has a -u flag which it describes as:

Replace (update) an existing entry in the zip archive only if it has been modified more recently than the version already in the zip archive.

0

I wrote a Vim plugin that extends stock zip.vim to browse and edit nested zip files.

https://github.com/lbrayner/vim-rzip

2
  • how do you install this on linux?
    – Pandrei
    Jan 7, 2019 at 11:45
  • I added installation instructions to the README of the repository. Please check back.
    – lbrayner
    Jan 9, 2019 at 16:16
0

You can use standart KDE text editor for example:

  1. open archive in archive manager
  2. open text file in KDE gui editor
  3. save text file and system ask you to refresh archive - click yes
  4. don't forget about permission for archive and folder of archive (my favorite mistake)
1
  • Please fix formatting on your post. Jan 30, 2018 at 8:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .