Extracting Single Files From a Zip

I’ve been working with lots of zip files lately – site backups mostly. Often times this is because of some error message referring to a file contained within the archive.

When the zip is quite large, having to extract gigabytes of information just to view the contents of one file is annoying. Because of this I came across this StackExchange link with a few options to extract just one file.

The are the two main ways I’ve been using, but there are others!

Extract the thing, right here!

The typical way which makes sense in most use cases is this:

unzip -d . site-backup.zip backup.sql

This is basically saying is:

Within the site-backkup.zip archive, extract the backup.sql file right here (.)

This is great, and for the most part does what we need it to do.

What About a Deeply Nested File?

The previous example is good, but what if I have a file that is deeply nested, something like:

wp-content/plugins/random-plugin/inc/foo/bar/baz.php

Depending on what I think might be going on, I might just want to take a quick peek. If I used the previous technique, all of those folders would be created and would require me to traverse down to the file before opening. Not too bad, but still a little annoying.

Instead, I can extract that file to stdout and then redirect the output to a file in the current directory. Using the above baz.php file as an example, this would look like:

unzip -p site-backup.zip wp-content/plugins/random-plugin/inc/foo/bar/baz.php > baz.php

Two main downsides:

  1. If it’s code, you’ll likely need to other files unless it’s a really trivial issue
  2. The fileperms aren’t preseverved

Posted

in

by

Tags: