Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 42db3ef

Browse files
committed
Merged revisions 68319 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r68319 | antoine.pitrou | 2009-01-04 22:29:23 +0100 (dim., 04 janv. 2009) | 3 lines Issue #4272: Add an optional argument to the GzipFile constructor to override the timestamp in the gzip stream. ........
1 parent 315a20a commit 42db3ef

4 files changed

Lines changed: 95 additions & 5 deletions

File tree

Doc/library/gzip.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ For other archive formats, see the :mod:`bz2`, :mod:`zipfile`, and
2424
The module defines the following items:
2525

2626

27-
.. class:: GzipFile([filename[, mode[, compresslevel[, fileobj]]]])
27+
.. class:: GzipFile([filename[, mode[, compresslevel[, fileobj[, mtime]]]]])
2828

2929
Constructor for the :class:`GzipFile` class, which simulates most of the methods
3030
of a file object, with the exception of the :meth:`readinto` and
@@ -52,6 +52,15 @@ The module defines the following items:
5252
level of compression; ``1`` is fastest and produces the least compression, and
5353
``9`` is slowest and produces the most compression. The default is ``9``.
5454

55+
The *mtime* argument is an optional numeric timestamp to be written to
56+
the stream when compressing. All :program:`gzip`compressed streams are
57+
required to contain a timestamp. If omitted or ``None``, the current
58+
time is used. This module ignores the timestamp when decompressing;
59+
however, some programs, such as :program:`gunzip`\ , make use of it.
60+
The format of the timestamp is the same as that of the return value of
61+
``time.time()`` and of the ``st_mtime`` member of the object returned
62+
by ``os.stat()``.
63+
5564
Calling a :class:`GzipFile` object's :meth:`close` method does not close
5665
*fileobj*, since you might wish to append more material after the compressed
5766
data. This also allows you to pass a :class:`StringIO` object opened for

Lib/gzip.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class GzipFile:
5454
max_read_chunk = 10 * 1024 * 1024 # 10Mb
5555

5656
def __init__(self, filename=None, mode=None,
57-
compresslevel=9, fileobj=None):
57+
compresslevel=9, fileobj=None, mtime=None):
5858
"""Constructor for the GzipFile class.
5959
6060
At least one of fileobj and filename must be given a
@@ -81,6 +81,15 @@ def __init__(self, filename=None, mode=None,
8181
level of compression; 1 is fastest and produces the least compression,
8282
and 9 is slowest and produces the most compression. The default is 9.
8383
84+
The mtime argument is an optional numeric timestamp to be written
85+
to the stream when compressing. All gzip compressed streams
86+
are required to contain a timestamp. If omitted or None, the
87+
current time is used. This module ignores the timestamp when
88+
decompressing; however, some programs, such as gunzip, make use
89+
of it. The format of the timestamp is the same as that of the
90+
return value of time.time() and of the st_mtime member of the
91+
object returned by os.stat().
92+
8493
"""
8594

