Linux – Unzipping files that are flying in through a pipe

compressionlinuxpipeUbuntu

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: How do I pipe a downloaded file to standard output in bash?

Best Answer

While a zip file is in fact a container format, there's no reason why it can't be read from a pipe (stdin) 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(sys.stdin.read())
z = zipfile.ZipFile(data)
dest = sys.argv[1] if len(sys.argv) == 2 else '.'
z.extractall(dest)

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

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

Now unzip the output of wget easily.

wget http://your.domain.com/your/file.zip -O - | unzip-stdin target_dir