summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGeorg Brandl <georg@python.org>2008-05-20 08:25:48 (GMT)
committerGeorg Brandl <georg@python.org>2008-05-20 08:25:48 (GMT)
commit112aa5032985925608844d7421e12703afd6ce41 (patch)
treeb7e26da27c90471192c42afea278b8b9f59ef9f3 /Lib
parent4dd019fde36b53b30e69a2ce9cee3f2f9f25c2a6 (diff)
downloadcpython-112aa5032985925608844d7421e12703afd6ce41.zip
cpython-112aa5032985925608844d7421e12703afd6ce41.tar.gz
cpython-112aa5032985925608844d7421e12703afd6ce41.tar.bz2
Patch #1775025: allow opening zipfile members via ZipInfo instances.
Patch by Graham Horler.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_zipfile.py19
-rw-r--r--Lib/zipfile.py13
2 files changed, 27 insertions, 5 deletions
diff --git a/Lib/test/test_zipfile.py b/Lib/test/test_zipfile.py
index c83622a..4bc2104 100644
--- a/Lib/test/test_zipfile.py
+++ b/Lib/test/test_zipfile.py
@@ -132,6 +132,25 @@ class TestsWithSourceFile(unittest.TestCase):
for f in (TESTFN2, TemporaryFile(), StringIO()):
self.zipOpenTest(f, zipfile.ZIP_STORED)
+ def testOpenViaZipInfo(self):
+ # Create the ZIP archive
+ zipfp = zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED)
+ zipfp.writestr("name", "foo")
+ zipfp.writestr("name", "bar")
+ zipfp.close()
+
+ zipfp = zipfile.ZipFile(TESTFN2, "r")
+ infos = zipfp.infolist()
+ data = ""
+ for info in infos:
+ data += zipfp.open(info).read()
+ self.assert_(data == "foobar" or data == "barfoo")
+ data = ""
+ for info in infos:
+ data += zipfp.read(info)
+ self.assert_(data == "foobar" or data == "barfoo")
+ zipfp.close()
+
def zipRandomOpenTest(self, f, compression):
self.makeTestArchive(f, compression)
diff --git a/Lib/zipfile.py b/Lib/zipfile.py
index 33fcfb1..b812a82 100644
--- a/Lib/zipfile.py
+++ b/Lib/zipfile.py
@@ -776,10 +776,13 @@ class ZipFile:
else:
zef_file = open(self.filename, 'rb')
- # Get info object for name
- zinfo = self.getinfo(name)
-
- filepos = zef_file.tell()
+ # Make sure we have an info object
+ if isinstance(name, ZipInfo):
+ # 'name' is already an info object
+ zinfo = name
+ else:
+ # Get info object for name
+ zinfo = self.getinfo(name)
zef_file.seek(zinfo.header_offset, 0)
@@ -884,7 +887,7 @@ class ZipFile:
if upperdirs and not os.path.exists(upperdirs):
os.makedirs(upperdirs)
- source = self.open(member.filename, pwd=pwd)
+ source = self.open(member, pwd=pwd)
target = file(targetpath, "wb")
shutil.copyfileobj(source, target)
source.close()