8695
# guarantee the file is opened in binary mode on platforms
@@ -119,6 +128,7 @@ def __init__(self, filename=None, mode=None,
119128

120129
self.fileobj = fileobj
121130
self.offset = 0
131+
self.mtime = mtime
122132

123133
if self.mode == WRITE:
124134
self._write_gzip_header()
@@ -157,7 +167,10 @@ def _write_gzip_header(self):
157167
if fname:
158168
flags = FNAME
159169
self.fileobj.write(chr(flags).encode('latin-1'))
160-
write32u(self.fileobj, int(time.time()))
170+
mtime = self.mtime
171+
if mtime is None:
172+
mtime = time.time()
173+
write32u(self.fileobj, int(mtime))
161174
self.fileobj.write(b'\002')
162175
self.fileobj.write(b'\377')
163176
if fname:
@@ -175,10 +188,10 @@ def _read_gzip_header(self):
175188
if method != 8:
176189
raise IOError('Unknown compression method')
177190
flag = ord( self.fileobj.read(1) )
178-
# modtime = self.fileobj.read(4)
191+
self.mtime = read32(self.fileobj)
179192
# extraflag = self.fileobj.read(1)
180193
# os = self.fileobj.read(1)
181-
self.fileobj.read(6)
194+
self.fileobj.read(2)
182195

183196
if flag & FEXTRA:
184197
# Read & discard the extra field, if present

Lib/test/test_gzip.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from test import support
77
import os
88
import gzip
9+
import struct
910

1011

1112
data1 = b""" int length=DEFAULTALLOC, err = Z_OK;
@@ -160,6 +161,68 @@ def test_1647484(self):
160161
self.assertEqual(f.name, self.filename)
161162
f.close()
162163

164+
def test_mtime(self):
165+
mtime = 123456789
166+
fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
167+
fWrite.write(data1)
168+
fWrite.close()
169+
170+
fRead = gzip.GzipFile(self.filename)
171+
dataRead = fRead.read()
172+
self.assertEqual(dataRead, data1)
173+
self.assert_(hasattr(fRead, 'mtime'))
174+
self.assertEqual(fRead.mtime, mtime)
175+
fRead.close()
176+
177+
def test_metadata(self):
178+
mtime = 123456789
179+
180+
fWrite = gzip.GzipFile(self.filename, 'w', mtime = mtime)
181+
fWrite.write(data1)
182+
fWrite.close()
183+
184+
fRead = open(self.filename, 'rb')
185+
186+
# see RFC 1952: http://www.faqs.org/rfcs/rfc1952.html
187+
188+
idBytes = fRead.read(2)
189+
self.assertEqual(idBytes, b'\x1f\x8b') # gzip ID
190+
191+
cmByte = fRead.read(1)
192+
self.assertEqual(cmByte, b'\x08') # deflate
193+
194+
flagsByte = fRead.read(1)
195+
self.assertEqual(flagsByte, b'\x08') # only the FNAME flag is set
196+
197+
mtimeBytes = fRead.read(4)
198+
self.assertEqual(mtimeBytes, struct.pack('<i', mtime)) # little-endian
199+
200+
xflByte = fRead.read(1)
201+
self.assertEqual(xflByte, b'\x02') # maximum compression
202+
203+
osByte = fRead.read(1)
204+
self.assertEqual(osByte, b'\xff') # OS "unknown" (OS-independent)
205+
206+
# Since the FNAME flag is set, the zero-terminated filename follows.
207+
# RFC 1952 specifies that this is the name of the input file, if any.
208+
# However, the gzip module defaults to storing the name of the output
209+
# file in this field.
210+
expected = self.filename.encode('Latin-1') + b'\x00'
211+
nameBytes = fRead.read(len(expected))
212+
self.assertEqual(nameBytes, expected)
213+
214+
# Since no other flags were set, the header ends here.
215+
# Rather than process the compressed data, let's seek to the trailer.
216+
fRead.seek(os.stat(self.filename).st_size - 8)
217+
218+
crc32Bytes = fRead.read(4) # CRC32 of uncompressed data [data1]
219+
self.assertEqual(crc32Bytes, b'\xaf\xd7d\x83')
220+
221+
isizeBytes = fRead.read(4)
222+
self.assertEqual(isizeBytes, struct.pack('<i', len(data1)))
223+
224+
fRead.close()
225+
163226
def test_main(verbose=None):
164227
support.run_unittest(TestGzip)
165228

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,11 @@ Core and Builtins
8282
Library
8383
-------
8484

85+
- Issue #4272: Add an optional argument to the GzipFile constructor to override
86+
the timestamp in the gzip stream. The default value remains the current time.
87+
The information can be used by e.g. gunzip when decompressing. Patch by
88+
Jacques Frechet.
89+
8590
- Restore Python 2.3 compatibility for decimal.py.
8691

8792
- Issue #3638: Remove functions from _tkinter module level that depend on

0 commit comments

Comments
 (0)