mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Itamar Shtull-Trauring <itamar@maxnm.com>:
Add support to zipfile to support opening an archive represented by an open file rather than a file name.
This commit is contained in:
parent
fc31f2692f
commit
3d9091ece1
2 changed files with 58 additions and 18 deletions
|
@ -1,24 +1,41 @@
|
|||
import zipfile, os
|
||||
import zipfile, os, StringIO, tempfile
|
||||
from test_support import TestFailed
|
||||
|
||||
srcname = "junk9630.tmp"
|
||||
zipname = "junk9708.tmp"
|
||||
|
||||
|
||||
def zipTest(f, compression, srccontents):
|
||||
zip = zipfile.ZipFile(f, "w", compression) # Create the ZIP archive
|
||||
zip.write(srcname, "another.name")
|
||||
zip.write(srcname, srcname)
|
||||
zip.close()
|
||||
|
||||
zip = zipfile.ZipFile(f, "r", compression) # Read the ZIP archive
|
||||
readData2 = zip.read(srcname)
|
||||
readData1 = zip.read("another.name")
|
||||
zip.close()
|
||||
|
||||
if readData1 != srccontents or readData2 != srccontents:
|
||||
raise TestFailed, "Written data doesn't equal read data."
|
||||
|
||||
|
||||
try:
|
||||
fp = open(srcname, "w") # Make a source file with some lines
|
||||
fp = open(srcname, "wb") # Make a source file with some lines
|
||||
for i in range(0, 1000):
|
||||
fp.write("Test of zipfile line %d.\n" % i)
|
||||
fp.close()
|
||||
|
||||
fp = open(srcname, "rb")
|
||||
writtenData = fp.read()
|
||||
fp.close()
|
||||
|
||||
for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
|
||||
zipTest(file, zipfile.ZIP_STORED, writtenData)
|
||||
|
||||
zip = zipfile.ZipFile(zipname, "w") # Create the ZIP archive
|
||||
zip.write(srcname, srcname)
|
||||
zip.write(srcname, "another.name")
|
||||
zip.close()
|
||||
for file in (zipname, tempfile.TemporaryFile(), StringIO.StringIO()):
|
||||
zipTest(file, zipfile.ZIP_DEFLATED, writtenData)
|
||||
|
||||
zip = zipfile.ZipFile(zipname, "r") # Read the ZIP archive
|
||||
zip.read("another.name")
|
||||
zip.read(srcname)
|
||||
zip.close()
|
||||
finally:
|
||||
if os.path.isfile(srcname): # Remove temporary files
|
||||
os.unlink(srcname)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue