Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Can I make unzip or any similar programs work on the standard output? The situation is I'm downloading a zip file, which is supposed to be unzipped on the fly.

Related issue:

share|improve this question
This seemed like it should be doable, but it looks like it's only possible to extract a zip and pipe the file to another command if the zip contains only a single file. I wanted to extract a specific file from a multi-file zip. Instead of piping, I switched to chaining multiple commands 'unzip /path/file && dostuff /path/file && rm -rf /path' While not answering the original question, and resulting in temporary files being created, it satisfied my need. – Stan Kurdziel Jul 18 '13 at 21:13
Check out pigz. We use it in a pipe. – dmourati Nov 14 '13 at 23:18

While a zip file is in fact a container format, there's no reason why it can't be read as a stream if the file can fit into memory easily enough. Here's a Python script that takes a zip file as standard input and extracts the contents to the current directory or to a specified directory if specified.

import zipfile
import sys
import StringIO
data = StringIO.StringIO(
z = zipfile.ZipFile(data)
dest = sys.argv[1] if len(sys.argv) == 2 else '.'

This script can be minified to one line and created as an alias.

alias unzip-stream="python -c \"import zipfile,sys,StringIO;zipfile.ZipFile(StringIO.StringIO([1] if len(sys.argv) == 2 else '.')\""

Now unzip your stream easily.

wget -O - | unzip-stream target_dir
share|improve this answer
You and python rock!!! – Farid Nouri Neshat May 27 '12 at 19:54
Nice one-liner, and +1 for mentioning that the file has to fit into memory. (There is unfortunately no way to unzip a pkzip file due to the file format structure). – lxgr Jun 9 '12 at 21:28
keep in mind this buffers everything in memory before extracting – William Casarin Jun 21 '14 at 5:19
there's no reason why it can't be read as a stream if the file can fit into memory easily enough isn't really accurate. The reason why you're forced to buffer the whole zip archive in memory before you extract the contents is specifically because it can't be read as a stream. Of course, it can still be useful to avoid writing the zip archive to a file. – Håkan Lindqvist Mar 5 at 16:23

This is unlikely to work how you expect. Zip is not just a compression format, but also a container format. It rolls up the jobs of both tar and gzip.bzip2 into one. Having said that, if your zip has a single file, you can use unzip -p to extract the files to stdout. If you have more than one file, there's no way for you to tell where they start and stop.

As for reading from stdin, the unzip man page has this sentence:

Archives read from standard input are not yet supported, except with funzip (and then only the first member of the archive can be extracted).

You might have some luck with funzip.

share|improve this answer
If zip has multiple files inside, then -p can print out single file using file name as a parameter: unzip -p file-inside-zip – Taavi Ilves Dec 23 '15 at 11:51

What you want to do is, make unzip take a ZIPped file on its standard input rather than as an argument. This is usually easily supported by gzip and tar kind of tools with a - argument. But the standard unzip does not do that (though, it does support extraction to a pipe). However, all is not lost...

Look at funzip manual page.

funzip without a file argument acts as a filter; that is, it assumes that a ZIP archive (or a gzip'd file) is being piped into standard input, and it extracts the first member from the archive to stdout. When stdin comes from a tty device, funzip assumes that this cannot be a stream of (binary) compressed data and shows a short help text, instead. If there is a file argument, then input is read from the specified file instead of from stdin.

Given the limitation on single-member extraction, funzip is most useful in conjunction with a secondary archiver program such as tar(1). The following section includes an example illustrating this usage in the case of disk backups to tape.

This goes well with the idea that most linux archives are usually TAR'ed and then ZIPped in some way (gzip, bzip, et al). This will work for you if you have a tar.ZIP.

It is worth noting that funzip is written by Info-ZIP original author Mark Adler. He writes in the funzip man page,

this functionality should be incorporated into unzip itself (future release).

however, no such update is seen around. I suspect that Mark found it unnecessary since other archiving methods worked easily with TAR.

share|improve this answer
Just a comment; some people would like python or any language as an option to unzip. A prime example is Heroku which does not include tar or unzip on its system. A work around is to use jar by installing Java which is allowed. – Nick Nov 11 '14 at 20:37

It's not possible with Info-Zip which is the most common OSS implementation. More importantly though, it's not recommended due to the constructs of ZIP archives.

If a change of format is viable to you then consider using tar(1) instead. It is quite happy with streamed input/output and, in fact, expects it by default.

Additionally you can often tell whether applications expect streamed input/output by specifying "-" for a filename. Info-Zip, as you can imagine, doesn't treat this as a valid argument.

share|improve this answer

This is a repost of my answer to a similar question:

The ZIP file format includes a directory (index) at the end of the archive. This directory says where, within the archive each file is located and thus allows for quick, random access, without reading the entire archive.

This would appear to pose a problem when attempting to read a ZIP archive through a pipe, in that the index is not accessed until the very end and so individual members cannot be correctly extracted until after the file has been entirely read and is no longer available. As such it appears unsurprising that most ZIP decompressors simply fail when the archive is supplied through a pipe.

The directory at the end of the archive is not the only location where file meta information is stored in the archive. In addition, individual entries also include this information in a local file header, for redundancy purposes.

Although not every ZIP decompressor will use local file headers when the index is unavailable, the tar and cpio front ends to libarchive (a.k.a. bsdtar and bsdcpio) can and will do so when reading through a pipe, meaning that the following is possible:

wget -qO- | bsdtar -xvf-
share|improve this answer

I like to use curl because it is installed by default (the -L is needed for redirects which often occur):

curl -L | bsdtar -xvf - -C /path/to/directory/

However, bsdtar is not installed by default, and I could not get funzip to work.

share|improve this answer

I actually needed something a little more complex - extract a specific file if it exists. The difficulty being, the input file stream may not be a zip file, and in which case, I needed it to continue through the pipe. Here is my solution (thanks mostly to Jason R. Coombs solution)

python -c "import zipfile,sys,StringIO
except (RuntimeError, zipfile.BadZipfile):

I saved this as a file named "effpoptp" (not a simple name) in the "/bin" folder on my machine so testing it is like so:

cat defaultModel.mwb|effpoptp "document.mwb.xml"

The purpose is to version control MySQL Workbench files, where the file could be the xml file named as the workbench file, or the complete workbench file.

share|improve this answer

In zsh, you can do the following:

unzip =( curl )
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.