Skip to content

Commit 0a6f02a

Browse files
author
Arjan Schrijver
committed
Merge pull request #8 from okmetti/add_datetime_to_write_iter
2 parents 58bfa8e + a2b5b8f commit 0a6f02a

3 files changed

Lines changed: 40 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
This file details the changes that were made after forking v1.1.4 from https://github.com/allanlei/python-zipstream.
44

5-
## v1.1.5 (2019-03-18)
6-
* Support Zip64 when compressing iterables and strings (https://github.com/allanlei/python-zipstream/pull/25)
5+
## Unreleased
6+
* New datetime parameter in write_iter (https://github.com/arjan-s/python-zipstream/pull/8)
7+
8+
## v1.1.7 (2019-10-22)
9+
* Stream data in the order it was received (https://github.com/arjan-s/python-zipstream/pull/4)
710

811
## v1.1.6 (2019-06-06)
912
* Add partial flushing of ZipStreams (https://github.com/arjan-s/python-zipstream/pull/1)
1013

11-
## v1.1.7 (2019-10-22)
12-
* Stream data in the order it was received (https://github.com/arjan-s/python-zipstream/pull/4)
14+
## v1.1.5 (2019-03-18)
15+
* Support Zip64 when compressing iterables and strings (https://github.com/allanlei/python-zipstream/pull/25)

tests/test_zipstream.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import tempfile
6+
import time
67
import unittest
78
import zipstream
89
import zipfile
@@ -76,6 +77,28 @@ def string_generator():
7677

7778
os.remove(f.name)
7879

80+
def test_write_iterable_with_date_time(self):
81+
file_name_in_zip = "data_datetime"
82+
file_date_time_in_zip = time.strptime("2011-04-19 22:30:21", "%Y-%m-%d %H:%M:%S")
83+
84+
z = zipstream.ZipFile(mode='w')
85+
def string_generator():
86+
for _ in range(10):
87+
yield b'zipstream\x01\n'
88+
z.write_iter(iterable=string_generator(), arcname=file_name_in_zip, date_time=file_date_time_in_zip)
89+
90+
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
91+
for chunk in z:
92+
f.write(chunk)
93+
f.close()
94+
95+
z2 = zipfile.ZipFile(f.name, 'r')
96+
self.assertFalse(z2.testzip())
97+
98+
self.assertEqual(file_date_time_in_zip[0:5], z2.getinfo(file_name_in_zip).date_time[0:5])
99+
100+
os.remove(f.name)
101+
79102
def test_writestr(self):
80103
z = zipstream.ZipFile(mode='w')
81104

zipstream/__init__.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,20 +220,20 @@ def write(self, filename, arcname=None, compress_type=None):
220220
kwargs = {'filename': filename, 'arcname': arcname, 'compress_type': compress_type}
221221
self.paths_to_write.append(kwargs)
222222

223-
def write_iter(self, arcname, iterable, compress_type=None, buffer_size=None):
223+
def write_iter(self, arcname, iterable, compress_type=None, buffer_size=None, date_time=None):
224224
"""Write the bytes iterable `iterable` to the archive under the name `arcname`."""
225-
kwargs = {'arcname': arcname, 'iterable': iterable, 'compress_type': compress_type, 'buffer_size': buffer_size}
225+
kwargs = {'arcname': arcname, 'iterable': iterable, 'compress_type': compress_type, 'buffer_size': buffer_size, 'date_time': date_time}
226226
self.paths_to_write.append(kwargs)
227227

228-
def writestr(self, arcname, data, compress_type=None, buffer_size=None):
228+
def writestr(self, arcname, data, compress_type=None, buffer_size=None, date_time=None):
229229
"""
230230
Writes a str into ZipFile by wrapping data as a generator
231231
"""
232232
def _iterable():
233233
yield data
234-
return self.write_iter(arcname, _iterable(), compress_type=compress_type, buffer_size=buffer_size)
234+
return self.write_iter(arcname, _iterable(), compress_type=compress_type, buffer_size=buffer_size, date_time=date_time)
235235

236-
def __write(self, filename=None, iterable=None, arcname=None, compress_type=None, buffer_size=None):
236+
def __write(self, filename=None, iterable=None, arcname=None, compress_type=None, buffer_size=None, date_time=None):
237237
"""Put the bytes from filename into the archive under the name
238238
`arcname`."""
239239
if not self.fp:
@@ -248,7 +248,11 @@ def __write(self, filename=None, iterable=None, arcname=None, compress_type=None
248248
mtime = time.localtime(st.st_mtime)
249249
date_time = mtime[0:6]
250250
else:
251-
st, isdir, date_time = None, False, time.localtime()[0:6]
251+
st, isdir = None, False
252+
if date_time is not None and isinstance(date_time, time.struct_time):
253+
date_time = date_time[0:6]
254+
if date_time is None:
255+
date_time = time.localtime()[0:6]
252256
# Create ZipInfo instance to store file information
253257
if arcname is None:
254258
arcname = filename

0 commit comments

Comments
 (0)