diff options
author | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-27 15:50:40 (GMT) |
---|---|---|
committer | Antoine Pitrou <solipsis@pitrou.net> | 2008-12-27 15:50:40 (GMT) |
commit | db5fe667311745a2bc86355620354449721545f3 (patch) | |
tree | a33cb5639ac9b87911c0a934ba5a42737c3df671 /Lib/zipfile.py | |
parent | d88e8fab148359a85f8e2a110a850c74e8ed5244 (diff) | |
download | cpython-db5fe667311745a2bc86355620354449721545f3.zip cpython-db5fe667311745a2bc86355620354449721545f3.tar.gz cpython-db5fe667311745a2bc86355620354449721545f3.tar.bz2 |
Merged revisions 67946 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r67946 | antoine.pitrou | 2008-12-27 16:43:12 +0100 (sam., 27 déc. 2008) | 4 lines
Issue #4756: zipfile.is_zipfile() now supports file-like objects.
Patch by Gabriel Genellina.
........
Diffstat (limited to 'Lib/zipfile.py')
-rw-r--r-- | Lib/zipfile.py | 26 |
1 files changed, 19 insertions, 7 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py index 46ec6ef..5054097 100644 --- a/Lib/zipfile.py +++ b/Lib/zipfile.py @@ -130,18 +130,30 @@ _CD64_NUMBER_ENTRIES_TOTAL = 7 _CD64_DIRECTORY_SIZE = 8 _CD64_OFFSET_START_CENTDIR = 9 -def is_zipfile(filename): - """Quickly see if file is a ZIP file by checking the magic number.""" +def _check_zipfile(fp): try: - fpin = io.open(filename, "rb") - endrec = _EndRecData(fpin) - fpin.close() - if endrec: - return True # file has correct magic number + if _EndRecData(fp): + return True # file has correct magic number except IOError: pass return False +def is_zipfile(filename): + """Quickly see if a file is a ZIP file by checking the magic number. + + The filename argument may be a file or file-like object too. + """ + 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 + def _EndRecData64(fpin, offset, endrec): """ Read the ZIP64 end-of-archive records and use that to update endrec |