Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
268e4d4c
Commit
268e4d4c
authored
Oct 14, 2010
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#1710703: write zipfile structures also in the case of closing a new, but empty, archive.
parent
77658bd9
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
55 additions
and
5 deletions
+55
-5
Doc/library/zipfile.rst
Doc/library/zipfile.rst
+4
-0
Lib/test/test_zipfile.py
Lib/test/test_zipfile.py
+25
-0
Lib/zipfile.py
Lib/zipfile.py
+22
-5
Misc/NEWS
Misc/NEWS
+4
-0
No files found.
Doc/library/zipfile.rst
View file @
268e4d4c
...
@@ -117,6 +117,10 @@ ZipFile Objects
...
@@ -117,6 +117,10 @@ ZipFile Objects
and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support
and :program:`unzip` commands on Unix (the InfoZIP utilities) don't support
these extensions.
these extensions.
If the file is created with mode ``'a'`` or ``'w'`` and then
:meth:`close`\ d without adding any files to the archive, the appropriate
ZIP structures for an empty archive will be written to the file.
ZipFile is also a context manager and therefore supports the
ZipFile is also a context manager and therefore supports the
:keyword:`with` statement. In the example, *myzip* is closed after the
:keyword:`with` statement. In the example, *myzip* is closed after the
:keyword:`with` statement's suite is finished---even if an exception occurs::
:keyword:`with` statement's suite is finished---even if an exception occurs::
...
...
Lib/test/test_zipfile.py
View file @
268e4d4c
...
@@ -959,6 +959,31 @@ class OtherTests(unittest.TestCase):
...
@@ -959,6 +959,31 @@ class OtherTests(unittest.TestCase):
def
test_read_return_size_deflated
(
self
):
def
test_read_return_size_deflated
(
self
):
self
.
check_read_return_size
(
zipfile
.
ZIP_DEFLATED
)
self
.
check_read_return_size
(
zipfile
.
ZIP_DEFLATED
)
def
test_empty_zipfile
(
self
):
# Check that creating a file in 'w' or 'a' mode and closing without
# adding any files to the archives creates a valid empty ZIP file
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"w"
)
zipf
.
close
()
try
:
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"r"
)
except
zipfile
.
BadZipFile
:
self
.
fail
(
"Unable to create empty ZIP file in 'w' mode"
)
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"a"
)
zipf
.
close
()
try
:
zipf
=
zipfile
.
ZipFile
(
TESTFN
,
mode
=
"r"
)
except
:
self
.
fail
(
"Unable to create empty ZIP file in 'a' mode"
)
def
test_open_empty_file
(
self
):
# Issue 1710703: Check that opening a file with less than 22 bytes
# raises a BadZipfile exception (rather than the previously unhelpful
# IOError)
f
=
open
(
TESTFN
,
'w'
)
f
.
close
()
self
.
assertRaises
(
zipfile
.
BadZipfile
,
zipfile
.
ZipFile
,
TESTFN
,
'r'
)
def
tearDown
(
self
):
def
tearDown
(
self
):
unlink
(
TESTFN
)
unlink
(
TESTFN
)
unlink
(
TESTFN2
)
unlink
(
TESTFN2
)
...
...
Lib/zipfile.py
View file @
268e4d4c
...
@@ -167,7 +167,13 @@ def _EndRecData64(fpin, offset, endrec):
...
@@ -167,7 +167,13 @@ def _EndRecData64(fpin, offset, endrec):
"""
"""
Read the ZIP64 end-of-archive records and use that to update endrec
Read the ZIP64 end-of-archive records and use that to update endrec
"""
"""
try
:
fpin
.
seek
(
offset
-
sizeEndCentDir64Locator
,
2
)
fpin
.
seek
(
offset
-
sizeEndCentDir64Locator
,
2
)
except
IOError
:
# If the seek fails, the file is not large enough to contain a ZIP64
# end-of-archive record, so just return the end record we were given.
return
endrec
data
=
fpin
.
read
(
sizeEndCentDir64Locator
)
data
=
fpin
.
read
(
sizeEndCentDir64Locator
)
sig
,
diskno
,
reloff
,
disks
=
struct
.
unpack
(
structEndArchive64Locator
,
data
)
sig
,
diskno
,
reloff
,
disks
=
struct
.
unpack
(
structEndArchive64Locator
,
data
)
if
sig
!=
stringEndArchive64Locator
:
if
sig
!=
stringEndArchive64Locator
:
...
@@ -705,14 +711,22 @@ class ZipFile:
...
@@ -705,14 +711,22 @@ class ZipFile:
if
key
==
'r'
:
if
key
==
'r'
:
self
.
_GetContents
()
self
.
_GetContents
()
elif
key
==
'w'
:
elif
key
==
'w'
:
pass
# set the modified flag so central directory gets written
# even if no files are added to the archive
self
.
_didModify
=
True
elif
key
==
'a'
:
elif
key
==
'a'
:
try
:
# See if file is a zip file
try
:
# See if file is a zip file
self
.
_RealGetContents
()
self
.
_RealGetContents
()
# seek to start of directory and overwrite
# seek to start of directory and overwrite
self
.
fp
.
seek
(
self
.
start_dir
,
0
)
self
.
fp
.
seek
(
self
.
start_dir
,
0
)
except
BadZipfile
:
# file is not a zip file, just append
except
BadZipfile
:
# file is not a zip file, just append
self
.
fp
.
seek
(
0
,
2
)
self
.
fp
.
seek
(
0
,
2
)
# set the modified flag so central directory gets written
# even if no files are added to the archive
self
.
_didModify
=
True
else
:
else
:
if
not
self
.
_filePassed
:
if
not
self
.
_filePassed
:
self
.
fp
.
close
()
self
.
fp
.
close
()
...
@@ -739,7 +753,10 @@ class ZipFile:
...
@@ -739,7 +753,10 @@ class ZipFile:
def
_RealGetContents
(
self
):
def
_RealGetContents
(
self
):
"""Read in the table of contents for the ZIP file."""
"""Read in the table of contents for the ZIP file."""
fp
=
self
.
fp
fp
=
self
.
fp
try
:
endrec
=
_EndRecData
(
fp
)
endrec
=
_EndRecData
(
fp
)
except
IOError
:
raise
BadZipfile
(
"File is not a zip file"
)
if
not
endrec
:
if
not
endrec
:
raise
BadZipfile
(
"File is not a zip file"
)
raise
BadZipfile
(
"File is not a zip file"
)
if
self
.
debug
>
1
:
if
self
.
debug
>
1
:
...
...
Misc/NEWS
View file @
268e4d4c
...
@@ -15,6 +15,10 @@ Core and Builtins
...
@@ -15,6 +15,10 @@ Core and Builtins
Library
Library
-------
-------
- Issue #1710703: Write structures for an empty ZIP archive when a ZipFile is
created in modes 'a' or 'w' and then closed without adding any files. Raise
BadZipfile (rather than IOError) when opening small non-ZIP files.
- Issue #10041: The signature of optional arguments in socket.makefile()
- Issue #10041: The signature of optional arguments in socket.makefile()
didn't match that of io.open(), and they also didn't get forwarded
didn't match that of io.open(), and they also didn't get forwarded
properly to TextIOWrapper in text mode. Patch by Kai Zhu.
properly to TextIOWrapper in text mode. Patch by Kai Zhu.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment