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?
Just curious if the answer works on .jar files? (I didn't test it there.) – beroe – 2013-09-24T02:31:43.690
@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 – 2013-09-24T14:41:09.930
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 – 2013-09-24T14:45:32.283
@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 – 2013-09-24T14:47:42.003
OK, added another solution to support sub-folders, and it works in limited testing. – beroe – 2013-09-24T19:37:09.313