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

Similar Threads

Buffers 5
fstream Buffers 26
File position and buffers 21
String Buffers? 5
How to manage static buffers 2
Copy string from 2D array to a 1D array in C 1
simple UnZip 5
String buffer overruns? 8

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top