Is it possible to fake destination directory path with zip

zip

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:

ZipFile.write(filename[, arcname[, compress_type]])
Write the file named filename to the archive, giving it the archive name arcname (by default, this will be the same as filename, but without a drive letter and with leading path separators removed). If given, compress_type overrides the value given for the compression parameter to the constructor for the new entry.
Note: Archive names should be relative to the archive root, that is, they should not start with a path separator.

Here's an example:

$ touch 1 2 3
$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
>>> import zipfile
>>> with zipfile.ZipFile('bundle.zip', 'w') as bundle:
...   bundle.write('1', '/bin/1')
...   bundle.write('2', '/sbin/2')
...   bundle.write('3', '/usr/bin/3')
... 
>>> 
$ unzip -l bundle
Archive:  bundle.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  2014-08-11 13:00   bin/1
        0  2014-08-11 13:00   sbin/2
        0  2014-08-11 13:00   usr/bin/3
---------                     -------
        0                     3 files

Note that zipfile.write will remove any leading '/' from the pathname, to conform with the standard.

Related Question