Commit ee5c885f authored by Ronald Oussoren's avatar Ronald Oussoren

Merged revisions 78097 via svnmerge from

svn+ssh://pythondev@svn.python.org/python/trunk

........
  r78097 | ronald.oussoren | 2010-02-07 21:18:02 +0100 (Sun, 07 Feb 2010) | 2 lines

  Issue 6003: ZipFile.writestr "compression_type" argument
........
parent 755740f3
...@@ -267,7 +267,7 @@ ZipFile Objects ...@@ -267,7 +267,7 @@ ZipFile Objects
byte, the name of the file in the archive will be truncated at the null byte. byte, the name of the file in the archive will be truncated at the null byte.
.. method:: ZipFile.writestr(zinfo_or_arcname, bytes) .. method:: ZipFile.writestr(zinfo_or_arcname, bytes[, compress_type])
Write the string *bytes* to the archive; *zinfo_or_arcname* is either the file Write the string *bytes* to the archive; *zinfo_or_arcname* is either the file
name it will be given in the archive, or a :class:`ZipInfo` instance. If it's name it will be given in the archive, or a :class:`ZipInfo` instance. If it's
...@@ -277,6 +277,10 @@ ZipFile Objects ...@@ -277,6 +277,10 @@ ZipFile Objects
created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling created with mode ``'r'`` will raise a :exc:`RuntimeError`. Calling
:meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`. :meth:`writestr` on a closed ZipFile will raise a :exc:`RuntimeError`.
If given, *compress_type* overrides the value given for the *compression*
parameter to the constructor for the new entry, or in the *zinfo_or_arcname*
(if that is a :class:`ZipInfo` instance).
.. note:: .. note::
When passing a :class:`ZipInfo` instance as the *zinfo_or_acrname* parameter, When passing a :class:`ZipInfo` instance as the *zinfo_or_acrname* parameter,
...@@ -284,6 +288,9 @@ ZipFile Objects ...@@ -284,6 +288,9 @@ ZipFile Objects
member of the given :class:`ZipInfo` instance. By default, the member of the given :class:`ZipInfo` instance. By default, the
:class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`. :class:`ZipInfo` constructor sets this member to :const:`ZIP_STORED`.
.. versionchanged:: 2.7
The *compression_type* argument.
The following data attributes are also available: The following data attributes are also available:
......
...@@ -407,6 +407,20 @@ class TestsWithSourceFile(unittest.TestCase): ...@@ -407,6 +407,20 @@ class TestsWithSourceFile(unittest.TestCase):
# remove the test file subdirectories # remove the test file subdirectories
shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir')) shutil.rmtree(os.path.join(os.getcwd(), 'ziptest2dir'))
def test_writestr_compression(self):
zipfp = zipfile.ZipFile(TESTFN2, "w")
zipfp.writestr("a.txt", "hello world", compress_type=zipfile.ZIP_STORED)
if zlib:
zipfp.writestr("b.txt", "hello world", compress_type=zipfile.ZIP_DEFLATED)
info = zipfp.getinfo('a.txt')
self.assertEqual(info.compress_type, zipfile.ZIP_STORED)
if zlib:
info = zipfp.getinfo('b.txt')
self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED)
def zip_test_writestr_permissions(self, f, compression): def zip_test_writestr_permissions(self, f, compression):
# Make sure that writestr creates files with mode 0600, # Make sure that writestr creates files with mode 0600,
# when it is passed a name rather than a ZipInfo instance. # when it is passed a name rather than a ZipInfo instance.
......
...@@ -1065,7 +1065,7 @@ class ZipFile: ...@@ -1065,7 +1065,7 @@ class ZipFile:
self.filelist.append(zinfo) self.filelist.append(zinfo)
self.NameToInfo[zinfo.filename] = zinfo self.NameToInfo[zinfo.filename] = zinfo
def writestr(self, zinfo_or_arcname, data): def writestr(self, zinfo_or_arcname, data, compress_type=None):
"""Write a file into the archive. The contents is 'data', which """Write a file into the archive. The contents is 'data', which
may be either a 'str' or a 'bytes' instance; if it is a 'str', may be either a 'str' or a 'bytes' instance; if it is a 'str',
it is encoded as UTF-8 first. it is encoded as UTF-8 first.
...@@ -1087,6 +1087,9 @@ class ZipFile: ...@@ -1087,6 +1087,9 @@ class ZipFile:
zinfo.file_size = len(data) # Uncompressed size zinfo.file_size = len(data) # Uncompressed size
zinfo.header_offset = self.fp.tell() # Start of header data zinfo.header_offset = self.fp.tell() # Start of header data
if compress_type is not None:
zinfo.compress_type = compress_type
self._writecheck(zinfo) self._writecheck(zinfo)
self._didModify = True self._didModify = True
zinfo.CRC = crc32(data) & 0xffffffff # CRC-32 checksum zinfo.CRC = crc32(data) & 0xffffffff # CRC-32 checksum
......
...@@ -242,6 +242,9 @@ C-API ...@@ -242,6 +242,9 @@ C-API
Library Library
------- -------
- Issue #6003: add an argument to ``zipfile.Zipfile.writestr`` to
specify the compression type.
- Issue #4772: Raise a ValueError when an unknown Bluetooth protocol is - Issue #4772: Raise a ValueError when an unknown Bluetooth protocol is
specified, rather than fall through to AF_PACKET (in the `socket` module). specified, rather than fall through to AF_PACKET (in the `socket` module).
Also, raise ValueError rather than TypeError when an unknown TIPC address Also, raise ValueError rather than TypeError when an unknown TIPC address
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment