Skip to content

Instantly share code, notes, and snippets.

@dnet
Created November 27, 2015 11:52
Show Gist options
  • Save dnet/7debd86deae286170284 to your computer and use it in GitHub Desktop.
Save dnet/7debd86deae286170284 to your computer and use it in GitHub Desktop.
Partial implementation of the interface provided by zipfile.ZipFile using the unzip command
from subprocess import Popen, PIPE
from tempfile import TemporaryFile
from os import devnull
class ZipFile(object):
def __init__(self, filename):
self.filename = filename
open(filename).close()
def __enter__(self):
self.dn = open(devnull)
return self
def __exit__(self, type, value, tb):
self.dn.close()
def open(self, path):
tf = TemporaryFile(prefix='unzipwrapper')
unzip = Popen(['unzip', '-p', self.filename, path], stdout=PIPE, stderr=self.dn)
stdout, stderr = unzip.communicate()
tf.write(stdout)
tf.seek(0)
return tf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment