Is there a way to edit files inside of a zip file without explicitly extracting them first?
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)
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?
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.