18

I've written some Python code that fetches a zip file from the web and into a string:

In [1]: zip_contents[0:5]
Out[1]: 'PK\x03\x04\x14'

I see there's a zipfile library, but I'm having trouble finding a function in it that I can just pass a bunch of raw zip data. It seems to want to read it from a file.

Do I really need to dump this to a temp file, or is there a way around it?

mike
  • 46,876
  • 44
  • 102
  • 112

3 Answers3

33

zipfile.ZipFile accepts any file-like object:

class zipfile.ZipFile(file, …)

Open a ZIP file, where file can be a path to a file (a string), a file-like object or a path-like object.

So you can use BytesIO (3.x) or StringIO (2.x):

# Modern Python 3.x
from io import BytesIO
import zipfile

fp = BytesIO(b'PK\x03\x04\x14')
zfp = zipfile.ZipFile(fp, "r")
# Legacy Python 2.x
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO
import zipfile

fp = StringIO('PK\x03\x04\x14')
zfp = zipfile.ZipFile(fp, "r")
Denilson Sá Maia
  • 47,466
  • 33
  • 109
  • 111
John Millikin
  • 197,344
  • 39
  • 212
  • 226
6

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

Eevee
  • 47,412
  • 11
  • 95
  • 127
0

This question is old, python changed a little bit since the accepted answer.

The cStringIO lib changed name since python 3.0 (more details in https://stackoverflow.com/a/18284900/8647541)

Just try the following :

import io
import zipfile

fp = io.StringIO('PK\x03\x04\x14') # or io.BytesIO if you have a Bytes object
zfp = zipfile.ZipFile(fp, "r")
zfp.extractall("./extract_folder") # extract zip in a `extract_folder` folder
Jorane
  • 1
  • 1