ZIP – How to Extract a Specific File to a Given Directory

zip

I need to extract a single file from a ZIP file which I know the path to. Is there a command like the following:

unzip -d . myarchive.zip path/to/zipped/file.txt

Unfortunately, the above command extracts and recreates the entire path to the file at ./path/to/zipped/file.txt. Is there a way for me to simply pull the file out into a specified directory?

Best Answer

You can extract just the text to standard output with the -p option:

unzip -p myarchive.zip path/to/zipped/file.txt >file.txt

This won't extract the metadata (date, permissions, …), only the file contents (obviously, it only works for regular files, not symlinks, devices, directories...). That's the price to pay for the convenience of not having to move the file afterwards.

Alternatively, mount the archive as a directory and just copy the file. With AVFS:

mountavfs
cp -p ~/.avfs"$PWD/myarchive.zip#"/path/to/zipped/file.txt .

Or with fuse-zip:

mkdir myarchive.d
fuse-zip myarchive.zip myarchive.d
cp -p myarchive.d/path/to/zipped/file.txt .
fusermount -u myarchive.d; rmdir myarchive.d
Related Question