Created
November 27, 2015 11:52
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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