Skip to content

Commit f607e5f

Browse files
committed
Merge pull request #12 from allanlei/bug-7
Bug 7
2 parents 771f7ea + 3d0d364 commit f607e5f

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

example.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
z.write('LICENSE')
99
z.write('LICENSE', arcname='stuff/LICENSE')
1010

11+
with open('tests/sample.rtf', 'rb') as fp:
12+
z.writestr('sample.rtf', fp.read())
13+
1114
for root, directories, files in os.walk('zipstream'):
1215
for filename in files:
1316
path = os.path.join(root, filename)

tests/sample.rtf

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{\rtf1\ansi\deff3\adeflang1025
2+
{\fonttbl{\f0\froman\fprq2\fcharset0 Times New Roman;}{\f1\froman\fprq2\fcharset2 Symbol;}{\f2\fswiss\fprq2\fcharset0 Arial;}{\f3\froman\fprq2\fcharset0 Liberation Serif{\*\falt Times New Roman};}{\f4\fswiss\fprq2\fcharset0 Liberation Sans{\*\falt Arial};}{\f5\fmodern\fprq1\fcharset0 Liberation Mono{\*\falt Courier New};}{\f6\fnil\fprq2\fcharset0 Droid Sans Fallback;}{\f7\fmodern\fprq1\fcharset0 Courier New;}{\f8\fnil\fprq2\fcharset0 FreeSans;}{\f9\fswiss\fprq0\fcharset128 FreeSans;}}
3+
{\colortbl;\red0\green0\blue0;\red0\green0\blue255;\red0\green255\blue255;\red0\green255\blue0;\red255\green0\blue255;\red255\green0\blue0;\red255\green255\blue0;\red255\green255\blue255;\red0\green0\blue128;\red0\green128\blue128;\red0\green128\blue0;\red128\green0\blue128;\red128\green0\blue0;\red128\green128\blue0;\red128\green128\blue128;\red192\green192\blue192;}
4+
{\stylesheet{\s0\snext0\nowidctlpar\hyphpar0\cf0\dbch\af6\langfe2052\dbch\af8\afs24\alang1081\loch\f3\fs24\lang4105 Normal;}
5+
{\s1\sbasedon15\snext16\ilvl0\outlinelevel0\sb240\sa120\keepn\b\dbch\af6\dbch\af8\afs36\ab\loch\f4\fs36 Heading 1;}
6+
{\s2\sbasedon15\snext16\ilvl1\outlinelevel1\sb200\sa120\keepn\b\dbch\af6\dbch\af8\afs32\ab\loch\f4\fs32 Heading 2;}
7+
{\s3\sbasedon15\snext16\ilvl2\outlinelevel2\sb140\sa120\keepn\b\dbch\af6\dbch\af8\afs28\ab\loch\f4\fs28 Heading 3;}
8+
{\s15\sbasedon0\snext16\sb240\sa120\keepn\dbch\af6\dbch\af8\afs28\loch\f4\fs28 Heading;}
9+
{\s16\sbasedon0\snext16\sl288\slmult1\sb0\sa140 Text Body;}
10+
{\s17\sbasedon16\snext17\sl288\slmult1\sb0\sa140\dbch\af9 List;}
11+
{\s18\sbasedon0\snext18\sb120\sa120\noline\i\dbch\af9\afs24\ai\fs24 Caption;}
12+
{\s19\sbasedon0\snext19\noline\dbch\af9 Index;}
13+
{\s20\sbasedon0\snext20\sb0\sa0\dbch\af7\dbch\af5\afs20\loch\f5\fs20 Preformatted Text;}
14+
{\s21\sbasedon0\snext21\li567\ri567\lin567\rin567\fi0\sb0\sa283 Quotations;}
15+
{\s22\sbasedon15\snext16\qc\sb240\sa120\keepn\b\dbch\af6\dbch\af8\afs56\ab\loch\f4\fs56 Title;}
16+
{\s23\sbasedon15\snext16\qc\sb60\sa120\keepn\dbch\af6\dbch\af8\afs36\loch\f4\fs36 Subtitle;}
17+
}{\*\listtable{\list\listtemplateid1
18+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-432\li432}
19+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-576\li576}
20+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-720\li720}
21+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-864\li864}
22+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-1008\li1008}
23+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-1152\li1152}
24+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-1296\li1296}
25+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-1440\li1440}
26+
{\listlevel\levelnfc255\leveljc0\levelstartat1\levelfollow2{\leveltext \'00;}{\levelnumbers;}\fi-1584\li1584}\listid1}
27+
}{\listoverridetable{\listoverride\listid1\listoverridecount0\ls1}}{\*\generator LibreOffice/5.0.2.2$Linux_X86_64 LibreOffice_project/00m0$Build-2}{\info{\creatim\yr0\mo0\dy0\hr0\min0}{\revtim\yr2015\mo9\dy29\hr20\min12}{\printim\yr0\mo0\dy0\hr0\min0}}\deftab709
28+
29+
{\*\pgdsctbl
30+
{\pgdsc0\pgdscuse451\pgwsxn12240\pghsxn15840\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\pgdscnxt0 Default Style;}}
31+
\formshade\paperh15840\paperw12240\margl1134\margr1134\margt1134\margb1134\sectd\sbknone\sectunlocked1\pgndec\pgwsxn12240\pghsxn15840\marglsxn1134\margrsxn1134\margtsxn1134\margbsxn1134\ftnbj\ftnstart1\ftnrstcont\ftnnar\aenddoc\aftnrstcont\aftnstart1\aftnnrlc
32+
{\*\ftnsep\chftnsep}\pgndec\pard\plain \s20\sb0\sa0\dbch\af7\dbch\af5\afs20\loch\f5\fs20{\rtlch \ltrch\loch
33+
Hello World}
34+
\par }

tests/test_zipstream.py

+18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import zipfile
99

1010

11+
SAMPLE_FILE_RTF = 'tests/sample.rtf'
12+
13+
1114
class ZipInfoTestCase(unittest.TestCase):
1215
pass
1316

@@ -73,6 +76,21 @@ def string_generator():
7376

7477
os.remove(f.name)
7578

79+
def test_writestr(self):
80+
z = zipstream.ZipFile(mode='w')
81+
82+
with open(SAMPLE_FILE_RTF, 'rb') as fp:
83+
z.writestr('sample.rtf', fp.read())
84+
85+
f = tempfile.NamedTemporaryFile(suffix='zip', delete=False)
86+
for chunk in z:
87+
f.write(chunk)
88+
f.close()
89+
90+
z2 = zipfile.ZipFile(f.name, 'r')
91+
self.assertFalse(z2.testzip())
92+
93+
os.remove(f.name)
7694

7795
def test_write_iterable_no_archive(self):
7896
z = zipstream.ZipFile(mode='w')

zipstream/__init__.py

+8
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,14 @@ def write_iter(self, arcname, iterable, compress_type=None):
220220
kwargs = {'arcname': arcname, 'iterable': iterable, 'compress_type': compress_type}
221221
self.paths_to_write.append(kwargs)
222222

223+
def writestr(self, arcname, data, compress_type=None):
224+
"""
225+
Writes a str into ZipFile by wrapping data as a generator
226+
"""
227+
def _iterable():
228+
yield data
229+
return self.write_iter(arcname, _iterable(), compress_type=compress_type)
230+
223231
def __write(self, filename=None, iterable=None, arcname=None, compress_type=None):
224232
"""Put the bytes from filename into the archive under the name
225233
`arcname`."""

0 commit comments

Comments
 (0)