zipfile.is_zipfile() and string buffers

B

Brendan

I would like zipfile.is_zipfile(), to operate on a cStringIO.StringIO
string buffer, but is seems only to accept file names as arguments.
Should it not be able to handle string buffers too?
 
G

Gabriel Genellina

I would like zipfile.is_zipfile(), to operate on a cStringIO.StringIO
string buffer, but is seems only to accept file names as arguments.
Should it not be able to handle string buffers too?

A version of zipfile.is_zipfile() accepting both file names and file
objects:

def _check_zipfile(fp):
try:
if _EndRecData(fp):
return True # file has correct magic number
except IOError:
pass
return False

def is_zipfile(filename):
"""Quickly see if file is a ZIP file by checking the magic number."""
result = False
try:
if hasattr(filename, "read"):
result = _check_zipfile(fp=filename)
else:
with open(filename, "rb") as fp:
result = _check_zipfile(fp)
except IOError:
pass
return result
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,081
Members
48,772
Latest member
Backspace Studios

Latest Threads

Top