summaryrefslogtreecommitdiffstats
path: root/Lib/zipfile.py
diff options
context:
space:
mode:
authorAntoine Pitrou <solipsis@pitrou.net>2008-12-27 15:43:12 (GMT)
committerAntoine Pitrou <solipsis@pitrou.net>2008-12-27 15:43:12 (GMT)
commit6f193e0e959432f2227c0380b970a82f58c8a0c8 (patch)
tree95cc667e9098a337288573b4e37ea93a4d0c7fdd /Lib/zipfile.py
parente57e9990e72f83963a34c37c985bc72c5f27eae5 (diff)
downloadcpython-6f193e0e959432f2227c0380b970a82f58c8a0c8.zip
cpython-6f193e0e959432f2227c0380b970a82f58c8a0c8.tar.gz
cpython-6f193e0e959432f2227c0380b970a82f58c8a0c8.tar.bz2
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.py26
1 files changed, 19 insertions, 7 deletions
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index bb8fc53..66fe078 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -128,18 +128,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 = 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