Given a bunch of files in the current directory, I want to be able to zip them such that, when extracted, they'd be placed in some given directories. For example, say I have these in $PWD
:
ls
fsck
xz
chroot
And when I run unzip foo.zip
, they each get extracted to these directories:
/bin
/sbin
/usr/bin
/usr/sbin
The closest I could find is that these files must already exist in whatever directory paths I want the files to be extracted to (using $PWD
as root directory):
$ zip foo.zip bin/ls sbin/fsck usr/bin/xz usr/sbin/chroot
Then, on a system where I want this extraction to happen, I would have to:
$ cd /
$ sudo unzip foo.zip
Best Answer
I don't see a way to do this using the
zip
command, but it's easy in python.Note that the zip file format specification, section 4.4.17.1, says that the pathname cannot start with a '/', so I can't help with that part.
The python zipfile module will let you override a file's pathname when you add it to the zip archive; just pass the desired name as the optional 2nd argument to
ZipFile.write
:Here's an example:
Note that
zipfile.write
will remove any leading '/' from the pathname, to conform with the standard.