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
Weird Behavior with Rays in C and OpenGL 4
String Buffers? 5
How to manage static buffers 2
Copy string from 2D array to a 1D array in C 1
simple UnZip 5

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top