[ACCEPTED]-If I have the contents of a zipfile in a Python string, can I decompress it without writing it to a file?-python

Accepted answer
Score: 32

zipfile.ZipFile accepts any file-like object, so you can 1 use StringIO (2.x) or BytesIO (3.x):

try:
  from cStringIO import StringIO
except:
  from StringIO import StringIO
import zipfile

fp = StringIO('PK\x03\x04\x14')
zfp = zipfile.ZipFile(fp, "r")
Score: 6

Wrap your string in a cStringIO object. It looks, acts, and 2 quacks like a file object, but resides in 1 memory.

More Related questions