Is there a way to edit files inside of a zip file without explicitly extracting them first?

87,146

Solution 1

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?

Solution 2

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)

Solution 3

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

Solution 4

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

Solution 5

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.

Share:
87,146

Related videos on Youtube

austin
Author by

austin

My name is Austin. I'm a software engineer. Twitter: @austinkeeley Blog: http://www.espressoninja.com Personal Website: http://austinkeeley.com I mostly do Python and JavaScript development these days, but in the past I've done a lot of full stack development in a variety of languages and environments. I've been getting into functional programming lately by teaching myself Haskell and Purescript. I graduated from James Madison University in 2008 with a degree in computer science and then earned my Masters degree from Johns Hopkins University in 2011. Other than software, I'm really into coffee. I'd like to do a trip to a coffee farm and try to follow a batch of beans from the tree to the cup.

Updated on September 18, 2022

Comments

  • austin
    austin over 1 year

    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)?

    • beroe
      beroe over 10 years
      Just curious if the answer works on .jar files? (I didn't test it there.)
    • austin
      austin over 10 years
      @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.
    • beroe
      beroe over 10 years
      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.
    • austin
      austin over 10 years
      @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.
    • beroe
      beroe over 10 years
      OK, added another solution to support sub-folders, and it works in limited testing.
  • Thomas Bindzus
    Thomas Bindzus about 10 years
    I can confirm that vim works splendid for editing zip-files from the command line on Linux, thanks alot for the tip!
  • jesjimher
    jesjimher almost 10 years
    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.
  • Mike
    Mike over 8 years
    Same with emacs.
  • Martijn Burger
    Martijn Burger about 8 years
    This should be marked as the right answer
  • Gz Zheng
    Gz Zheng over 7 years
    life saver. really cool feature of vim, and quite unexpected too
  • davidbaumann
    davidbaumann about 6 years
    Please fix formatting on your post.
  • Pandrei
    Pandrei over 5 years
    how do you install this on linux?
  • lbrayner
    lbrayner over 5 years
    I added installation instructions to the README of the repository. Please check back.
  • myrdd
    myrdd about 5 years
    don't know why someone gave -1. +1 for fuse-zip.
  • JonShipman
    JonShipman about 4 years
    Also, if you get the buftype issue, issue the comman :set bt= and then you can save.
  • Eric Duminil
    Eric Duminil over 3 years
    Nice, it even works with vimdiff a.zip b.zip in order to compare the contents.
  • Rafal
    Rafal over 2 years
    Is there a way to specify a full path to a file inside the zip archive to edit it right away (without the need to select it interactively)? For example, if the zip is an EPUB and it includes text/file.html file, I would want to do something like myepub.epub:text/file.html or myepub.epub/text/file.text. I need this to modify a file inside ZIP non-interactively (through vim script like this: unix.stackexchange.com/a/14221/48886) but it would be useful if the same file needs to be modified interactively in different archives.
  • Sridhar Sarnobat
    Sridhar Sarnobat over 2 years
    and here is another :)