test_zipfile.py 136 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489
  1. import array
  2. import contextlib
  3. import importlib.util
  4. import io
  5. import itertools
  6. import os
  7. import pathlib
  8. import posixpath
  9. import string
  10. import struct
  11. import subprocess
  12. import sys
  13. from test.support.script_helper import assert_python_ok
  14. import time
  15. import unittest
  16. import unittest.mock as mock
  17. import zipfile
  18. import functools
  19. from tempfile import TemporaryFile
  20. from random import randint, random, randbytes
  21. from test.support import script_helper
  22. from test.support import (
  23. findfile, requires_zlib, requires_bz2, requires_lzma,
  24. captured_stdout, captured_stderr, requires_subprocess
  25. )
  26. from test.support.os_helper import (
  27. TESTFN, unlink, rmtree, temp_dir, temp_cwd, fd_count
  28. )
  29. TESTFN2 = TESTFN + "2"
  30. TESTFNDIR = TESTFN + "d"
  31. FIXEDTEST_SIZE = 1000
  32. DATAFILES_DIR = 'zipfile_datafiles'
  33. SMALL_TEST_DATA = [('_ziptest1', '1q2w3e4r5t'),
  34. ('ziptest2dir/_ziptest2', 'qawsedrftg'),
  35. ('ziptest2dir/ziptest3dir/_ziptest3', 'azsxdcfvgb'),
  36. ('ziptest2dir/ziptest3dir/ziptest4dir/_ziptest3', '6y7u8i9o0p')]
  37. def get_files(test):
  38. yield TESTFN2
  39. with TemporaryFile() as f:
  40. yield f
  41. test.assertFalse(f.closed)
  42. with io.BytesIO() as f:
  43. yield f
  44. test.assertFalse(f.closed)
  45. class AbstractTestsWithSourceFile:
  46. @classmethod
  47. def setUpClass(cls):
  48. cls.line_gen = [bytes("Zipfile test line %d. random float: %f\n" %
  49. (i, random()), "ascii")
  50. for i in range(FIXEDTEST_SIZE)]
  51. cls.data = b''.join(cls.line_gen)
  52. def setUp(self):
  53. # Make a source file with some lines
  54. with open(TESTFN, "wb") as fp:
  55. fp.write(self.data)
  56. def make_test_archive(self, f, compression, compresslevel=None):
  57. kwargs = {'compression': compression, 'compresslevel': compresslevel}
  58. # Create the ZIP archive
  59. with zipfile.ZipFile(f, "w", **kwargs) as zipfp:
  60. zipfp.write(TESTFN, "another.name")
  61. zipfp.write(TESTFN, TESTFN)
  62. zipfp.writestr("strfile", self.data)
  63. with zipfp.open('written-open-w', mode='w') as f:
  64. for line in self.line_gen:
  65. f.write(line)
  66. def zip_test(self, f, compression, compresslevel=None):
  67. self.make_test_archive(f, compression, compresslevel)
  68. # Read the ZIP archive
  69. with zipfile.ZipFile(f, "r", compression) as zipfp:
  70. self.assertEqual(zipfp.read(TESTFN), self.data)
  71. self.assertEqual(zipfp.read("another.name"), self.data)
  72. self.assertEqual(zipfp.read("strfile"), self.data)
  73. # Print the ZIP directory
  74. fp = io.StringIO()
  75. zipfp.printdir(file=fp)
  76. directory = fp.getvalue()
  77. lines = directory.splitlines()
  78. self.assertEqual(len(lines), 5) # Number of files + header
  79. self.assertIn('File Name', lines[0])
  80. self.assertIn('Modified', lines[0])
  81. self.assertIn('Size', lines[0])
  82. fn, date, time_, size = lines[1].split()
  83. self.assertEqual(fn, 'another.name')
  84. self.assertTrue(time.strptime(date, '%Y-%m-%d'))
  85. self.assertTrue(time.strptime(time_, '%H:%M:%S'))
  86. self.assertEqual(size, str(len(self.data)))
  87. # Check the namelist
  88. names = zipfp.namelist()
  89. self.assertEqual(len(names), 4)
  90. self.assertIn(TESTFN, names)
  91. self.assertIn("another.name", names)
  92. self.assertIn("strfile", names)
  93. self.assertIn("written-open-w", names)
  94. # Check infolist
  95. infos = zipfp.infolist()
  96. names = [i.filename for i in infos]
  97. self.assertEqual(len(names), 4)
  98. self.assertIn(TESTFN, names)
  99. self.assertIn("another.name", names)
  100. self.assertIn("strfile", names)
  101. self.assertIn("written-open-w", names)
  102. for i in infos:
  103. self.assertEqual(i.file_size, len(self.data))
  104. # check getinfo
  105. for nm in (TESTFN, "another.name", "strfile", "written-open-w"):
  106. info = zipfp.getinfo(nm)
  107. self.assertEqual(info.filename, nm)
  108. self.assertEqual(info.file_size, len(self.data))
  109. # Check that testzip doesn't raise an exception
  110. zipfp.testzip()
  111. def test_basic(self):
  112. for f in get_files(self):
  113. self.zip_test(f, self.compression)
  114. def zip_open_test(self, f, compression):
  115. self.make_test_archive(f, compression)
  116. # Read the ZIP archive
  117. with zipfile.ZipFile(f, "r", compression) as zipfp:
  118. zipdata1 = []
  119. with zipfp.open(TESTFN) as zipopen1:
  120. while True:
  121. read_data = zipopen1.read(256)
  122. if not read_data:
  123. break
  124. zipdata1.append(read_data)
  125. zipdata2 = []
  126. with zipfp.open("another.name") as zipopen2:
  127. while True:
  128. read_data = zipopen2.read(256)
  129. if not read_data:
  130. break
  131. zipdata2.append(read_data)
  132. self.assertEqual(b''.join(zipdata1), self.data)
  133. self.assertEqual(b''.join(zipdata2), self.data)
  134. def test_open(self):
  135. for f in get_files(self):
  136. self.zip_open_test(f, self.compression)
  137. def test_open_with_pathlike(self):
  138. path = pathlib.Path(TESTFN2)
  139. self.zip_open_test(path, self.compression)
  140. with zipfile.ZipFile(path, "r", self.compression) as zipfp:
  141. self.assertIsInstance(zipfp.filename, str)
  142. def zip_random_open_test(self, f, compression):
  143. self.make_test_archive(f, compression)
  144. # Read the ZIP archive
  145. with zipfile.ZipFile(f, "r", compression) as zipfp:
  146. zipdata1 = []
  147. with zipfp.open(TESTFN) as zipopen1:
  148. while True:
  149. read_data = zipopen1.read(randint(1, 1024))
  150. if not read_data:
  151. break
  152. zipdata1.append(read_data)
  153. self.assertEqual(b''.join(zipdata1), self.data)
  154. def test_random_open(self):
  155. for f in get_files(self):
  156. self.zip_random_open_test(f, self.compression)
  157. def zip_read1_test(self, f, compression):
  158. self.make_test_archive(f, compression)
  159. # Read the ZIP archive
  160. with zipfile.ZipFile(f, "r") as zipfp, \
  161. zipfp.open(TESTFN) as zipopen:
  162. zipdata = []
  163. while True:
  164. read_data = zipopen.read1(-1)
  165. if not read_data:
  166. break
  167. zipdata.append(read_data)
  168. self.assertEqual(b''.join(zipdata), self.data)
  169. def test_read1(self):
  170. for f in get_files(self):
  171. self.zip_read1_test(f, self.compression)
  172. def zip_read1_10_test(self, f, compression):
  173. self.make_test_archive(f, compression)
  174. # Read the ZIP archive
  175. with zipfile.ZipFile(f, "r") as zipfp, \
  176. zipfp.open(TESTFN) as zipopen:
  177. zipdata = []
  178. while True:
  179. read_data = zipopen.read1(10)
  180. self.assertLessEqual(len(read_data), 10)
  181. if not read_data:
  182. break
  183. zipdata.append(read_data)
  184. self.assertEqual(b''.join(zipdata), self.data)
  185. def test_read1_10(self):
  186. for f in get_files(self):
  187. self.zip_read1_10_test(f, self.compression)
  188. def zip_readline_read_test(self, f, compression):
  189. self.make_test_archive(f, compression)
  190. # Read the ZIP archive
  191. with zipfile.ZipFile(f, "r") as zipfp, \
  192. zipfp.open(TESTFN) as zipopen:
  193. data = b''
  194. while True:
  195. read = zipopen.readline()
  196. if not read:
  197. break
  198. data += read
  199. read = zipopen.read(100)
  200. if not read:
  201. break
  202. data += read
  203. self.assertEqual(data, self.data)
  204. def test_readline_read(self):
  205. # Issue #7610: calls to readline() interleaved with calls to read().
  206. for f in get_files(self):
  207. self.zip_readline_read_test(f, self.compression)
  208. def zip_readline_test(self, f, compression):
  209. self.make_test_archive(f, compression)
  210. # Read the ZIP archive
  211. with zipfile.ZipFile(f, "r") as zipfp:
  212. with zipfp.open(TESTFN) as zipopen:
  213. for line in self.line_gen:
  214. linedata = zipopen.readline()
  215. self.assertEqual(linedata, line)
  216. def test_readline(self):
  217. for f in get_files(self):
  218. self.zip_readline_test(f, self.compression)
  219. def zip_readlines_test(self, f, compression):
  220. self.make_test_archive(f, compression)
  221. # Read the ZIP archive
  222. with zipfile.ZipFile(f, "r") as zipfp:
  223. with zipfp.open(TESTFN) as zipopen:
  224. ziplines = zipopen.readlines()
  225. for line, zipline in zip(self.line_gen, ziplines):
  226. self.assertEqual(zipline, line)
  227. def test_readlines(self):
  228. for f in get_files(self):
  229. self.zip_readlines_test(f, self.compression)
  230. def zip_iterlines_test(self, f, compression):
  231. self.make_test_archive(f, compression)
  232. # Read the ZIP archive
  233. with zipfile.ZipFile(f, "r") as zipfp:
  234. with zipfp.open(TESTFN) as zipopen:
  235. for line, zipline in zip(self.line_gen, zipopen):
  236. self.assertEqual(zipline, line)
  237. def test_iterlines(self):
  238. for f in get_files(self):
  239. self.zip_iterlines_test(f, self.compression)
  240. def test_low_compression(self):
  241. """Check for cases where compressed data is larger than original."""
  242. # Create the ZIP archive
  243. with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipfp:
  244. zipfp.writestr("strfile", '12')
  245. # Get an open object for strfile
  246. with zipfile.ZipFile(TESTFN2, "r", self.compression) as zipfp:
  247. with zipfp.open("strfile") as openobj:
  248. self.assertEqual(openobj.read(1), b'1')
  249. self.assertEqual(openobj.read(1), b'2')
  250. def test_writestr_compression(self):
  251. zipfp = zipfile.ZipFile(TESTFN2, "w")
  252. zipfp.writestr("b.txt", "hello world", compress_type=self.compression)
  253. info = zipfp.getinfo('b.txt')
  254. self.assertEqual(info.compress_type, self.compression)
  255. def test_writestr_compresslevel(self):
  256. zipfp = zipfile.ZipFile(TESTFN2, "w", compresslevel=1)
  257. zipfp.writestr("a.txt", "hello world", compress_type=self.compression)
  258. zipfp.writestr("b.txt", "hello world", compress_type=self.compression,
  259. compresslevel=2)
  260. # Compression level follows the constructor.
  261. a_info = zipfp.getinfo('a.txt')
  262. self.assertEqual(a_info.compress_type, self.compression)
  263. self.assertEqual(a_info._compresslevel, 1)
  264. # Compression level is overridden.
  265. b_info = zipfp.getinfo('b.txt')
  266. self.assertEqual(b_info.compress_type, self.compression)
  267. self.assertEqual(b_info._compresslevel, 2)
  268. def test_read_return_size(self):
  269. # Issue #9837: ZipExtFile.read() shouldn't return more bytes
  270. # than requested.
  271. for test_size in (1, 4095, 4096, 4097, 16384):
  272. file_size = test_size + 1
  273. junk = randbytes(file_size)
  274. with zipfile.ZipFile(io.BytesIO(), "w", self.compression) as zipf:
  275. zipf.writestr('foo', junk)
  276. with zipf.open('foo', 'r') as fp:
  277. buf = fp.read(test_size)
  278. self.assertEqual(len(buf), test_size)
  279. def test_truncated_zipfile(self):
  280. fp = io.BytesIO()
  281. with zipfile.ZipFile(fp, mode='w') as zipf:
  282. zipf.writestr('strfile', self.data, compress_type=self.compression)
  283. end_offset = fp.tell()
  284. zipfiledata = fp.getvalue()
  285. fp = io.BytesIO(zipfiledata)
  286. with zipfile.ZipFile(fp) as zipf:
  287. with zipf.open('strfile') as zipopen:
  288. fp.truncate(end_offset - 20)
  289. with self.assertRaises(EOFError):
  290. zipopen.read()
  291. fp = io.BytesIO(zipfiledata)
  292. with zipfile.ZipFile(fp) as zipf:
  293. with zipf.open('strfile') as zipopen:
  294. fp.truncate(end_offset - 20)
  295. with self.assertRaises(EOFError):
  296. while zipopen.read(100):
  297. pass
  298. fp = io.BytesIO(zipfiledata)
  299. with zipfile.ZipFile(fp) as zipf:
  300. with zipf.open('strfile') as zipopen:
  301. fp.truncate(end_offset - 20)
  302. with self.assertRaises(EOFError):
  303. while zipopen.read1(100):
  304. pass
  305. def test_repr(self):
  306. fname = 'file.name'
  307. for f in get_files(self):
  308. with zipfile.ZipFile(f, 'w', self.compression) as zipfp:
  309. zipfp.write(TESTFN, fname)
  310. r = repr(zipfp)
  311. self.assertIn("mode='w'", r)
  312. with zipfile.ZipFile(f, 'r') as zipfp:
  313. r = repr(zipfp)
  314. if isinstance(f, str):
  315. self.assertIn('filename=%r' % f, r)
  316. else:
  317. self.assertIn('file=%r' % f, r)
  318. self.assertIn("mode='r'", r)
  319. r = repr(zipfp.getinfo(fname))
  320. self.assertIn('filename=%r' % fname, r)
  321. self.assertIn('filemode=', r)
  322. self.assertIn('file_size=', r)
  323. if self.compression != zipfile.ZIP_STORED:
  324. self.assertIn('compress_type=', r)
  325. self.assertIn('compress_size=', r)
  326. with zipfp.open(fname) as zipopen:
  327. r = repr(zipopen)
  328. self.assertIn('name=%r' % fname, r)
  329. self.assertIn("mode='r'", r)
  330. if self.compression != zipfile.ZIP_STORED:
  331. self.assertIn('compress_type=', r)
  332. self.assertIn('[closed]', repr(zipopen))
  333. self.assertIn('[closed]', repr(zipfp))
  334. def test_compresslevel_basic(self):
  335. for f in get_files(self):
  336. self.zip_test(f, self.compression, compresslevel=9)
  337. def test_per_file_compresslevel(self):
  338. """Check that files within a Zip archive can have different
  339. compression levels."""
  340. with zipfile.ZipFile(TESTFN2, "w", compresslevel=1) as zipfp:
  341. zipfp.write(TESTFN, 'compress_1')
  342. zipfp.write(TESTFN, 'compress_9', compresslevel=9)
  343. one_info = zipfp.getinfo('compress_1')
  344. nine_info = zipfp.getinfo('compress_9')
  345. self.assertEqual(one_info._compresslevel, 1)
  346. self.assertEqual(nine_info._compresslevel, 9)
  347. def test_writing_errors(self):
  348. class BrokenFile(io.BytesIO):
  349. def write(self, data):
  350. nonlocal count
  351. if count is not None:
  352. if count == stop:
  353. raise OSError
  354. count += 1
  355. super().write(data)
  356. stop = 0
  357. while True:
  358. testfile = BrokenFile()
  359. count = None
  360. with zipfile.ZipFile(testfile, 'w', self.compression) as zipfp:
  361. with zipfp.open('file1', 'w') as f:
  362. f.write(b'data1')
  363. count = 0
  364. try:
  365. with zipfp.open('file2', 'w') as f:
  366. f.write(b'data2')
  367. except OSError:
  368. stop += 1
  369. else:
  370. break
  371. finally:
  372. count = None
  373. with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp:
  374. self.assertEqual(zipfp.namelist(), ['file1'])
  375. self.assertEqual(zipfp.read('file1'), b'data1')
  376. with zipfile.ZipFile(io.BytesIO(testfile.getvalue())) as zipfp:
  377. self.assertEqual(zipfp.namelist(), ['file1', 'file2'])
  378. self.assertEqual(zipfp.read('file1'), b'data1')
  379. self.assertEqual(zipfp.read('file2'), b'data2')
  380. def tearDown(self):
  381. unlink(TESTFN)
  382. unlink(TESTFN2)
  383. class StoredTestsWithSourceFile(AbstractTestsWithSourceFile,
  384. unittest.TestCase):
  385. compression = zipfile.ZIP_STORED
  386. test_low_compression = None
  387. def zip_test_writestr_permissions(self, f, compression):
  388. # Make sure that writestr and open(... mode='w') create files with
  389. # mode 0600, when they are passed a name rather than a ZipInfo
  390. # instance.
  391. self.make_test_archive(f, compression)
  392. with zipfile.ZipFile(f, "r") as zipfp:
  393. zinfo = zipfp.getinfo('strfile')
  394. self.assertEqual(zinfo.external_attr, 0o600 << 16)
  395. zinfo2 = zipfp.getinfo('written-open-w')
  396. self.assertEqual(zinfo2.external_attr, 0o600 << 16)
  397. def test_writestr_permissions(self):
  398. for f in get_files(self):
  399. self.zip_test_writestr_permissions(f, zipfile.ZIP_STORED)
  400. def test_absolute_arcnames(self):
  401. with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
  402. zipfp.write(TESTFN, "/absolute")
  403. with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
  404. self.assertEqual(zipfp.namelist(), ["absolute"])
  405. def test_append_to_zip_file(self):
  406. """Test appending to an existing zipfile."""
  407. with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
  408. zipfp.write(TESTFN, TESTFN)
  409. with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
  410. zipfp.writestr("strfile", self.data)
  411. self.assertEqual(zipfp.namelist(), [TESTFN, "strfile"])
  412. def test_append_to_non_zip_file(self):
  413. """Test appending to an existing file that is not a zipfile."""
  414. # NOTE: this test fails if len(d) < 22 because of the first
  415. # line "fpin.seek(-22, 2)" in _EndRecData
  416. data = b'I am not a ZipFile!'*10
  417. with open(TESTFN2, 'wb') as f:
  418. f.write(data)
  419. with zipfile.ZipFile(TESTFN2, "a", zipfile.ZIP_STORED) as zipfp:
  420. zipfp.write(TESTFN, TESTFN)
  421. with open(TESTFN2, 'rb') as f:
  422. f.seek(len(data))
  423. with zipfile.ZipFile(f, "r") as zipfp:
  424. self.assertEqual(zipfp.namelist(), [TESTFN])
  425. self.assertEqual(zipfp.read(TESTFN), self.data)
  426. with open(TESTFN2, 'rb') as f:
  427. self.assertEqual(f.read(len(data)), data)
  428. zipfiledata = f.read()
  429. with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp:
  430. self.assertEqual(zipfp.namelist(), [TESTFN])
  431. self.assertEqual(zipfp.read(TESTFN), self.data)
  432. def test_read_concatenated_zip_file(self):
  433. with io.BytesIO() as bio:
  434. with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp:
  435. zipfp.write(TESTFN, TESTFN)
  436. zipfiledata = bio.getvalue()
  437. data = b'I am not a ZipFile!'*10
  438. with open(TESTFN2, 'wb') as f:
  439. f.write(data)
  440. f.write(zipfiledata)
  441. with zipfile.ZipFile(TESTFN2) as zipfp:
  442. self.assertEqual(zipfp.namelist(), [TESTFN])
  443. self.assertEqual(zipfp.read(TESTFN), self.data)
  444. def test_append_to_concatenated_zip_file(self):
  445. with io.BytesIO() as bio:
  446. with zipfile.ZipFile(bio, 'w', zipfile.ZIP_STORED) as zipfp:
  447. zipfp.write(TESTFN, TESTFN)
  448. zipfiledata = bio.getvalue()
  449. data = b'I am not a ZipFile!'*1000000
  450. with open(TESTFN2, 'wb') as f:
  451. f.write(data)
  452. f.write(zipfiledata)
  453. with zipfile.ZipFile(TESTFN2, 'a') as zipfp:
  454. self.assertEqual(zipfp.namelist(), [TESTFN])
  455. zipfp.writestr('strfile', self.data)
  456. with open(TESTFN2, 'rb') as f:
  457. self.assertEqual(f.read(len(data)), data)
  458. zipfiledata = f.read()
  459. with io.BytesIO(zipfiledata) as bio, zipfile.ZipFile(bio) as zipfp:
  460. self.assertEqual(zipfp.namelist(), [TESTFN, 'strfile'])
  461. self.assertEqual(zipfp.read(TESTFN), self.data)
  462. self.assertEqual(zipfp.read('strfile'), self.data)
  463. def test_ignores_newline_at_end(self):
  464. with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
  465. zipfp.write(TESTFN, TESTFN)
  466. with open(TESTFN2, 'a', encoding='utf-8') as f:
  467. f.write("\r\n\00\00\00")
  468. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  469. self.assertIsInstance(zipfp, zipfile.ZipFile)
  470. def test_ignores_stuff_appended_past_comments(self):
  471. with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
  472. zipfp.comment = b"this is a comment"
  473. zipfp.write(TESTFN, TESTFN)
  474. with open(TESTFN2, 'a', encoding='utf-8') as f:
  475. f.write("abcdef\r\n")
  476. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  477. self.assertIsInstance(zipfp, zipfile.ZipFile)
  478. self.assertEqual(zipfp.comment, b"this is a comment")
  479. def test_write_default_name(self):
  480. """Check that calling ZipFile.write without arcname specified
  481. produces the expected result."""
  482. with zipfile.ZipFile(TESTFN2, "w") as zipfp:
  483. zipfp.write(TESTFN)
  484. with open(TESTFN, "rb") as f:
  485. self.assertEqual(zipfp.read(TESTFN), f.read())
  486. def test_io_on_closed_zipextfile(self):
  487. fname = "somefile.txt"
  488. with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
  489. zipfp.writestr(fname, "bogus")
  490. with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
  491. with zipfp.open(fname) as fid:
  492. fid.close()
  493. self.assertRaises(ValueError, fid.read)
  494. self.assertRaises(ValueError, fid.seek, 0)
  495. self.assertRaises(ValueError, fid.tell)
  496. self.assertRaises(ValueError, fid.readable)
  497. self.assertRaises(ValueError, fid.seekable)
  498. def test_write_to_readonly(self):
  499. """Check that trying to call write() on a readonly ZipFile object
  500. raises a ValueError."""
  501. with zipfile.ZipFile(TESTFN2, mode="w") as zipfp:
  502. zipfp.writestr("somefile.txt", "bogus")
  503. with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
  504. self.assertRaises(ValueError, zipfp.write, TESTFN)
  505. with zipfile.ZipFile(TESTFN2, mode="r") as zipfp:
  506. with self.assertRaises(ValueError):
  507. zipfp.open(TESTFN, mode='w')
  508. def test_add_file_before_1980(self):
  509. # Set atime and mtime to 1970-01-01
  510. os.utime(TESTFN, (0, 0))
  511. with zipfile.ZipFile(TESTFN2, "w") as zipfp:
  512. self.assertRaises(ValueError, zipfp.write, TESTFN)
  513. with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
  514. zipfp.write(TESTFN)
  515. zinfo = zipfp.getinfo(TESTFN)
  516. self.assertEqual(zinfo.date_time, (1980, 1, 1, 0, 0, 0))
  517. def test_add_file_after_2107(self):
  518. # Set atime and mtime to 2108-12-30
  519. ts = 4386268800
  520. try:
  521. time.localtime(ts)
  522. except OverflowError:
  523. self.skipTest(f'time.localtime({ts}) raises OverflowError')
  524. try:
  525. os.utime(TESTFN, (ts, ts))
  526. except OverflowError:
  527. self.skipTest('Host fs cannot set timestamp to required value.')
  528. mtime_ns = os.stat(TESTFN).st_mtime_ns
  529. if mtime_ns != (4386268800 * 10**9):
  530. # XFS filesystem is limited to 32-bit timestamp, but the syscall
  531. # didn't fail. Moreover, there is a VFS bug which returns
  532. # a cached timestamp which is different than the value on disk.
  533. #
  534. # Test st_mtime_ns rather than st_mtime to avoid rounding issues.
  535. #
  536. # https://bugzilla.redhat.com/show_bug.cgi?id=1795576
  537. # https://bugs.python.org/issue39460#msg360952
  538. self.skipTest(f"Linux VFS/XFS kernel bug detected: {mtime_ns=}")
  539. with zipfile.ZipFile(TESTFN2, "w") as zipfp:
  540. self.assertRaises(struct.error, zipfp.write, TESTFN)
  541. with zipfile.ZipFile(TESTFN2, "w", strict_timestamps=False) as zipfp:
  542. zipfp.write(TESTFN)
  543. zinfo = zipfp.getinfo(TESTFN)
  544. self.assertEqual(zinfo.date_time, (2107, 12, 31, 23, 59, 59))
  545. @requires_zlib()
  546. class DeflateTestsWithSourceFile(AbstractTestsWithSourceFile,
  547. unittest.TestCase):
  548. compression = zipfile.ZIP_DEFLATED
  549. def test_per_file_compression(self):
  550. """Check that files within a Zip archive can have different
  551. compression options."""
  552. with zipfile.ZipFile(TESTFN2, "w") as zipfp:
  553. zipfp.write(TESTFN, 'storeme', zipfile.ZIP_STORED)
  554. zipfp.write(TESTFN, 'deflateme', zipfile.ZIP_DEFLATED)
  555. sinfo = zipfp.getinfo('storeme')
  556. dinfo = zipfp.getinfo('deflateme')
  557. self.assertEqual(sinfo.compress_type, zipfile.ZIP_STORED)
  558. self.assertEqual(dinfo.compress_type, zipfile.ZIP_DEFLATED)
  559. @requires_bz2()
  560. class Bzip2TestsWithSourceFile(AbstractTestsWithSourceFile,
  561. unittest.TestCase):
  562. compression = zipfile.ZIP_BZIP2
  563. @requires_lzma()
  564. class LzmaTestsWithSourceFile(AbstractTestsWithSourceFile,
  565. unittest.TestCase):
  566. compression = zipfile.ZIP_LZMA
  567. class AbstractTestZip64InSmallFiles:
  568. # These tests test the ZIP64 functionality without using large files,
  569. # see test_zipfile64 for proper tests.
  570. @classmethod
  571. def setUpClass(cls):
  572. line_gen = (bytes("Test of zipfile line %d." % i, "ascii")
  573. for i in range(0, FIXEDTEST_SIZE))
  574. cls.data = b'\n'.join(line_gen)
  575. def setUp(self):
  576. self._limit = zipfile.ZIP64_LIMIT
  577. self._filecount_limit = zipfile.ZIP_FILECOUNT_LIMIT
  578. zipfile.ZIP64_LIMIT = 1000
  579. zipfile.ZIP_FILECOUNT_LIMIT = 9
  580. # Make a source file with some lines
  581. with open(TESTFN, "wb") as fp:
  582. fp.write(self.data)
  583. def zip_test(self, f, compression):
  584. # Create the ZIP archive
  585. with zipfile.ZipFile(f, "w", compression, allowZip64=True) as zipfp:
  586. zipfp.write(TESTFN, "another.name")
  587. zipfp.write(TESTFN, TESTFN)
  588. zipfp.writestr("strfile", self.data)
  589. # Read the ZIP archive
  590. with zipfile.ZipFile(f, "r", compression) as zipfp:
  591. self.assertEqual(zipfp.read(TESTFN), self.data)
  592. self.assertEqual(zipfp.read("another.name"), self.data)
  593. self.assertEqual(zipfp.read("strfile"), self.data)
  594. # Print the ZIP directory
  595. fp = io.StringIO()
  596. zipfp.printdir(fp)
  597. directory = fp.getvalue()
  598. lines = directory.splitlines()
  599. self.assertEqual(len(lines), 4) # Number of files + header
  600. self.assertIn('File Name', lines[0])
  601. self.assertIn('Modified', lines[0])
  602. self.assertIn('Size', lines[0])
  603. fn, date, time_, size = lines[1].split()
  604. self.assertEqual(fn, 'another.name')
  605. self.assertTrue(time.strptime(date, '%Y-%m-%d'))
  606. self.assertTrue(time.strptime(time_, '%H:%M:%S'))
  607. self.assertEqual(size, str(len(self.data)))
  608. # Check the namelist
  609. names = zipfp.namelist()
  610. self.assertEqual(len(names), 3)
  611. self.assertIn(TESTFN, names)
  612. self.assertIn("another.name", names)
  613. self.assertIn("strfile", names)
  614. # Check infolist
  615. infos = zipfp.infolist()
  616. names = [i.filename for i in infos]
  617. self.assertEqual(len(names), 3)
  618. self.assertIn(TESTFN, names)
  619. self.assertIn("another.name", names)
  620. self.assertIn("strfile", names)
  621. for i in infos:
  622. self.assertEqual(i.file_size, len(self.data))
  623. # check getinfo
  624. for nm in (TESTFN, "another.name", "strfile"):
  625. info = zipfp.getinfo(nm)
  626. self.assertEqual(info.filename, nm)
  627. self.assertEqual(info.file_size, len(self.data))
  628. # Check that testzip doesn't raise an exception
  629. zipfp.testzip()
  630. def test_basic(self):
  631. for f in get_files(self):
  632. self.zip_test(f, self.compression)
  633. def test_too_many_files(self):
  634. # This test checks that more than 64k files can be added to an archive,
  635. # and that the resulting archive can be read properly by ZipFile
  636. zipf = zipfile.ZipFile(TESTFN, "w", self.compression,
  637. allowZip64=True)
  638. zipf.debug = 100
  639. numfiles = 15
  640. for i in range(numfiles):
  641. zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
  642. self.assertEqual(len(zipf.namelist()), numfiles)
  643. zipf.close()
  644. zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression)
  645. self.assertEqual(len(zipf2.namelist()), numfiles)
  646. for i in range(numfiles):
  647. content = zipf2.read("foo%08d" % i).decode('ascii')
  648. self.assertEqual(content, "%d" % (i**3 % 57))
  649. zipf2.close()
  650. def test_too_many_files_append(self):
  651. zipf = zipfile.ZipFile(TESTFN, "w", self.compression,
  652. allowZip64=False)
  653. zipf.debug = 100
  654. numfiles = 9
  655. for i in range(numfiles):
  656. zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
  657. self.assertEqual(len(zipf.namelist()), numfiles)
  658. with self.assertRaises(zipfile.LargeZipFile):
  659. zipf.writestr("foo%08d" % numfiles, b'')
  660. self.assertEqual(len(zipf.namelist()), numfiles)
  661. zipf.close()
  662. zipf = zipfile.ZipFile(TESTFN, "a", self.compression,
  663. allowZip64=False)
  664. zipf.debug = 100
  665. self.assertEqual(len(zipf.namelist()), numfiles)
  666. with self.assertRaises(zipfile.LargeZipFile):
  667. zipf.writestr("foo%08d" % numfiles, b'')
  668. self.assertEqual(len(zipf.namelist()), numfiles)
  669. zipf.close()
  670. zipf = zipfile.ZipFile(TESTFN, "a", self.compression,
  671. allowZip64=True)
  672. zipf.debug = 100
  673. self.assertEqual(len(zipf.namelist()), numfiles)
  674. numfiles2 = 15
  675. for i in range(numfiles, numfiles2):
  676. zipf.writestr("foo%08d" % i, "%d" % (i**3 % 57))
  677. self.assertEqual(len(zipf.namelist()), numfiles2)
  678. zipf.close()
  679. zipf2 = zipfile.ZipFile(TESTFN, "r", self.compression)
  680. self.assertEqual(len(zipf2.namelist()), numfiles2)
  681. for i in range(numfiles2):
  682. content = zipf2.read("foo%08d" % i).decode('ascii')
  683. self.assertEqual(content, "%d" % (i**3 % 57))
  684. zipf2.close()
  685. def tearDown(self):
  686. zipfile.ZIP64_LIMIT = self._limit
  687. zipfile.ZIP_FILECOUNT_LIMIT = self._filecount_limit
  688. unlink(TESTFN)
  689. unlink(TESTFN2)
  690. class StoredTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
  691. unittest.TestCase):
  692. compression = zipfile.ZIP_STORED
  693. def large_file_exception_test(self, f, compression):
  694. with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp:
  695. self.assertRaises(zipfile.LargeZipFile,
  696. zipfp.write, TESTFN, "another.name")
  697. def large_file_exception_test2(self, f, compression):
  698. with zipfile.ZipFile(f, "w", compression, allowZip64=False) as zipfp:
  699. self.assertRaises(zipfile.LargeZipFile,
  700. zipfp.writestr, "another.name", self.data)
  701. def test_large_file_exception(self):
  702. for f in get_files(self):
  703. self.large_file_exception_test(f, zipfile.ZIP_STORED)
  704. self.large_file_exception_test2(f, zipfile.ZIP_STORED)
  705. def test_absolute_arcnames(self):
  706. with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED,
  707. allowZip64=True) as zipfp:
  708. zipfp.write(TESTFN, "/absolute")
  709. with zipfile.ZipFile(TESTFN2, "r", zipfile.ZIP_STORED) as zipfp:
  710. self.assertEqual(zipfp.namelist(), ["absolute"])
  711. def test_append(self):
  712. # Test that appending to the Zip64 archive doesn't change
  713. # extra fields of existing entries.
  714. with zipfile.ZipFile(TESTFN2, "w", allowZip64=True) as zipfp:
  715. zipfp.writestr("strfile", self.data)
  716. with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp:
  717. zinfo = zipfp.getinfo("strfile")
  718. extra = zinfo.extra
  719. with zipfile.ZipFile(TESTFN2, "a", allowZip64=True) as zipfp:
  720. zipfp.writestr("strfile2", self.data)
  721. with zipfile.ZipFile(TESTFN2, "r", allowZip64=True) as zipfp:
  722. zinfo = zipfp.getinfo("strfile")
  723. self.assertEqual(zinfo.extra, extra)
  724. def make_zip64_file(
  725. self, file_size_64_set=False, file_size_extra=False,
  726. compress_size_64_set=False, compress_size_extra=False,
  727. header_offset_64_set=False, header_offset_extra=False,
  728. ):
  729. """Generate bytes sequence for a zip with (incomplete) zip64 data.
  730. The actual values (not the zip 64 0xffffffff values) stored in the file
  731. are:
  732. file_size: 8
  733. compress_size: 8
  734. header_offset: 0
  735. """
  736. actual_size = 8
  737. actual_header_offset = 0
  738. local_zip64_fields = []
  739. central_zip64_fields = []
  740. file_size = actual_size
  741. if file_size_64_set:
  742. file_size = 0xffffffff
  743. if file_size_extra:
  744. local_zip64_fields.append(actual_size)
  745. central_zip64_fields.append(actual_size)
  746. file_size = struct.pack("<L", file_size)
  747. compress_size = actual_size
  748. if compress_size_64_set:
  749. compress_size = 0xffffffff
  750. if compress_size_extra:
  751. local_zip64_fields.append(actual_size)
  752. central_zip64_fields.append(actual_size)
  753. compress_size = struct.pack("<L", compress_size)
  754. header_offset = actual_header_offset
  755. if header_offset_64_set:
  756. header_offset = 0xffffffff
  757. if header_offset_extra:
  758. central_zip64_fields.append(actual_header_offset)
  759. header_offset = struct.pack("<L", header_offset)
  760. local_extra = struct.pack(
  761. '<HH' + 'Q'*len(local_zip64_fields),
  762. 0x0001,
  763. 8*len(local_zip64_fields),
  764. *local_zip64_fields
  765. )
  766. central_extra = struct.pack(
  767. '<HH' + 'Q'*len(central_zip64_fields),
  768. 0x0001,
  769. 8*len(central_zip64_fields),
  770. *central_zip64_fields
  771. )
  772. central_dir_size = struct.pack('<Q', 58 + 8 * len(central_zip64_fields))
  773. offset_to_central_dir = struct.pack('<Q', 50 + 8 * len(local_zip64_fields))
  774. local_extra_length = struct.pack("<H", 4 + 8 * len(local_zip64_fields))
  775. central_extra_length = struct.pack("<H", 4 + 8 * len(central_zip64_fields))
  776. filename = b"test.txt"
  777. content = b"test1234"
  778. filename_length = struct.pack("<H", len(filename))
  779. zip64_contents = (
  780. # Local file header
  781. b"PK\x03\x04\x14\x00\x00\x00\x00\x00\x00\x00!\x00\x9e%\xf5\xaf"
  782. + compress_size
  783. + file_size
  784. + filename_length
  785. + local_extra_length
  786. + filename
  787. + local_extra
  788. + content
  789. # Central directory:
  790. + b"PK\x01\x02-\x03-\x00\x00\x00\x00\x00\x00\x00!\x00\x9e%\xf5\xaf"
  791. + compress_size
  792. + file_size
  793. + filename_length
  794. + central_extra_length
  795. + b"\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01"
  796. + header_offset
  797. + filename
  798. + central_extra
  799. # Zip64 end of central directory
  800. + b"PK\x06\x06,\x00\x00\x00\x00\x00\x00\x00-\x00-"
  801. + b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00"
  802. + b"\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00"
  803. + central_dir_size
  804. + offset_to_central_dir
  805. # Zip64 end of central directory locator
  806. + b"PK\x06\x07\x00\x00\x00\x00l\x00\x00\x00\x00\x00\x00\x00\x01"
  807. + b"\x00\x00\x00"
  808. # end of central directory
  809. + b"PK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00:\x00\x00\x002\x00"
  810. + b"\x00\x00\x00\x00"
  811. )
  812. return zip64_contents
  813. def test_bad_zip64_extra(self):
  814. """Missing zip64 extra records raises an exception.
  815. There are 4 fields that the zip64 format handles (the disk number is
  816. not used in this module and so is ignored here). According to the zip
  817. spec:
  818. The order of the fields in the zip64 extended
  819. information record is fixed, but the fields MUST
  820. only appear if the corresponding Local or Central
  821. directory record field is set to 0xFFFF or 0xFFFFFFFF.
  822. If the zip64 extra content doesn't contain enough entries for the
  823. number of fields marked with 0xFFFF or 0xFFFFFFFF, we raise an error.
  824. This test mismatches the length of the zip64 extra field and the number
  825. of fields set to indicate the presence of zip64 data.
  826. """
  827. # zip64 file size present, no fields in extra, expecting one, equals
  828. # missing file size.
  829. missing_file_size_extra = self.make_zip64_file(
  830. file_size_64_set=True,
  831. )
  832. with self.assertRaises(zipfile.BadZipFile) as e:
  833. zipfile.ZipFile(io.BytesIO(missing_file_size_extra))
  834. self.assertIn('file size', str(e.exception).lower())
  835. # zip64 file size present, zip64 compress size present, one field in
  836. # extra, expecting two, equals missing compress size.
  837. missing_compress_size_extra = self.make_zip64_file(
  838. file_size_64_set=True,
  839. file_size_extra=True,
  840. compress_size_64_set=True,
  841. )
  842. with self.assertRaises(zipfile.BadZipFile) as e:
  843. zipfile.ZipFile(io.BytesIO(missing_compress_size_extra))
  844. self.assertIn('compress size', str(e.exception).lower())
  845. # zip64 compress size present, no fields in extra, expecting one,
  846. # equals missing compress size.
  847. missing_compress_size_extra = self.make_zip64_file(
  848. compress_size_64_set=True,
  849. )
  850. with self.assertRaises(zipfile.BadZipFile) as e:
  851. zipfile.ZipFile(io.BytesIO(missing_compress_size_extra))
  852. self.assertIn('compress size', str(e.exception).lower())
  853. # zip64 file size present, zip64 compress size present, zip64 header
  854. # offset present, two fields in extra, expecting three, equals missing
  855. # header offset
  856. missing_header_offset_extra = self.make_zip64_file(
  857. file_size_64_set=True,
  858. file_size_extra=True,
  859. compress_size_64_set=True,
  860. compress_size_extra=True,
  861. header_offset_64_set=True,
  862. )
  863. with self.assertRaises(zipfile.BadZipFile) as e:
  864. zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
  865. self.assertIn('header offset', str(e.exception).lower())
  866. # zip64 compress size present, zip64 header offset present, one field
  867. # in extra, expecting two, equals missing header offset
  868. missing_header_offset_extra = self.make_zip64_file(
  869. file_size_64_set=False,
  870. compress_size_64_set=True,
  871. compress_size_extra=True,
  872. header_offset_64_set=True,
  873. )
  874. with self.assertRaises(zipfile.BadZipFile) as e:
  875. zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
  876. self.assertIn('header offset', str(e.exception).lower())
  877. # zip64 file size present, zip64 header offset present, one field in
  878. # extra, expecting two, equals missing header offset
  879. missing_header_offset_extra = self.make_zip64_file(
  880. file_size_64_set=True,
  881. file_size_extra=True,
  882. compress_size_64_set=False,
  883. header_offset_64_set=True,
  884. )
  885. with self.assertRaises(zipfile.BadZipFile) as e:
  886. zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
  887. self.assertIn('header offset', str(e.exception).lower())
  888. # zip64 header offset present, no fields in extra, expecting one,
  889. # equals missing header offset
  890. missing_header_offset_extra = self.make_zip64_file(
  891. file_size_64_set=False,
  892. compress_size_64_set=False,
  893. header_offset_64_set=True,
  894. )
  895. with self.assertRaises(zipfile.BadZipFile) as e:
  896. zipfile.ZipFile(io.BytesIO(missing_header_offset_extra))
  897. self.assertIn('header offset', str(e.exception).lower())
  898. def test_generated_valid_zip64_extra(self):
  899. # These values are what is set in the make_zip64_file method.
  900. expected_file_size = 8
  901. expected_compress_size = 8
  902. expected_header_offset = 0
  903. expected_content = b"test1234"
  904. # Loop through the various valid combinations of zip64 masks
  905. # present and extra fields present.
  906. params = (
  907. {"file_size_64_set": True, "file_size_extra": True},
  908. {"compress_size_64_set": True, "compress_size_extra": True},
  909. {"header_offset_64_set": True, "header_offset_extra": True},
  910. )
  911. for r in range(1, len(params) + 1):
  912. for combo in itertools.combinations(params, r):
  913. kwargs = {}
  914. for c in combo:
  915. kwargs.update(c)
  916. with zipfile.ZipFile(io.BytesIO(self.make_zip64_file(**kwargs))) as zf:
  917. zinfo = zf.infolist()[0]
  918. self.assertEqual(zinfo.file_size, expected_file_size)
  919. self.assertEqual(zinfo.compress_size, expected_compress_size)
  920. self.assertEqual(zinfo.header_offset, expected_header_offset)
  921. self.assertEqual(zf.read(zinfo), expected_content)
  922. @requires_zlib()
  923. class DeflateTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
  924. unittest.TestCase):
  925. compression = zipfile.ZIP_DEFLATED
  926. @requires_bz2()
  927. class Bzip2TestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
  928. unittest.TestCase):
  929. compression = zipfile.ZIP_BZIP2
  930. @requires_lzma()
  931. class LzmaTestZip64InSmallFiles(AbstractTestZip64InSmallFiles,
  932. unittest.TestCase):
  933. compression = zipfile.ZIP_LZMA
  934. class AbstractWriterTests:
  935. def tearDown(self):
  936. unlink(TESTFN2)
  937. def test_close_after_close(self):
  938. data = b'content'
  939. with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
  940. w = zipf.open('test', 'w')
  941. w.write(data)
  942. w.close()
  943. self.assertTrue(w.closed)
  944. w.close()
  945. self.assertTrue(w.closed)
  946. self.assertEqual(zipf.read('test'), data)
  947. def test_write_after_close(self):
  948. data = b'content'
  949. with zipfile.ZipFile(TESTFN2, "w", self.compression) as zipf:
  950. w = zipf.open('test', 'w')
  951. w.write(data)
  952. w.close()
  953. self.assertTrue(w.closed)
  954. self.assertRaises(ValueError, w.write, b'')
  955. self.assertEqual(zipf.read('test'), data)
  956. def test_issue44439(self):
  957. q = array.array('Q', [1, 2, 3, 4, 5])
  958. LENGTH = len(q) * q.itemsize
  959. with zipfile.ZipFile(io.BytesIO(), 'w', self.compression) as zip:
  960. with zip.open('data', 'w') as data:
  961. self.assertEqual(data.write(q), LENGTH)
  962. self.assertEqual(zip.getinfo('data').file_size, LENGTH)
  963. class StoredWriterTests(AbstractWriterTests, unittest.TestCase):
  964. compression = zipfile.ZIP_STORED
  965. @requires_zlib()
  966. class DeflateWriterTests(AbstractWriterTests, unittest.TestCase):
  967. compression = zipfile.ZIP_DEFLATED
  968. @requires_bz2()
  969. class Bzip2WriterTests(AbstractWriterTests, unittest.TestCase):
  970. compression = zipfile.ZIP_BZIP2
  971. @requires_lzma()
  972. class LzmaWriterTests(AbstractWriterTests, unittest.TestCase):
  973. compression = zipfile.ZIP_LZMA
  974. class PyZipFileTests(unittest.TestCase):
  975. def assertCompiledIn(self, name, namelist):
  976. if name + 'o' not in namelist:
  977. self.assertIn(name + 'c', namelist)
  978. def requiresWriteAccess(self, path):
  979. # effective_ids unavailable on windows
  980. if not os.access(path, os.W_OK,
  981. effective_ids=os.access in os.supports_effective_ids):
  982. self.skipTest('requires write access to the installed location')
  983. filename = os.path.join(path, 'test_zipfile.try')
  984. try:
  985. fd = os.open(filename, os.O_WRONLY | os.O_CREAT)
  986. os.close(fd)
  987. except Exception:
  988. self.skipTest('requires write access to the installed location')
  989. unlink(filename)
  990. def test_write_pyfile(self):
  991. self.requiresWriteAccess(os.path.dirname(__file__))
  992. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  993. fn = __file__
  994. if fn.endswith('.pyc'):
  995. path_split = fn.split(os.sep)
  996. if os.altsep is not None:
  997. path_split.extend(fn.split(os.altsep))
  998. if '__pycache__' in path_split:
  999. fn = importlib.util.source_from_cache(fn)
  1000. else:
  1001. fn = fn[:-1]
  1002. zipfp.writepy(fn)
  1003. bn = os.path.basename(fn)
  1004. self.assertNotIn(bn, zipfp.namelist())
  1005. self.assertCompiledIn(bn, zipfp.namelist())
  1006. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1007. fn = __file__
  1008. if fn.endswith('.pyc'):
  1009. fn = fn[:-1]
  1010. zipfp.writepy(fn, "testpackage")
  1011. bn = "%s/%s" % ("testpackage", os.path.basename(fn))
  1012. self.assertNotIn(bn, zipfp.namelist())
  1013. self.assertCompiledIn(bn, zipfp.namelist())
  1014. def test_write_python_package(self):
  1015. import email
  1016. packagedir = os.path.dirname(email.__file__)
  1017. self.requiresWriteAccess(packagedir)
  1018. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1019. zipfp.writepy(packagedir)
  1020. # Check for a couple of modules at different levels of the
  1021. # hierarchy
  1022. names = zipfp.namelist()
  1023. self.assertCompiledIn('email/__init__.py', names)
  1024. self.assertCompiledIn('email/mime/text.py', names)
  1025. def test_write_filtered_python_package(self):
  1026. import test
  1027. packagedir = os.path.dirname(test.__file__)
  1028. self.requiresWriteAccess(packagedir)
  1029. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1030. # first make sure that the test folder gives error messages
  1031. # (on the badsyntax_... files)
  1032. with captured_stdout() as reportSIO:
  1033. zipfp.writepy(packagedir)
  1034. reportStr = reportSIO.getvalue()
  1035. self.assertTrue('SyntaxError' in reportStr)
  1036. # then check that the filter works on the whole package
  1037. with captured_stdout() as reportSIO:
  1038. zipfp.writepy(packagedir, filterfunc=lambda whatever: False)
  1039. reportStr = reportSIO.getvalue()
  1040. self.assertTrue('SyntaxError' not in reportStr)
  1041. # then check that the filter works on individual files
  1042. def filter(path):
  1043. return not os.path.basename(path).startswith("bad")
  1044. with captured_stdout() as reportSIO, self.assertWarns(UserWarning):
  1045. zipfp.writepy(packagedir, filterfunc=filter)
  1046. reportStr = reportSIO.getvalue()
  1047. if reportStr:
  1048. print(reportStr)
  1049. self.assertTrue('SyntaxError' not in reportStr)
  1050. def test_write_with_optimization(self):
  1051. import email
  1052. packagedir = os.path.dirname(email.__file__)
  1053. self.requiresWriteAccess(packagedir)
  1054. optlevel = 1 if __debug__ else 0
  1055. ext = '.pyc'
  1056. with TemporaryFile() as t, \
  1057. zipfile.PyZipFile(t, "w", optimize=optlevel) as zipfp:
  1058. zipfp.writepy(packagedir)
  1059. names = zipfp.namelist()
  1060. self.assertIn('email/__init__' + ext, names)
  1061. self.assertIn('email/mime/text' + ext, names)
  1062. def test_write_python_directory(self):
  1063. os.mkdir(TESTFN2)
  1064. try:
  1065. with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
  1066. fp.write("print(42)\n")
  1067. with open(os.path.join(TESTFN2, "mod2.py"), "w", encoding='utf-8') as fp:
  1068. fp.write("print(42 * 42)\n")
  1069. with open(os.path.join(TESTFN2, "mod2.txt"), "w", encoding='utf-8') as fp:
  1070. fp.write("bla bla bla\n")
  1071. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1072. zipfp.writepy(TESTFN2)
  1073. names = zipfp.namelist()
  1074. self.assertCompiledIn('mod1.py', names)
  1075. self.assertCompiledIn('mod2.py', names)
  1076. self.assertNotIn('mod2.txt', names)
  1077. finally:
  1078. rmtree(TESTFN2)
  1079. def test_write_python_directory_filtered(self):
  1080. os.mkdir(TESTFN2)
  1081. try:
  1082. with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
  1083. fp.write("print(42)\n")
  1084. with open(os.path.join(TESTFN2, "mod2.py"), "w", encoding='utf-8') as fp:
  1085. fp.write("print(42 * 42)\n")
  1086. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1087. zipfp.writepy(TESTFN2, filterfunc=lambda fn:
  1088. not fn.endswith('mod2.py'))
  1089. names = zipfp.namelist()
  1090. self.assertCompiledIn('mod1.py', names)
  1091. self.assertNotIn('mod2.py', names)
  1092. finally:
  1093. rmtree(TESTFN2)
  1094. def test_write_non_pyfile(self):
  1095. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1096. with open(TESTFN, 'w', encoding='utf-8') as f:
  1097. f.write('most definitely not a python file')
  1098. self.assertRaises(RuntimeError, zipfp.writepy, TESTFN)
  1099. unlink(TESTFN)
  1100. def test_write_pyfile_bad_syntax(self):
  1101. os.mkdir(TESTFN2)
  1102. try:
  1103. with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
  1104. fp.write("Bad syntax in python file\n")
  1105. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1106. # syntax errors are printed to stdout
  1107. with captured_stdout() as s:
  1108. zipfp.writepy(os.path.join(TESTFN2, "mod1.py"))
  1109. self.assertIn("SyntaxError", s.getvalue())
  1110. # as it will not have compiled the python file, it will
  1111. # include the .py file not .pyc
  1112. names = zipfp.namelist()
  1113. self.assertIn('mod1.py', names)
  1114. self.assertNotIn('mod1.pyc', names)
  1115. finally:
  1116. rmtree(TESTFN2)
  1117. def test_write_pathlike(self):
  1118. os.mkdir(TESTFN2)
  1119. try:
  1120. with open(os.path.join(TESTFN2, "mod1.py"), "w", encoding='utf-8') as fp:
  1121. fp.write("print(42)\n")
  1122. with TemporaryFile() as t, zipfile.PyZipFile(t, "w") as zipfp:
  1123. zipfp.writepy(pathlib.Path(TESTFN2) / "mod1.py")
  1124. names = zipfp.namelist()
  1125. self.assertCompiledIn('mod1.py', names)
  1126. finally:
  1127. rmtree(TESTFN2)
  1128. class ExtractTests(unittest.TestCase):
  1129. def make_test_file(self):
  1130. with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
  1131. for fpath, fdata in SMALL_TEST_DATA:
  1132. zipfp.writestr(fpath, fdata)
  1133. def test_extract(self):
  1134. with temp_cwd():
  1135. self.make_test_file()
  1136. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  1137. for fpath, fdata in SMALL_TEST_DATA:
  1138. writtenfile = zipfp.extract(fpath)
  1139. # make sure it was written to the right place
  1140. correctfile = os.path.join(os.getcwd(), fpath)
  1141. correctfile = os.path.normpath(correctfile)
  1142. self.assertEqual(writtenfile, correctfile)
  1143. # make sure correct data is in correct file
  1144. with open(writtenfile, "rb") as f:
  1145. self.assertEqual(fdata.encode(), f.read())
  1146. unlink(writtenfile)
  1147. def _test_extract_with_target(self, target):
  1148. self.make_test_file()
  1149. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  1150. for fpath, fdata in SMALL_TEST_DATA:
  1151. writtenfile = zipfp.extract(fpath, target)
  1152. # make sure it was written to the right place
  1153. correctfile = os.path.join(target, fpath)
  1154. correctfile = os.path.normpath(correctfile)
  1155. self.assertTrue(os.path.samefile(writtenfile, correctfile), (writtenfile, target))
  1156. # make sure correct data is in correct file
  1157. with open(writtenfile, "rb") as f:
  1158. self.assertEqual(fdata.encode(), f.read())
  1159. unlink(writtenfile)
  1160. unlink(TESTFN2)
  1161. def test_extract_with_target(self):
  1162. with temp_dir() as extdir:
  1163. self._test_extract_with_target(extdir)
  1164. def test_extract_with_target_pathlike(self):
  1165. with temp_dir() as extdir:
  1166. self._test_extract_with_target(pathlib.Path(extdir))
  1167. def test_extract_all(self):
  1168. with temp_cwd():
  1169. self.make_test_file()
  1170. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  1171. zipfp.extractall()
  1172. for fpath, fdata in SMALL_TEST_DATA:
  1173. outfile = os.path.join(os.getcwd(), fpath)
  1174. with open(outfile, "rb") as f:
  1175. self.assertEqual(fdata.encode(), f.read())
  1176. unlink(outfile)
  1177. def _test_extract_all_with_target(self, target):
  1178. self.make_test_file()
  1179. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  1180. zipfp.extractall(target)
  1181. for fpath, fdata in SMALL_TEST_DATA:
  1182. outfile = os.path.join(target, fpath)
  1183. with open(outfile, "rb") as f:
  1184. self.assertEqual(fdata.encode(), f.read())
  1185. unlink(outfile)
  1186. unlink(TESTFN2)
  1187. def test_extract_all_with_target(self):
  1188. with temp_dir() as extdir:
  1189. self._test_extract_all_with_target(extdir)
  1190. def test_extract_all_with_target_pathlike(self):
  1191. with temp_dir() as extdir:
  1192. self._test_extract_all_with_target(pathlib.Path(extdir))
  1193. def check_file(self, filename, content):
  1194. self.assertTrue(os.path.isfile(filename))
  1195. with open(filename, 'rb') as f:
  1196. self.assertEqual(f.read(), content)
  1197. def test_sanitize_windows_name(self):
  1198. san = zipfile.ZipFile._sanitize_windows_name
  1199. # Passing pathsep in allows this test to work regardless of platform.
  1200. self.assertEqual(san(r',,?,C:,foo,bar/z', ','), r'_,C_,foo,bar/z')
  1201. self.assertEqual(san(r'a\b,c<d>e|f"g?h*i', ','), r'a\b,c_d_e_f_g_h_i')
  1202. self.assertEqual(san('../../foo../../ba..r', '/'), r'foo/ba..r')
  1203. def test_extract_hackers_arcnames_common_cases(self):
  1204. common_hacknames = [
  1205. ('../foo/bar', 'foo/bar'),
  1206. ('foo/../bar', 'foo/bar'),
  1207. ('foo/../../bar', 'foo/bar'),
  1208. ('foo/bar/..', 'foo/bar'),
  1209. ('./../foo/bar', 'foo/bar'),
  1210. ('/foo/bar', 'foo/bar'),
  1211. ('/foo/../bar', 'foo/bar'),
  1212. ('/foo/../../bar', 'foo/bar'),
  1213. ]
  1214. self._test_extract_hackers_arcnames(common_hacknames)
  1215. @unittest.skipIf(os.path.sep != '\\', 'Requires \\ as path separator.')
  1216. def test_extract_hackers_arcnames_windows_only(self):
  1217. """Test combination of path fixing and windows name sanitization."""
  1218. windows_hacknames = [
  1219. (r'..\foo\bar', 'foo/bar'),
  1220. (r'..\/foo\/bar', 'foo/bar'),
  1221. (r'foo/\..\/bar', 'foo/bar'),
  1222. (r'foo\/../\bar', 'foo/bar'),
  1223. (r'C:foo/bar', 'foo/bar'),
  1224. (r'C:/foo/bar', 'foo/bar'),
  1225. (r'C://foo/bar', 'foo/bar'),
  1226. (r'C:\foo\bar', 'foo/bar'),
  1227. (r'//conky/mountpoint/foo/bar', 'foo/bar'),
  1228. (r'\\conky\mountpoint\foo\bar', 'foo/bar'),
  1229. (r'///conky/mountpoint/foo/bar', 'mountpoint/foo/bar'),
  1230. (r'\\\conky\mountpoint\foo\bar', 'mountpoint/foo/bar'),
  1231. (r'//conky//mountpoint/foo/bar', 'mountpoint/foo/bar'),
  1232. (r'\\conky\\mountpoint\foo\bar', 'mountpoint/foo/bar'),
  1233. (r'//?/C:/foo/bar', 'foo/bar'),
  1234. (r'\\?\C:\foo\bar', 'foo/bar'),
  1235. (r'C:/../C:/foo/bar', 'C_/foo/bar'),
  1236. (r'a:b\c<d>e|f"g?h*i', 'b/c_d_e_f_g_h_i'),
  1237. ('../../foo../../ba..r', 'foo/ba..r'),
  1238. ]
  1239. self._test_extract_hackers_arcnames(windows_hacknames)
  1240. @unittest.skipIf(os.path.sep != '/', r'Requires / as path separator.')
  1241. def test_extract_hackers_arcnames_posix_only(self):
  1242. posix_hacknames = [
  1243. ('//foo/bar', 'foo/bar'),
  1244. ('../../foo../../ba..r', 'foo../ba..r'),
  1245. (r'foo/..\bar', r'foo/..\bar'),
  1246. ]
  1247. self._test_extract_hackers_arcnames(posix_hacknames)
  1248. def _test_extract_hackers_arcnames(self, hacknames):
  1249. for arcname, fixedname in hacknames:
  1250. content = b'foobar' + arcname.encode()
  1251. with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipfp:
  1252. zinfo = zipfile.ZipInfo()
  1253. # preserve backslashes
  1254. zinfo.filename = arcname
  1255. zinfo.external_attr = 0o600 << 16
  1256. zipfp.writestr(zinfo, content)
  1257. arcname = arcname.replace(os.sep, "/")
  1258. targetpath = os.path.join('target', 'subdir', 'subsub')
  1259. correctfile = os.path.join(targetpath, *fixedname.split('/'))
  1260. with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
  1261. writtenfile = zipfp.extract(arcname, targetpath)
  1262. self.assertEqual(writtenfile, correctfile,
  1263. msg='extract %r: %r != %r' %
  1264. (arcname, writtenfile, correctfile))
  1265. self.check_file(correctfile, content)
  1266. rmtree('target')
  1267. with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
  1268. zipfp.extractall(targetpath)
  1269. self.check_file(correctfile, content)
  1270. rmtree('target')
  1271. correctfile = os.path.join(os.getcwd(), *fixedname.split('/'))
  1272. with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
  1273. writtenfile = zipfp.extract(arcname)
  1274. self.assertEqual(writtenfile, correctfile,
  1275. msg="extract %r" % arcname)
  1276. self.check_file(correctfile, content)
  1277. rmtree(fixedname.split('/')[0])
  1278. with zipfile.ZipFile(TESTFN2, 'r') as zipfp:
  1279. zipfp.extractall()
  1280. self.check_file(correctfile, content)
  1281. rmtree(fixedname.split('/')[0])
  1282. unlink(TESTFN2)
  1283. class OtherTests(unittest.TestCase):
  1284. def test_open_via_zip_info(self):
  1285. # Create the ZIP archive
  1286. with zipfile.ZipFile(TESTFN2, "w", zipfile.ZIP_STORED) as zipfp:
  1287. zipfp.writestr("name", "foo")
  1288. with self.assertWarns(UserWarning):
  1289. zipfp.writestr("name", "bar")
  1290. self.assertEqual(zipfp.namelist(), ["name"] * 2)
  1291. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  1292. infos = zipfp.infolist()
  1293. data = b""
  1294. for info in infos:
  1295. with zipfp.open(info) as zipopen:
  1296. data += zipopen.read()
  1297. self.assertIn(data, {b"foobar", b"barfoo"})
  1298. data = b""
  1299. for info in infos:
  1300. data += zipfp.read(info)
  1301. self.assertIn(data, {b"foobar", b"barfoo"})
  1302. def test_writestr_extended_local_header_issue1202(self):
  1303. with zipfile.ZipFile(TESTFN2, 'w') as orig_zip:
  1304. for data in 'abcdefghijklmnop':
  1305. zinfo = zipfile.ZipInfo(data)
  1306. zinfo.flag_bits |= zipfile._MASK_USE_DATA_DESCRIPTOR # Include an extended local header.
  1307. orig_zip.writestr(zinfo, data)
  1308. def test_close(self):
  1309. """Check that the zipfile is closed after the 'with' block."""
  1310. with zipfile.ZipFile(TESTFN2, "w") as zipfp:
  1311. for fpath, fdata in SMALL_TEST_DATA:
  1312. zipfp.writestr(fpath, fdata)
  1313. self.assertIsNotNone(zipfp.fp, 'zipfp is not open')
  1314. self.assertIsNone(zipfp.fp, 'zipfp is not closed')
  1315. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  1316. self.assertIsNotNone(zipfp.fp, 'zipfp is not open')
  1317. self.assertIsNone(zipfp.fp, 'zipfp is not closed')
  1318. def test_close_on_exception(self):
  1319. """Check that the zipfile is closed if an exception is raised in the
  1320. 'with' block."""
  1321. with zipfile.ZipFile(TESTFN2, "w") as zipfp:
  1322. for fpath, fdata in SMALL_TEST_DATA:
  1323. zipfp.writestr(fpath, fdata)
  1324. try:
  1325. with zipfile.ZipFile(TESTFN2, "r") as zipfp2:
  1326. raise zipfile.BadZipFile()
  1327. except zipfile.BadZipFile:
  1328. self.assertIsNone(zipfp2.fp, 'zipfp is not closed')
  1329. def test_unsupported_version(self):
  1330. # File has an extract_version of 120
  1331. data = (b'PK\x03\x04x\x00\x00\x00\x00\x00!p\xa1@\x00\x00\x00\x00\x00\x00'
  1332. b'\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00xPK\x01\x02x\x03x\x00\x00\x00\x00'
  1333. b'\x00!p\xa1@\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00'
  1334. b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00\x00xPK\x05\x06'
  1335. b'\x00\x00\x00\x00\x01\x00\x01\x00/\x00\x00\x00\x1f\x00\x00\x00\x00\x00')
  1336. self.assertRaises(NotImplementedError, zipfile.ZipFile,
  1337. io.BytesIO(data), 'r')
  1338. @requires_zlib()
  1339. def test_read_unicode_filenames(self):
  1340. # bug #10801
  1341. fname = findfile('zip_cp437_header.zip')
  1342. with zipfile.ZipFile(fname) as zipfp:
  1343. for name in zipfp.namelist():
  1344. zipfp.open(name).close()
  1345. def test_write_unicode_filenames(self):
  1346. with zipfile.ZipFile(TESTFN, "w") as zf:
  1347. zf.writestr("foo.txt", "Test for unicode filename")
  1348. zf.writestr("\xf6.txt", "Test for unicode filename")
  1349. self.assertIsInstance(zf.infolist()[0].filename, str)
  1350. with zipfile.ZipFile(TESTFN, "r") as zf:
  1351. self.assertEqual(zf.filelist[0].filename, "foo.txt")
  1352. self.assertEqual(zf.filelist[1].filename, "\xf6.txt")
  1353. def test_read_after_write_unicode_filenames(self):
  1354. with zipfile.ZipFile(TESTFN2, 'w') as zipfp:
  1355. zipfp.writestr('приклад', b'sample')
  1356. self.assertEqual(zipfp.read('приклад'), b'sample')
  1357. def test_exclusive_create_zip_file(self):
  1358. """Test exclusive creating a new zipfile."""
  1359. unlink(TESTFN2)
  1360. filename = 'testfile.txt'
  1361. content = b'hello, world. this is some content.'
  1362. with zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED) as zipfp:
  1363. zipfp.writestr(filename, content)
  1364. with self.assertRaises(FileExistsError):
  1365. zipfile.ZipFile(TESTFN2, "x", zipfile.ZIP_STORED)
  1366. with zipfile.ZipFile(TESTFN2, "r") as zipfp:
  1367. self.assertEqual(zipfp.namelist(), [filename])
  1368. self.assertEqual(zipfp.read(filename), content)
  1369. def test_create_non_existent_file_for_append(self):
  1370. if os.path.exists(TESTFN):
  1371. os.unlink(TESTFN)
  1372. filename = 'testfile.txt'
  1373. content = b'hello, world. this is some content.'
  1374. try:
  1375. with zipfile.ZipFile(TESTFN, 'a') as zf:
  1376. zf.writestr(filename, content)
  1377. except OSError:
  1378. self.fail('Could not append data to a non-existent zip file.')
  1379. self.assertTrue(os.path.exists(TESTFN))
  1380. with zipfile.ZipFile(TESTFN, 'r') as zf:
  1381. self.assertEqual(zf.read(filename), content)
  1382. def test_close_erroneous_file(self):
  1383. # This test checks that the ZipFile constructor closes the file object
  1384. # it opens if there's an error in the file. If it doesn't, the
  1385. # traceback holds a reference to the ZipFile object and, indirectly,
  1386. # the file object.
  1387. # On Windows, this causes the os.unlink() call to fail because the
  1388. # underlying file is still open. This is SF bug #412214.
  1389. #
  1390. with open(TESTFN, "w", encoding="utf-8") as fp:
  1391. fp.write("this is not a legal zip file\n")
  1392. try:
  1393. zf = zipfile.ZipFile(TESTFN)
  1394. except zipfile.BadZipFile:
  1395. pass
  1396. def test_is_zip_erroneous_file(self):
  1397. """Check that is_zipfile() correctly identifies non-zip files."""
  1398. # - passing a filename
  1399. with open(TESTFN, "w", encoding='utf-8') as fp:
  1400. fp.write("this is not a legal zip file\n")
  1401. self.assertFalse(zipfile.is_zipfile(TESTFN))
  1402. # - passing a path-like object
  1403. self.assertFalse(zipfile.is_zipfile(pathlib.Path(TESTFN)))
  1404. # - passing a file object
  1405. with open(TESTFN, "rb") as fp:
  1406. self.assertFalse(zipfile.is_zipfile(fp))
  1407. # - passing a file-like object
  1408. fp = io.BytesIO()
  1409. fp.write(b"this is not a legal zip file\n")
  1410. self.assertFalse(zipfile.is_zipfile(fp))
  1411. fp.seek(0, 0)
  1412. self.assertFalse(zipfile.is_zipfile(fp))
  1413. def test_damaged_zipfile(self):
  1414. """Check that zipfiles with missing bytes at the end raise BadZipFile."""
  1415. # - Create a valid zip file
  1416. fp = io.BytesIO()
  1417. with zipfile.ZipFile(fp, mode="w") as zipf:
  1418. zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
  1419. zipfiledata = fp.getvalue()
  1420. # - Now create copies of it missing the last N bytes and make sure
  1421. # a BadZipFile exception is raised when we try to open it
  1422. for N in range(len(zipfiledata)):
  1423. fp = io.BytesIO(zipfiledata[:N])
  1424. self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, fp)
  1425. def test_is_zip_valid_file(self):
  1426. """Check that is_zipfile() correctly identifies zip files."""
  1427. # - passing a filename
  1428. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1429. zipf.writestr("foo.txt", b"O, for a Muse of Fire!")
  1430. self.assertTrue(zipfile.is_zipfile(TESTFN))
  1431. # - passing a file object
  1432. with open(TESTFN, "rb") as fp:
  1433. self.assertTrue(zipfile.is_zipfile(fp))
  1434. fp.seek(0, 0)
  1435. zip_contents = fp.read()
  1436. # - passing a file-like object
  1437. fp = io.BytesIO()
  1438. fp.write(zip_contents)
  1439. self.assertTrue(zipfile.is_zipfile(fp))
  1440. fp.seek(0, 0)
  1441. self.assertTrue(zipfile.is_zipfile(fp))
  1442. def test_non_existent_file_raises_OSError(self):
  1443. # make sure we don't raise an AttributeError when a partially-constructed
  1444. # ZipFile instance is finalized; this tests for regression on SF tracker
  1445. # bug #403871.
  1446. # The bug we're testing for caused an AttributeError to be raised
  1447. # when a ZipFile instance was created for a file that did not
  1448. # exist; the .fp member was not initialized but was needed by the
  1449. # __del__() method. Since the AttributeError is in the __del__(),
  1450. # it is ignored, but the user should be sufficiently annoyed by
  1451. # the message on the output that regression will be noticed
  1452. # quickly.
  1453. self.assertRaises(OSError, zipfile.ZipFile, TESTFN)
  1454. def test_empty_file_raises_BadZipFile(self):
  1455. f = open(TESTFN, 'w', encoding='utf-8')
  1456. f.close()
  1457. self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
  1458. with open(TESTFN, 'w', encoding='utf-8') as fp:
  1459. fp.write("short file")
  1460. self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN)
  1461. def test_negative_central_directory_offset_raises_BadZipFile(self):
  1462. # Zip file containing an empty EOCD record
  1463. buffer = bytearray(b'PK\x05\x06' + b'\0'*18)
  1464. # Set the size of the central directory bytes to become 1,
  1465. # causing the central directory offset to become negative
  1466. for dirsize in 1, 2**32-1:
  1467. buffer[12:16] = struct.pack('<L', dirsize)
  1468. f = io.BytesIO(buffer)
  1469. self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, f)
  1470. def test_closed_zip_raises_ValueError(self):
  1471. """Verify that testzip() doesn't swallow inappropriate exceptions."""
  1472. data = io.BytesIO()
  1473. with zipfile.ZipFile(data, mode="w") as zipf:
  1474. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1475. # This is correct; calling .read on a closed ZipFile should raise
  1476. # a ValueError, and so should calling .testzip. An earlier
  1477. # version of .testzip would swallow this exception (and any other)
  1478. # and report that the first file in the archive was corrupt.
  1479. self.assertRaises(ValueError, zipf.read, "foo.txt")
  1480. self.assertRaises(ValueError, zipf.open, "foo.txt")
  1481. self.assertRaises(ValueError, zipf.testzip)
  1482. self.assertRaises(ValueError, zipf.writestr, "bogus.txt", "bogus")
  1483. with open(TESTFN, 'w', encoding='utf-8') as f:
  1484. f.write('zipfile test data')
  1485. self.assertRaises(ValueError, zipf.write, TESTFN)
  1486. def test_bad_constructor_mode(self):
  1487. """Check that bad modes passed to ZipFile constructor are caught."""
  1488. self.assertRaises(ValueError, zipfile.ZipFile, TESTFN, "q")
  1489. def test_bad_open_mode(self):
  1490. """Check that bad modes passed to ZipFile.open are caught."""
  1491. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1492. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1493. with zipfile.ZipFile(TESTFN, mode="r") as zipf:
  1494. # read the data to make sure the file is there
  1495. zipf.read("foo.txt")
  1496. self.assertRaises(ValueError, zipf.open, "foo.txt", "q")
  1497. # universal newlines support is removed
  1498. self.assertRaises(ValueError, zipf.open, "foo.txt", "U")
  1499. self.assertRaises(ValueError, zipf.open, "foo.txt", "rU")
  1500. def test_read0(self):
  1501. """Check that calling read(0) on a ZipExtFile object returns an empty
  1502. string and doesn't advance file pointer."""
  1503. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1504. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1505. # read the data to make sure the file is there
  1506. with zipf.open("foo.txt") as f:
  1507. for i in range(FIXEDTEST_SIZE):
  1508. self.assertEqual(f.read(0), b'')
  1509. self.assertEqual(f.read(), b"O, for a Muse of Fire!")
  1510. def test_open_non_existent_item(self):
  1511. """Check that attempting to call open() for an item that doesn't
  1512. exist in the archive raises a RuntimeError."""
  1513. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1514. self.assertRaises(KeyError, zipf.open, "foo.txt", "r")
  1515. def test_bad_compression_mode(self):
  1516. """Check that bad compression methods passed to ZipFile.open are
  1517. caught."""
  1518. self.assertRaises(NotImplementedError, zipfile.ZipFile, TESTFN, "w", -1)
  1519. def test_unsupported_compression(self):
  1520. # data is declared as shrunk, but actually deflated
  1521. data = (b'PK\x03\x04.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00'
  1522. b'\x00\x02\x00\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00x\x03\x00PK\x01'
  1523. b'\x02.\x03.\x00\x00\x00\x01\x00\xe4C\xa1@\x00\x00\x00\x00\x02\x00\x00'
  1524. b'\x00\x00\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  1525. b'\x80\x01\x00\x00\x00\x00xPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x00'
  1526. b'/\x00\x00\x00!\x00\x00\x00\x00\x00')
  1527. with zipfile.ZipFile(io.BytesIO(data), 'r') as zipf:
  1528. self.assertRaises(NotImplementedError, zipf.open, 'x')
  1529. def test_null_byte_in_filename(self):
  1530. """Check that a filename containing a null byte is properly
  1531. terminated."""
  1532. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1533. zipf.writestr("foo.txt\x00qqq", b"O, for a Muse of Fire!")
  1534. self.assertEqual(zipf.namelist(), ['foo.txt'])
  1535. def test_struct_sizes(self):
  1536. """Check that ZIP internal structure sizes are calculated correctly."""
  1537. self.assertEqual(zipfile.sizeEndCentDir, 22)
  1538. self.assertEqual(zipfile.sizeCentralDir, 46)
  1539. self.assertEqual(zipfile.sizeEndCentDir64, 56)
  1540. self.assertEqual(zipfile.sizeEndCentDir64Locator, 20)
  1541. def test_comments(self):
  1542. """Check that comments on the archive are handled properly."""
  1543. # check default comment is empty
  1544. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1545. self.assertEqual(zipf.comment, b'')
  1546. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1547. with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
  1548. self.assertEqual(zipfr.comment, b'')
  1549. # check a simple short comment
  1550. comment = b'Bravely taking to his feet, he beat a very brave retreat.'
  1551. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1552. zipf.comment = comment
  1553. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1554. with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
  1555. self.assertEqual(zipf.comment, comment)
  1556. # check a comment of max length
  1557. comment2 = ''.join(['%d' % (i**3 % 10) for i in range((1 << 16)-1)])
  1558. comment2 = comment2.encode("ascii")
  1559. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1560. zipf.comment = comment2
  1561. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1562. with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
  1563. self.assertEqual(zipfr.comment, comment2)
  1564. # check a comment that is too long is truncated
  1565. with zipfile.ZipFile(TESTFN, mode="w") as zipf:
  1566. with self.assertWarns(UserWarning):
  1567. zipf.comment = comment2 + b'oops'
  1568. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1569. with zipfile.ZipFile(TESTFN, mode="r") as zipfr:
  1570. self.assertEqual(zipfr.comment, comment2)
  1571. # check that comments are correctly modified in append mode
  1572. with zipfile.ZipFile(TESTFN,mode="w") as zipf:
  1573. zipf.comment = b"original comment"
  1574. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1575. with zipfile.ZipFile(TESTFN,mode="a") as zipf:
  1576. zipf.comment = b"an updated comment"
  1577. with zipfile.ZipFile(TESTFN,mode="r") as zipf:
  1578. self.assertEqual(zipf.comment, b"an updated comment")
  1579. # check that comments are correctly shortened in append mode
  1580. # and the file is indeed truncated
  1581. with zipfile.ZipFile(TESTFN,mode="w") as zipf:
  1582. zipf.comment = b"original comment that's longer"
  1583. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1584. original_zip_size = os.path.getsize(TESTFN)
  1585. with zipfile.ZipFile(TESTFN,mode="a") as zipf:
  1586. zipf.comment = b"shorter comment"
  1587. self.assertTrue(original_zip_size > os.path.getsize(TESTFN))
  1588. with zipfile.ZipFile(TESTFN,mode="r") as zipf:
  1589. self.assertEqual(zipf.comment, b"shorter comment")
  1590. def test_unicode_comment(self):
  1591. with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
  1592. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1593. with self.assertRaises(TypeError):
  1594. zipf.comment = "this is an error"
  1595. def test_change_comment_in_empty_archive(self):
  1596. with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
  1597. self.assertFalse(zipf.filelist)
  1598. zipf.comment = b"this is a comment"
  1599. with zipfile.ZipFile(TESTFN, "r") as zipf:
  1600. self.assertEqual(zipf.comment, b"this is a comment")
  1601. def test_change_comment_in_nonempty_archive(self):
  1602. with zipfile.ZipFile(TESTFN, "w", zipfile.ZIP_STORED) as zipf:
  1603. zipf.writestr("foo.txt", "O, for a Muse of Fire!")
  1604. with zipfile.ZipFile(TESTFN, "a", zipfile.ZIP_STORED) as zipf:
  1605. self.assertTrue(zipf.filelist)
  1606. zipf.comment = b"this is a comment"
  1607. with zipfile.ZipFile(TESTFN, "r") as zipf:
  1608. self.assertEqual(zipf.comment, b"this is a comment")
  1609. def test_empty_zipfile(self):
  1610. # Check that creating a file in 'w' or 'a' mode and closing without
  1611. # adding any files to the archives creates a valid empty ZIP file
  1612. zipf = zipfile.ZipFile(TESTFN, mode="w")
  1613. zipf.close()
  1614. try:
  1615. zipf = zipfile.ZipFile(TESTFN, mode="r")
  1616. except zipfile.BadZipFile:
  1617. self.fail("Unable to create empty ZIP file in 'w' mode")
  1618. zipf = zipfile.ZipFile(TESTFN, mode="a")
  1619. zipf.close()
  1620. try:
  1621. zipf = zipfile.ZipFile(TESTFN, mode="r")
  1622. except:
  1623. self.fail("Unable to create empty ZIP file in 'a' mode")
  1624. def test_open_empty_file(self):
  1625. # Issue 1710703: Check that opening a file with less than 22 bytes
  1626. # raises a BadZipFile exception (rather than the previously unhelpful
  1627. # OSError)
  1628. f = open(TESTFN, 'w', encoding='utf-8')
  1629. f.close()
  1630. self.assertRaises(zipfile.BadZipFile, zipfile.ZipFile, TESTFN, 'r')
  1631. def test_create_zipinfo_before_1980(self):
  1632. self.assertRaises(ValueError,
  1633. zipfile.ZipInfo, 'seventies', (1979, 1, 1, 0, 0, 0))
  1634. def test_create_empty_zipinfo_repr(self):
  1635. """Before bpo-26185, repr() on empty ZipInfo object was failing."""
  1636. zi = zipfile.ZipInfo(filename="empty")
  1637. self.assertEqual(repr(zi), "<ZipInfo filename='empty' file_size=0>")
  1638. def test_create_empty_zipinfo_default_attributes(self):
  1639. """Ensure all required attributes are set."""
  1640. zi = zipfile.ZipInfo()
  1641. self.assertEqual(zi.orig_filename, "NoName")
  1642. self.assertEqual(zi.filename, "NoName")
  1643. self.assertEqual(zi.date_time, (1980, 1, 1, 0, 0, 0))
  1644. self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
  1645. self.assertEqual(zi.comment, b"")
  1646. self.assertEqual(zi.extra, b"")
  1647. self.assertIn(zi.create_system, (0, 3))
  1648. self.assertEqual(zi.create_version, zipfile.DEFAULT_VERSION)
  1649. self.assertEqual(zi.extract_version, zipfile.DEFAULT_VERSION)
  1650. self.assertEqual(zi.reserved, 0)
  1651. self.assertEqual(zi.flag_bits, 0)
  1652. self.assertEqual(zi.volume, 0)
  1653. self.assertEqual(zi.internal_attr, 0)
  1654. self.assertEqual(zi.external_attr, 0)
  1655. # Before bpo-26185, both were missing
  1656. self.assertEqual(zi.file_size, 0)
  1657. self.assertEqual(zi.compress_size, 0)
  1658. def test_zipfile_with_short_extra_field(self):
  1659. """If an extra field in the header is less than 4 bytes, skip it."""
  1660. zipdata = (
  1661. b'PK\x03\x04\x14\x00\x00\x00\x00\x00\x93\x9b\xad@\x8b\x9e'
  1662. b'\xd9\xd3\x01\x00\x00\x00\x01\x00\x00\x00\x03\x00\x03\x00ab'
  1663. b'c\x00\x00\x00APK\x01\x02\x14\x03\x14\x00\x00\x00\x00'
  1664. b'\x00\x93\x9b\xad@\x8b\x9e\xd9\xd3\x01\x00\x00\x00\x01\x00\x00'
  1665. b'\x00\x03\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00'
  1666. b'\x00\x00\x00abc\x00\x00PK\x05\x06\x00\x00\x00\x00'
  1667. b'\x01\x00\x01\x003\x00\x00\x00%\x00\x00\x00\x00\x00'
  1668. )
  1669. with zipfile.ZipFile(io.BytesIO(zipdata), 'r') as zipf:
  1670. # testzip returns the name of the first corrupt file, or None
  1671. self.assertIsNone(zipf.testzip())
  1672. def test_open_conflicting_handles(self):
  1673. # It's only possible to open one writable file handle at a time
  1674. msg1 = b"It's fun to charter an accountant!"
  1675. msg2 = b"And sail the wide accountant sea"
  1676. msg3 = b"To find, explore the funds offshore"
  1677. with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_STORED) as zipf:
  1678. with zipf.open('foo', mode='w') as w2:
  1679. w2.write(msg1)
  1680. with zipf.open('bar', mode='w') as w1:
  1681. with self.assertRaises(ValueError):
  1682. zipf.open('handle', mode='w')
  1683. with self.assertRaises(ValueError):
  1684. zipf.open('foo', mode='r')
  1685. with self.assertRaises(ValueError):
  1686. zipf.writestr('str', 'abcde')
  1687. with self.assertRaises(ValueError):
  1688. zipf.write(__file__, 'file')
  1689. with self.assertRaises(ValueError):
  1690. zipf.close()
  1691. w1.write(msg2)
  1692. with zipf.open('baz', mode='w') as w2:
  1693. w2.write(msg3)
  1694. with zipfile.ZipFile(TESTFN2, 'r') as zipf:
  1695. self.assertEqual(zipf.read('foo'), msg1)
  1696. self.assertEqual(zipf.read('bar'), msg2)
  1697. self.assertEqual(zipf.read('baz'), msg3)
  1698. self.assertEqual(zipf.namelist(), ['foo', 'bar', 'baz'])
  1699. def test_seek_tell(self):
  1700. # Test seek functionality
  1701. txt = b"Where's Bruce?"
  1702. bloc = txt.find(b"Bruce")
  1703. # Check seek on a file
  1704. with zipfile.ZipFile(TESTFN, "w") as zipf:
  1705. zipf.writestr("foo.txt", txt)
  1706. with zipfile.ZipFile(TESTFN, "r") as zipf:
  1707. with zipf.open("foo.txt", "r") as fp:
  1708. fp.seek(bloc, os.SEEK_SET)
  1709. self.assertEqual(fp.tell(), bloc)
  1710. fp.seek(-bloc, os.SEEK_CUR)
  1711. self.assertEqual(fp.tell(), 0)
  1712. fp.seek(bloc, os.SEEK_CUR)
  1713. self.assertEqual(fp.tell(), bloc)
  1714. self.assertEqual(fp.read(5), txt[bloc:bloc+5])
  1715. fp.seek(0, os.SEEK_END)
  1716. self.assertEqual(fp.tell(), len(txt))
  1717. fp.seek(0, os.SEEK_SET)
  1718. self.assertEqual(fp.tell(), 0)
  1719. # Check seek on memory file
  1720. data = io.BytesIO()
  1721. with zipfile.ZipFile(data, mode="w") as zipf:
  1722. zipf.writestr("foo.txt", txt)
  1723. with zipfile.ZipFile(data, mode="r") as zipf:
  1724. with zipf.open("foo.txt", "r") as fp:
  1725. fp.seek(bloc, os.SEEK_SET)
  1726. self.assertEqual(fp.tell(), bloc)
  1727. fp.seek(-bloc, os.SEEK_CUR)
  1728. self.assertEqual(fp.tell(), 0)
  1729. fp.seek(bloc, os.SEEK_CUR)
  1730. self.assertEqual(fp.tell(), bloc)
  1731. self.assertEqual(fp.read(5), txt[bloc:bloc+5])
  1732. fp.seek(0, os.SEEK_END)
  1733. self.assertEqual(fp.tell(), len(txt))
  1734. fp.seek(0, os.SEEK_SET)
  1735. self.assertEqual(fp.tell(), 0)
  1736. @requires_bz2()
  1737. def test_decompress_without_3rd_party_library(self):
  1738. data = b'PK\x05\x06\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
  1739. zip_file = io.BytesIO(data)
  1740. with zipfile.ZipFile(zip_file, 'w', compression=zipfile.ZIP_BZIP2) as zf:
  1741. zf.writestr('a.txt', b'a')
  1742. with mock.patch('zipfile.bz2', None):
  1743. with zipfile.ZipFile(zip_file) as zf:
  1744. self.assertRaises(RuntimeError, zf.extract, 'a.txt')
  1745. def tearDown(self):
  1746. unlink(TESTFN)
  1747. unlink(TESTFN2)
  1748. class AbstractBadCrcTests:
  1749. def test_testzip_with_bad_crc(self):
  1750. """Tests that files with bad CRCs return their name from testzip."""
  1751. zipdata = self.zip_with_bad_crc
  1752. with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
  1753. # testzip returns the name of the first corrupt file, or None
  1754. self.assertEqual('afile', zipf.testzip())
  1755. def test_read_with_bad_crc(self):
  1756. """Tests that files with bad CRCs raise a BadZipFile exception when read."""
  1757. zipdata = self.zip_with_bad_crc
  1758. # Using ZipFile.read()
  1759. with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
  1760. self.assertRaises(zipfile.BadZipFile, zipf.read, 'afile')
  1761. # Using ZipExtFile.read()
  1762. with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
  1763. with zipf.open('afile', 'r') as corrupt_file:
  1764. self.assertRaises(zipfile.BadZipFile, corrupt_file.read)
  1765. # Same with small reads (in order to exercise the buffering logic)
  1766. with zipfile.ZipFile(io.BytesIO(zipdata), mode="r") as zipf:
  1767. with zipf.open('afile', 'r') as corrupt_file:
  1768. corrupt_file.MIN_READ_SIZE = 2
  1769. with self.assertRaises(zipfile.BadZipFile):
  1770. while corrupt_file.read(2):
  1771. pass
  1772. class StoredBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
  1773. compression = zipfile.ZIP_STORED
  1774. zip_with_bad_crc = (
  1775. b'PK\003\004\024\0\0\0\0\0 \213\212;:r'
  1776. b'\253\377\f\0\0\0\f\0\0\0\005\0\0\000af'
  1777. b'ilehello,AworldP'
  1778. b'K\001\002\024\003\024\0\0\0\0\0 \213\212;:'
  1779. b'r\253\377\f\0\0\0\f\0\0\0\005\0\0\0\0'
  1780. b'\0\0\0\0\0\0\0\200\001\0\0\0\000afi'
  1781. b'lePK\005\006\0\0\0\0\001\0\001\0003\000'
  1782. b'\0\0/\0\0\0\0\0')
  1783. @requires_zlib()
  1784. class DeflateBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
  1785. compression = zipfile.ZIP_DEFLATED
  1786. zip_with_bad_crc = (
  1787. b'PK\x03\x04\x14\x00\x00\x00\x08\x00n}\x0c=FA'
  1788. b'KE\x10\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
  1789. b'ile\xcbH\xcd\xc9\xc9W(\xcf/\xcaI\xc9\xa0'
  1790. b'=\x13\x00PK\x01\x02\x14\x03\x14\x00\x00\x00\x08\x00n'
  1791. b'}\x0c=FAKE\x10\x00\x00\x00n\x00\x00\x00\x05'
  1792. b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x01\x00\x00\x00'
  1793. b'\x00afilePK\x05\x06\x00\x00\x00\x00\x01\x00'
  1794. b'\x01\x003\x00\x00\x003\x00\x00\x00\x00\x00')
  1795. @requires_bz2()
  1796. class Bzip2BadCrcTests(AbstractBadCrcTests, unittest.TestCase):
  1797. compression = zipfile.ZIP_BZIP2
  1798. zip_with_bad_crc = (
  1799. b'PK\x03\x04\x14\x03\x00\x00\x0c\x00nu\x0c=FA'
  1800. b'KE8\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
  1801. b'ileBZh91AY&SY\xd4\xa8\xca'
  1802. b'\x7f\x00\x00\x0f\x11\x80@\x00\x06D\x90\x80 \x00 \xa5'
  1803. b'P\xd9!\x03\x03\x13\x13\x13\x89\xa9\xa9\xc2u5:\x9f'
  1804. b'\x8b\xb9"\x9c(HjTe?\x80PK\x01\x02\x14'
  1805. b'\x03\x14\x03\x00\x00\x0c\x00nu\x0c=FAKE8'
  1806. b'\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00'
  1807. b'\x00 \x80\x80\x81\x00\x00\x00\x00afilePK'
  1808. b'\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00\x00[\x00'
  1809. b'\x00\x00\x00\x00')
  1810. @requires_lzma()
  1811. class LzmaBadCrcTests(AbstractBadCrcTests, unittest.TestCase):
  1812. compression = zipfile.ZIP_LZMA
  1813. zip_with_bad_crc = (
  1814. b'PK\x03\x04\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
  1815. b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00af'
  1816. b'ile\t\x04\x05\x00]\x00\x00\x00\x04\x004\x19I'
  1817. b'\xee\x8d\xe9\x17\x89:3`\tq!.8\x00PK'
  1818. b'\x01\x02\x14\x03\x14\x03\x00\x00\x0e\x00nu\x0c=FA'
  1819. b'KE\x1b\x00\x00\x00n\x00\x00\x00\x05\x00\x00\x00\x00\x00'
  1820. b'\x00\x00\x00\x00 \x80\x80\x81\x00\x00\x00\x00afil'
  1821. b'ePK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x003\x00\x00'
  1822. b'\x00>\x00\x00\x00\x00\x00')
  1823. class DecryptionTests(unittest.TestCase):
  1824. """Check that ZIP decryption works. Since the library does not
  1825. support encryption at the moment, we use a pre-generated encrypted
  1826. ZIP file."""
  1827. data = (
  1828. b'PK\x03\x04\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00\x1a\x00'
  1829. b'\x00\x00\x08\x00\x00\x00test.txt\xfa\x10\xa0gly|\xfa-\xc5\xc0=\xf9y'
  1830. b'\x18\xe0\xa8r\xb3Z}Lg\xbc\xae\xf9|\x9b\x19\xe4\x8b\xba\xbb)\x8c\xb0\xdbl'
  1831. b'PK\x01\x02\x14\x00\x14\x00\x01\x00\x00\x00n\x92i.#y\xef?&\x00\x00\x00'
  1832. b'\x1a\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x01\x00 \x00\xb6\x81'
  1833. b'\x00\x00\x00\x00test.txtPK\x05\x06\x00\x00\x00\x00\x01\x00\x01\x006\x00'
  1834. b'\x00\x00L\x00\x00\x00\x00\x00' )
  1835. data2 = (
  1836. b'PK\x03\x04\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02'
  1837. b'\x00\x00\x04\x00\x15\x00zeroUT\t\x00\x03\xd6\x8b\x92G\xda\x8b\x92GUx\x04'
  1838. b'\x00\xe8\x03\xe8\x03\xc7<M\xb5a\xceX\xa3Y&\x8b{oE\xd7\x9d\x8c\x98\x02\xc0'
  1839. b'PK\x07\x08xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00PK\x01\x02\x17\x03'
  1840. b'\x14\x00\t\x00\x08\x00\xcf}38xu\xaa\xb2\x14\x00\x00\x00\x00\x02\x00\x00'
  1841. b'\x04\x00\r\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa4\x81\x00\x00\x00\x00ze'
  1842. b'roUT\x05\x00\x03\xd6\x8b\x92GUx\x00\x00PK\x05\x06\x00\x00\x00\x00\x01'
  1843. b'\x00\x01\x00?\x00\x00\x00[\x00\x00\x00\x00\x00' )
  1844. plain = b'zipfile.py encryption test'
  1845. plain2 = b'\x00'*512
  1846. def setUp(self):
  1847. with open(TESTFN, "wb") as fp:
  1848. fp.write(self.data)
  1849. self.zip = zipfile.ZipFile(TESTFN, "r")
  1850. with open(TESTFN2, "wb") as fp:
  1851. fp.write(self.data2)
  1852. self.zip2 = zipfile.ZipFile(TESTFN2, "r")
  1853. def tearDown(self):
  1854. self.zip.close()
  1855. os.unlink(TESTFN)
  1856. self.zip2.close()
  1857. os.unlink(TESTFN2)
  1858. def test_no_password(self):
  1859. # Reading the encrypted file without password
  1860. # must generate a RunTime exception
  1861. self.assertRaises(RuntimeError, self.zip.read, "test.txt")
  1862. self.assertRaises(RuntimeError, self.zip2.read, "zero")
  1863. def test_bad_password(self):
  1864. self.zip.setpassword(b"perl")
  1865. self.assertRaises(RuntimeError, self.zip.read, "test.txt")
  1866. self.zip2.setpassword(b"perl")
  1867. self.assertRaises(RuntimeError, self.zip2.read, "zero")
  1868. @requires_zlib()
  1869. def test_good_password(self):
  1870. self.zip.setpassword(b"python")
  1871. self.assertEqual(self.zip.read("test.txt"), self.plain)
  1872. self.zip2.setpassword(b"12345")
  1873. self.assertEqual(self.zip2.read("zero"), self.plain2)
  1874. def test_unicode_password(self):
  1875. expected_msg = "pwd: expected bytes, got str"
  1876. with self.assertRaisesRegex(TypeError, expected_msg):
  1877. self.zip.setpassword("unicode")
  1878. with self.assertRaisesRegex(TypeError, expected_msg):
  1879. self.zip.read("test.txt", "python")
  1880. with self.assertRaisesRegex(TypeError, expected_msg):
  1881. self.zip.open("test.txt", pwd="python")
  1882. with self.assertRaisesRegex(TypeError, expected_msg):
  1883. self.zip.extract("test.txt", pwd="python")
  1884. with self.assertRaisesRegex(TypeError, expected_msg):
  1885. self.zip.pwd = "python"
  1886. self.zip.open("test.txt")
  1887. def test_seek_tell(self):
  1888. self.zip.setpassword(b"python")
  1889. txt = self.plain
  1890. test_word = b'encryption'
  1891. bloc = txt.find(test_word)
  1892. bloc_len = len(test_word)
  1893. with self.zip.open("test.txt", "r") as fp:
  1894. fp.seek(bloc, os.SEEK_SET)
  1895. self.assertEqual(fp.tell(), bloc)
  1896. fp.seek(-bloc, os.SEEK_CUR)
  1897. self.assertEqual(fp.tell(), 0)
  1898. fp.seek(bloc, os.SEEK_CUR)
  1899. self.assertEqual(fp.tell(), bloc)
  1900. self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len])
  1901. # Make sure that the second read after seeking back beyond
  1902. # _readbuffer returns the same content (ie. rewind to the start of
  1903. # the file to read forward to the required position).
  1904. old_read_size = fp.MIN_READ_SIZE
  1905. fp.MIN_READ_SIZE = 1
  1906. fp._readbuffer = b''
  1907. fp._offset = 0
  1908. fp.seek(0, os.SEEK_SET)
  1909. self.assertEqual(fp.tell(), 0)
  1910. fp.seek(bloc, os.SEEK_CUR)
  1911. self.assertEqual(fp.read(bloc_len), txt[bloc:bloc+bloc_len])
  1912. fp.MIN_READ_SIZE = old_read_size
  1913. fp.seek(0, os.SEEK_END)
  1914. self.assertEqual(fp.tell(), len(txt))
  1915. fp.seek(0, os.SEEK_SET)
  1916. self.assertEqual(fp.tell(), 0)
  1917. # Read the file completely to definitely call any eof integrity
  1918. # checks (crc) and make sure they still pass.
  1919. fp.read()
  1920. class AbstractTestsWithRandomBinaryFiles:
  1921. @classmethod
  1922. def setUpClass(cls):
  1923. datacount = randint(16, 64)*1024 + randint(1, 1024)
  1924. cls.data = b''.join(struct.pack('<f', random()*randint(-1000, 1000))
  1925. for i in range(datacount))
  1926. def setUp(self):
  1927. # Make a source file with some lines
  1928. with open(TESTFN, "wb") as fp:
  1929. fp.write(self.data)
  1930. def tearDown(self):
  1931. unlink(TESTFN)
  1932. unlink(TESTFN2)
  1933. def make_test_archive(self, f, compression):
  1934. # Create the ZIP archive
  1935. with zipfile.ZipFile(f, "w", compression) as zipfp:
  1936. zipfp.write(TESTFN, "another.name")
  1937. zipfp.write(TESTFN, TESTFN)
  1938. def zip_test(self, f, compression):
  1939. self.make_test_archive(f, compression)
  1940. # Read the ZIP archive
  1941. with zipfile.ZipFile(f, "r", compression) as zipfp:
  1942. testdata = zipfp.read(TESTFN)
  1943. self.assertEqual(len(testdata), len(self.data))
  1944. self.assertEqual(testdata, self.data)
  1945. self.assertEqual(zipfp.read("another.name"), self.data)
  1946. def test_read(self):
  1947. for f in get_files(self):
  1948. self.zip_test(f, self.compression)
  1949. def zip_open_test(self, f, compression):
  1950. self.make_test_archive(f, compression)
  1951. # Read the ZIP archive
  1952. with zipfile.ZipFile(f, "r", compression) as zipfp:
  1953. zipdata1 = []
  1954. with zipfp.open(TESTFN) as zipopen1:
  1955. while True:
  1956. read_data = zipopen1.read(256)
  1957. if not read_data:
  1958. break
  1959. zipdata1.append(read_data)
  1960. zipdata2 = []
  1961. with zipfp.open("another.name") as zipopen2:
  1962. while True:
  1963. read_data = zipopen2.read(256)
  1964. if not read_data:
  1965. break
  1966. zipdata2.append(read_data)
  1967. testdata1 = b''.join(zipdata1)
  1968. self.assertEqual(len(testdata1), len(self.data))
  1969. self.assertEqual(testdata1, self.data)
  1970. testdata2 = b''.join(zipdata2)
  1971. self.assertEqual(len(testdata2), len(self.data))
  1972. self.assertEqual(testdata2, self.data)
  1973. def test_open(self):
  1974. for f in get_files(self):
  1975. self.zip_open_test(f, self.compression)
  1976. def zip_random_open_test(self, f, compression):
  1977. self.make_test_archive(f, compression)
  1978. # Read the ZIP archive
  1979. with zipfile.ZipFile(f, "r", compression) as zipfp:
  1980. zipdata1 = []
  1981. with zipfp.open(TESTFN) as zipopen1:
  1982. while True:
  1983. read_data = zipopen1.read(randint(1, 1024))
  1984. if not read_data:
  1985. break
  1986. zipdata1.append(read_data)
  1987. testdata = b''.join(zipdata1)
  1988. self.assertEqual(len(testdata), len(self.data))
  1989. self.assertEqual(testdata, self.data)
  1990. def test_random_open(self):
  1991. for f in get_files(self):
  1992. self.zip_random_open_test(f, self.compression)
  1993. class StoredTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
  1994. unittest.TestCase):
  1995. compression = zipfile.ZIP_STORED
  1996. @requires_zlib()
  1997. class DeflateTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
  1998. unittest.TestCase):
  1999. compression = zipfile.ZIP_DEFLATED
  2000. @requires_bz2()
  2001. class Bzip2TestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
  2002. unittest.TestCase):
  2003. compression = zipfile.ZIP_BZIP2
  2004. @requires_lzma()
  2005. class LzmaTestsWithRandomBinaryFiles(AbstractTestsWithRandomBinaryFiles,
  2006. unittest.TestCase):
  2007. compression = zipfile.ZIP_LZMA
  2008. # Provide the tell() method but not seek()
  2009. class Tellable:
  2010. def __init__(self, fp):
  2011. self.fp = fp
  2012. self.offset = 0
  2013. def write(self, data):
  2014. n = self.fp.write(data)
  2015. self.offset += n
  2016. return n
  2017. def tell(self):
  2018. return self.offset
  2019. def flush(self):
  2020. self.fp.flush()
  2021. class Unseekable:
  2022. def __init__(self, fp):
  2023. self.fp = fp
  2024. def write(self, data):
  2025. return self.fp.write(data)
  2026. def flush(self):
  2027. self.fp.flush()
  2028. class UnseekableTests(unittest.TestCase):
  2029. def test_writestr(self):
  2030. for wrapper in (lambda f: f), Tellable, Unseekable:
  2031. with self.subTest(wrapper=wrapper):
  2032. f = io.BytesIO()
  2033. f.write(b'abc')
  2034. bf = io.BufferedWriter(f)
  2035. with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp:
  2036. zipfp.writestr('ones', b'111')
  2037. zipfp.writestr('twos', b'222')
  2038. self.assertEqual(f.getvalue()[:5], b'abcPK')
  2039. with zipfile.ZipFile(f, mode='r') as zipf:
  2040. with zipf.open('ones') as zopen:
  2041. self.assertEqual(zopen.read(), b'111')
  2042. with zipf.open('twos') as zopen:
  2043. self.assertEqual(zopen.read(), b'222')
  2044. def test_write(self):
  2045. for wrapper in (lambda f: f), Tellable, Unseekable:
  2046. with self.subTest(wrapper=wrapper):
  2047. f = io.BytesIO()
  2048. f.write(b'abc')
  2049. bf = io.BufferedWriter(f)
  2050. with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipfp:
  2051. self.addCleanup(unlink, TESTFN)
  2052. with open(TESTFN, 'wb') as f2:
  2053. f2.write(b'111')
  2054. zipfp.write(TESTFN, 'ones')
  2055. with open(TESTFN, 'wb') as f2:
  2056. f2.write(b'222')
  2057. zipfp.write(TESTFN, 'twos')
  2058. self.assertEqual(f.getvalue()[:5], b'abcPK')
  2059. with zipfile.ZipFile(f, mode='r') as zipf:
  2060. with zipf.open('ones') as zopen:
  2061. self.assertEqual(zopen.read(), b'111')
  2062. with zipf.open('twos') as zopen:
  2063. self.assertEqual(zopen.read(), b'222')
  2064. def test_open_write(self):
  2065. for wrapper in (lambda f: f), Tellable, Unseekable:
  2066. with self.subTest(wrapper=wrapper):
  2067. f = io.BytesIO()
  2068. f.write(b'abc')
  2069. bf = io.BufferedWriter(f)
  2070. with zipfile.ZipFile(wrapper(bf), 'w', zipfile.ZIP_STORED) as zipf:
  2071. with zipf.open('ones', 'w') as zopen:
  2072. zopen.write(b'111')
  2073. with zipf.open('twos', 'w') as zopen:
  2074. zopen.write(b'222')
  2075. self.assertEqual(f.getvalue()[:5], b'abcPK')
  2076. with zipfile.ZipFile(f) as zipf:
  2077. self.assertEqual(zipf.read('ones'), b'111')
  2078. self.assertEqual(zipf.read('twos'), b'222')
  2079. @requires_zlib()
  2080. class TestsWithMultipleOpens(unittest.TestCase):
  2081. @classmethod
  2082. def setUpClass(cls):
  2083. cls.data1 = b'111' + randbytes(10000)
  2084. cls.data2 = b'222' + randbytes(10000)
  2085. def make_test_archive(self, f):
  2086. # Create the ZIP archive
  2087. with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipfp:
  2088. zipfp.writestr('ones', self.data1)
  2089. zipfp.writestr('twos', self.data2)
  2090. def test_same_file(self):
  2091. # Verify that (when the ZipFile is in control of creating file objects)
  2092. # multiple open() calls can be made without interfering with each other.
  2093. for f in get_files(self):
  2094. self.make_test_archive(f)
  2095. with zipfile.ZipFile(f, mode="r") as zipf:
  2096. with zipf.open('ones') as zopen1, zipf.open('ones') as zopen2:
  2097. data1 = zopen1.read(500)
  2098. data2 = zopen2.read(500)
  2099. data1 += zopen1.read()
  2100. data2 += zopen2.read()
  2101. self.assertEqual(data1, data2)
  2102. self.assertEqual(data1, self.data1)
  2103. def test_different_file(self):
  2104. # Verify that (when the ZipFile is in control of creating file objects)
  2105. # multiple open() calls can be made without interfering with each other.
  2106. for f in get_files(self):
  2107. self.make_test_archive(f)
  2108. with zipfile.ZipFile(f, mode="r") as zipf:
  2109. with zipf.open('ones') as zopen1, zipf.open('twos') as zopen2:
  2110. data1 = zopen1.read(500)
  2111. data2 = zopen2.read(500)
  2112. data1 += zopen1.read()
  2113. data2 += zopen2.read()
  2114. self.assertEqual(data1, self.data1)
  2115. self.assertEqual(data2, self.data2)
  2116. def test_interleaved(self):
  2117. # Verify that (when the ZipFile is in control of creating file objects)
  2118. # multiple open() calls can be made without interfering with each other.
  2119. for f in get_files(self):
  2120. self.make_test_archive(f)
  2121. with zipfile.ZipFile(f, mode="r") as zipf:
  2122. with zipf.open('ones') as zopen1:
  2123. data1 = zopen1.read(500)
  2124. with zipf.open('twos') as zopen2:
  2125. data2 = zopen2.read(500)
  2126. data1 += zopen1.read()
  2127. data2 += zopen2.read()
  2128. self.assertEqual(data1, self.data1)
  2129. self.assertEqual(data2, self.data2)
  2130. def test_read_after_close(self):
  2131. for f in get_files(self):
  2132. self.make_test_archive(f)
  2133. with contextlib.ExitStack() as stack:
  2134. with zipfile.ZipFile(f, 'r') as zipf:
  2135. zopen1 = stack.enter_context(zipf.open('ones'))
  2136. zopen2 = stack.enter_context(zipf.open('twos'))
  2137. data1 = zopen1.read(500)
  2138. data2 = zopen2.read(500)
  2139. data1 += zopen1.read()
  2140. data2 += zopen2.read()
  2141. self.assertEqual(data1, self.data1)
  2142. self.assertEqual(data2, self.data2)
  2143. def test_read_after_write(self):
  2144. for f in get_files(self):
  2145. with zipfile.ZipFile(f, 'w', zipfile.ZIP_DEFLATED) as zipf:
  2146. zipf.writestr('ones', self.data1)
  2147. zipf.writestr('twos', self.data2)
  2148. with zipf.open('ones') as zopen1:
  2149. data1 = zopen1.read(500)
  2150. self.assertEqual(data1, self.data1[:500])
  2151. with zipfile.ZipFile(f, 'r') as zipf:
  2152. data1 = zipf.read('ones')
  2153. data2 = zipf.read('twos')
  2154. self.assertEqual(data1, self.data1)
  2155. self.assertEqual(data2, self.data2)
  2156. def test_write_after_read(self):
  2157. for f in get_files(self):
  2158. with zipfile.ZipFile(f, "w", zipfile.ZIP_DEFLATED) as zipf:
  2159. zipf.writestr('ones', self.data1)
  2160. with zipf.open('ones') as zopen1:
  2161. zopen1.read(500)
  2162. zipf.writestr('twos', self.data2)
  2163. with zipfile.ZipFile(f, 'r') as zipf:
  2164. data1 = zipf.read('ones')
  2165. data2 = zipf.read('twos')
  2166. self.assertEqual(data1, self.data1)
  2167. self.assertEqual(data2, self.data2)
  2168. def test_many_opens(self):
  2169. # Verify that read() and open() promptly close the file descriptor,
  2170. # and don't rely on the garbage collector to free resources.
  2171. startcount = fd_count()
  2172. self.make_test_archive(TESTFN2)
  2173. with zipfile.ZipFile(TESTFN2, mode="r") as zipf:
  2174. for x in range(100):
  2175. zipf.read('ones')
  2176. with zipf.open('ones') as zopen1:
  2177. pass
  2178. self.assertEqual(startcount, fd_count())
  2179. def test_write_while_reading(self):
  2180. with zipfile.ZipFile(TESTFN2, 'w', zipfile.ZIP_DEFLATED) as zipf:
  2181. zipf.writestr('ones', self.data1)
  2182. with zipfile.ZipFile(TESTFN2, 'a', zipfile.ZIP_DEFLATED) as zipf:
  2183. with zipf.open('ones', 'r') as r1:
  2184. data1 = r1.read(500)
  2185. with zipf.open('twos', 'w') as w1:
  2186. w1.write(self.data2)
  2187. data1 += r1.read()
  2188. self.assertEqual(data1, self.data1)
  2189. with zipfile.ZipFile(TESTFN2) as zipf:
  2190. self.assertEqual(zipf.read('twos'), self.data2)
  2191. def tearDown(self):
  2192. unlink(TESTFN2)
  2193. class TestWithDirectory(unittest.TestCase):
  2194. def setUp(self):
  2195. os.mkdir(TESTFN2)
  2196. def test_extract_dir(self):
  2197. with zipfile.ZipFile(findfile("zipdir.zip")) as zipf:
  2198. zipf.extractall(TESTFN2)
  2199. self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a")))
  2200. self.assertTrue(os.path.isdir(os.path.join(TESTFN2, "a", "b")))
  2201. self.assertTrue(os.path.exists(os.path.join(TESTFN2, "a", "b", "c")))
  2202. def test_bug_6050(self):
  2203. # Extraction should succeed if directories already exist
  2204. os.mkdir(os.path.join(TESTFN2, "a"))
  2205. self.test_extract_dir()
  2206. def test_write_dir(self):
  2207. dirpath = os.path.join(TESTFN2, "x")
  2208. os.mkdir(dirpath)
  2209. mode = os.stat(dirpath).st_mode & 0xFFFF
  2210. with zipfile.ZipFile(TESTFN, "w") as zipf:
  2211. zipf.write(dirpath)
  2212. zinfo = zipf.filelist[0]
  2213. self.assertTrue(zinfo.filename.endswith("/x/"))
  2214. self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
  2215. zipf.write(dirpath, "y")
  2216. zinfo = zipf.filelist[1]
  2217. self.assertTrue(zinfo.filename, "y/")
  2218. self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
  2219. with zipfile.ZipFile(TESTFN, "r") as zipf:
  2220. zinfo = zipf.filelist[0]
  2221. self.assertTrue(zinfo.filename.endswith("/x/"))
  2222. self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
  2223. zinfo = zipf.filelist[1]
  2224. self.assertTrue(zinfo.filename, "y/")
  2225. self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
  2226. target = os.path.join(TESTFN2, "target")
  2227. os.mkdir(target)
  2228. zipf.extractall(target)
  2229. self.assertTrue(os.path.isdir(os.path.join(target, "y")))
  2230. self.assertEqual(len(os.listdir(target)), 2)
  2231. def test_writestr_dir(self):
  2232. os.mkdir(os.path.join(TESTFN2, "x"))
  2233. with zipfile.ZipFile(TESTFN, "w") as zipf:
  2234. zipf.writestr("x/", b'')
  2235. zinfo = zipf.filelist[0]
  2236. self.assertEqual(zinfo.filename, "x/")
  2237. self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
  2238. with zipfile.ZipFile(TESTFN, "r") as zipf:
  2239. zinfo = zipf.filelist[0]
  2240. self.assertTrue(zinfo.filename.endswith("x/"))
  2241. self.assertEqual(zinfo.external_attr, (0o40775 << 16) | 0x10)
  2242. target = os.path.join(TESTFN2, "target")
  2243. os.mkdir(target)
  2244. zipf.extractall(target)
  2245. self.assertTrue(os.path.isdir(os.path.join(target, "x")))
  2246. self.assertEqual(os.listdir(target), ["x"])
  2247. def test_mkdir(self):
  2248. with zipfile.ZipFile(TESTFN, "w") as zf:
  2249. zf.mkdir("directory")
  2250. zinfo = zf.filelist[0]
  2251. self.assertEqual(zinfo.filename, "directory/")
  2252. self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
  2253. zf.mkdir("directory2/")
  2254. zinfo = zf.filelist[1]
  2255. self.assertEqual(zinfo.filename, "directory2/")
  2256. self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
  2257. zf.mkdir("directory3", mode=0o777)
  2258. zinfo = zf.filelist[2]
  2259. self.assertEqual(zinfo.filename, "directory3/")
  2260. self.assertEqual(zinfo.external_attr, (0o40777 << 16) | 0x10)
  2261. old_zinfo = zipfile.ZipInfo("directory4/")
  2262. old_zinfo.external_attr = (0o40777 << 16) | 0x10
  2263. old_zinfo.CRC = 0
  2264. old_zinfo.file_size = 0
  2265. old_zinfo.compress_size = 0
  2266. zf.mkdir(old_zinfo)
  2267. new_zinfo = zf.filelist[3]
  2268. self.assertEqual(old_zinfo.filename, "directory4/")
  2269. self.assertEqual(old_zinfo.external_attr, new_zinfo.external_attr)
  2270. target = os.path.join(TESTFN2, "target")
  2271. os.mkdir(target)
  2272. zf.extractall(target)
  2273. self.assertEqual(set(os.listdir(target)), {"directory", "directory2", "directory3", "directory4"})
  2274. def test_create_directory_with_write(self):
  2275. with zipfile.ZipFile(TESTFN, "w") as zf:
  2276. zf.writestr(zipfile.ZipInfo('directory/'), '')
  2277. zinfo = zf.filelist[0]
  2278. self.assertEqual(zinfo.filename, "directory/")
  2279. directory = os.path.join(TESTFN2, "directory2")
  2280. os.mkdir(directory)
  2281. mode = os.stat(directory).st_mode
  2282. zf.write(directory, arcname="directory2/")
  2283. zinfo = zf.filelist[1]
  2284. self.assertEqual(zinfo.filename, "directory2/")
  2285. self.assertEqual(zinfo.external_attr, (mode << 16) | 0x10)
  2286. target = os.path.join(TESTFN2, "target")
  2287. os.mkdir(target)
  2288. zf.extractall(target)
  2289. self.assertEqual(set(os.listdir(target)), {"directory", "directory2"})
  2290. def tearDown(self):
  2291. rmtree(TESTFN2)
  2292. if os.path.exists(TESTFN):
  2293. unlink(TESTFN)
  2294. class ZipInfoTests(unittest.TestCase):
  2295. def test_from_file(self):
  2296. zi = zipfile.ZipInfo.from_file(__file__)
  2297. self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py')
  2298. self.assertFalse(zi.is_dir())
  2299. self.assertEqual(zi.file_size, os.path.getsize(__file__))
  2300. def test_from_file_pathlike(self):
  2301. zi = zipfile.ZipInfo.from_file(pathlib.Path(__file__))
  2302. self.assertEqual(posixpath.basename(zi.filename), 'test_zipfile.py')
  2303. self.assertFalse(zi.is_dir())
  2304. self.assertEqual(zi.file_size, os.path.getsize(__file__))
  2305. def test_from_file_bytes(self):
  2306. zi = zipfile.ZipInfo.from_file(os.fsencode(__file__), 'test')
  2307. self.assertEqual(posixpath.basename(zi.filename), 'test')
  2308. self.assertFalse(zi.is_dir())
  2309. self.assertEqual(zi.file_size, os.path.getsize(__file__))
  2310. def test_from_file_fileno(self):
  2311. with open(__file__, 'rb') as f:
  2312. zi = zipfile.ZipInfo.from_file(f.fileno(), 'test')
  2313. self.assertEqual(posixpath.basename(zi.filename), 'test')
  2314. self.assertFalse(zi.is_dir())
  2315. self.assertEqual(zi.file_size, os.path.getsize(__file__))
  2316. def test_from_dir(self):
  2317. dirpath = os.path.dirname(os.path.abspath(__file__))
  2318. zi = zipfile.ZipInfo.from_file(dirpath, 'stdlib_tests')
  2319. self.assertEqual(zi.filename, 'stdlib_tests/')
  2320. self.assertTrue(zi.is_dir())
  2321. self.assertEqual(zi.compress_type, zipfile.ZIP_STORED)
  2322. self.assertEqual(zi.file_size, 0)
  2323. class CommandLineTest(unittest.TestCase):
  2324. def zipfilecmd(self, *args, **kwargs):
  2325. rc, out, err = script_helper.assert_python_ok('-m', 'zipfile', *args,
  2326. **kwargs)
  2327. return out.replace(os.linesep.encode(), b'\n')
  2328. def zipfilecmd_failure(self, *args):
  2329. return script_helper.assert_python_failure('-m', 'zipfile', *args)
  2330. def test_bad_use(self):
  2331. rc, out, err = self.zipfilecmd_failure()
  2332. self.assertEqual(out, b'')
  2333. self.assertIn(b'usage', err.lower())
  2334. self.assertIn(b'error', err.lower())
  2335. self.assertIn(b'required', err.lower())
  2336. rc, out, err = self.zipfilecmd_failure('-l', '')
  2337. self.assertEqual(out, b'')
  2338. self.assertNotEqual(err.strip(), b'')
  2339. def test_test_command(self):
  2340. zip_name = findfile('zipdir.zip')
  2341. for opt in '-t', '--test':
  2342. out = self.zipfilecmd(opt, zip_name)
  2343. self.assertEqual(out.rstrip(), b'Done testing')
  2344. zip_name = findfile('testtar.tar')
  2345. rc, out, err = self.zipfilecmd_failure('-t', zip_name)
  2346. self.assertEqual(out, b'')
  2347. def test_list_command(self):
  2348. zip_name = findfile('zipdir.zip')
  2349. t = io.StringIO()
  2350. with zipfile.ZipFile(zip_name, 'r') as tf:
  2351. tf.printdir(t)
  2352. expected = t.getvalue().encode('ascii', 'backslashreplace')
  2353. for opt in '-l', '--list':
  2354. out = self.zipfilecmd(opt, zip_name,
  2355. PYTHONIOENCODING='ascii:backslashreplace')
  2356. self.assertEqual(out, expected)
  2357. @requires_zlib()
  2358. def test_create_command(self):
  2359. self.addCleanup(unlink, TESTFN)
  2360. with open(TESTFN, 'w', encoding='utf-8') as f:
  2361. f.write('test 1')
  2362. os.mkdir(TESTFNDIR)
  2363. self.addCleanup(rmtree, TESTFNDIR)
  2364. with open(os.path.join(TESTFNDIR, 'file.txt'), 'w', encoding='utf-8') as f:
  2365. f.write('test 2')
  2366. files = [TESTFN, TESTFNDIR]
  2367. namelist = [TESTFN, TESTFNDIR + '/', TESTFNDIR + '/file.txt']
  2368. for opt in '-c', '--create':
  2369. try:
  2370. out = self.zipfilecmd(opt, TESTFN2, *files)
  2371. self.assertEqual(out, b'')
  2372. with zipfile.ZipFile(TESTFN2) as zf:
  2373. self.assertEqual(zf.namelist(), namelist)
  2374. self.assertEqual(zf.read(namelist[0]), b'test 1')
  2375. self.assertEqual(zf.read(namelist[2]), b'test 2')
  2376. finally:
  2377. unlink(TESTFN2)
  2378. def test_extract_command(self):
  2379. zip_name = findfile('zipdir.zip')
  2380. for opt in '-e', '--extract':
  2381. with temp_dir() as extdir:
  2382. out = self.zipfilecmd(opt, zip_name, extdir)
  2383. self.assertEqual(out, b'')
  2384. with zipfile.ZipFile(zip_name) as zf:
  2385. for zi in zf.infolist():
  2386. path = os.path.join(extdir,
  2387. zi.filename.replace('/', os.sep))
  2388. if zi.is_dir():
  2389. self.assertTrue(os.path.isdir(path))
  2390. else:
  2391. self.assertTrue(os.path.isfile(path))
  2392. with open(path, 'rb') as f:
  2393. self.assertEqual(f.read(), zf.read(zi))
  2394. class TestExecutablePrependedZip(unittest.TestCase):
  2395. """Test our ability to open zip files with an executable prepended."""
  2396. def setUp(self):
  2397. self.exe_zip = findfile('exe_with_zip', subdir='ziptestdata')
  2398. self.exe_zip64 = findfile('exe_with_z64', subdir='ziptestdata')
  2399. def _test_zip_works(self, name):
  2400. # bpo28494 sanity check: ensure is_zipfile works on these.
  2401. self.assertTrue(zipfile.is_zipfile(name),
  2402. f'is_zipfile failed on {name}')
  2403. # Ensure we can operate on these via ZipFile.
  2404. with zipfile.ZipFile(name) as zipfp:
  2405. for n in zipfp.namelist():
  2406. data = zipfp.read(n)
  2407. self.assertIn(b'FAVORITE_NUMBER', data)
  2408. def test_read_zip_with_exe_prepended(self):
  2409. self._test_zip_works(self.exe_zip)
  2410. def test_read_zip64_with_exe_prepended(self):
  2411. self._test_zip_works(self.exe_zip64)
  2412. @unittest.skipUnless(sys.executable, 'sys.executable required.')
  2413. @unittest.skipUnless(os.access('/bin/bash', os.X_OK),
  2414. 'Test relies on #!/bin/bash working.')
  2415. @requires_subprocess()
  2416. def test_execute_zip2(self):
  2417. output = subprocess.check_output([self.exe_zip, sys.executable])
  2418. self.assertIn(b'number in executable: 5', output)
  2419. @unittest.skipUnless(sys.executable, 'sys.executable required.')
  2420. @unittest.skipUnless(os.access('/bin/bash', os.X_OK),
  2421. 'Test relies on #!/bin/bash working.')
  2422. @requires_subprocess()
  2423. def test_execute_zip64(self):
  2424. output = subprocess.check_output([self.exe_zip64, sys.executable])
  2425. self.assertIn(b'number in executable: 5', output)
  2426. # Poor man's technique to consume a (smallish) iterable.
  2427. consume = tuple
  2428. # from jaraco.itertools 5.0
  2429. class jaraco:
  2430. class itertools:
  2431. class Counter:
  2432. def __init__(self, i):
  2433. self.count = 0
  2434. self._orig_iter = iter(i)
  2435. def __iter__(self):
  2436. return self
  2437. def __next__(self):
  2438. result = next(self._orig_iter)
  2439. self.count += 1
  2440. return result
  2441. def add_dirs(zf):
  2442. """
  2443. Given a writable zip file zf, inject directory entries for
  2444. any directories implied by the presence of children.
  2445. """
  2446. for name in zipfile.CompleteDirs._implied_dirs(zf.namelist()):
  2447. zf.writestr(name, b"")
  2448. return zf
  2449. def build_alpharep_fixture():
  2450. """
  2451. Create a zip file with this structure:
  2452. .
  2453. ├── a.txt
  2454. ├── b
  2455. │ ├── c.txt
  2456. │ ├── d
  2457. │ │ └── e.txt
  2458. │ └── f.txt
  2459. └── g
  2460. └── h
  2461. └── i.txt
  2462. This fixture has the following key characteristics:
  2463. - a file at the root (a)
  2464. - a file two levels deep (b/d/e)
  2465. - multiple files in a directory (b/c, b/f)
  2466. - a directory containing only a directory (g/h)
  2467. "alpha" because it uses alphabet
  2468. "rep" because it's a representative example
  2469. """
  2470. data = io.BytesIO()
  2471. zf = zipfile.ZipFile(data, "w")
  2472. zf.writestr("a.txt", b"content of a")
  2473. zf.writestr("b/c.txt", b"content of c")
  2474. zf.writestr("b/d/e.txt", b"content of e")
  2475. zf.writestr("b/f.txt", b"content of f")
  2476. zf.writestr("g/h/i.txt", b"content of i")
  2477. zf.filename = "alpharep.zip"
  2478. return zf
  2479. def pass_alpharep(meth):
  2480. """
  2481. Given a method, wrap it in a for loop that invokes method
  2482. with each subtest.
  2483. """
  2484. @functools.wraps(meth)
  2485. def wrapper(self):
  2486. for alpharep in self.zipfile_alpharep():
  2487. meth(self, alpharep=alpharep)
  2488. return wrapper
  2489. class TestPath(unittest.TestCase):
  2490. def setUp(self):
  2491. self.fixtures = contextlib.ExitStack()
  2492. self.addCleanup(self.fixtures.close)
  2493. def zipfile_alpharep(self):
  2494. with self.subTest():
  2495. yield build_alpharep_fixture()
  2496. with self.subTest():
  2497. yield add_dirs(build_alpharep_fixture())
  2498. def zipfile_ondisk(self, alpharep):
  2499. tmpdir = pathlib.Path(self.fixtures.enter_context(temp_dir()))
  2500. buffer = alpharep.fp
  2501. alpharep.close()
  2502. path = tmpdir / alpharep.filename
  2503. with path.open("wb") as strm:
  2504. strm.write(buffer.getvalue())
  2505. return path
  2506. @pass_alpharep
  2507. def test_iterdir_and_types(self, alpharep):
  2508. root = zipfile.Path(alpharep)
  2509. assert root.is_dir()
  2510. a, b, g = root.iterdir()
  2511. assert a.is_file()
  2512. assert b.is_dir()
  2513. assert g.is_dir()
  2514. c, f, d = b.iterdir()
  2515. assert c.is_file() and f.is_file()
  2516. (e,) = d.iterdir()
  2517. assert e.is_file()
  2518. (h,) = g.iterdir()
  2519. (i,) = h.iterdir()
  2520. assert i.is_file()
  2521. @pass_alpharep
  2522. def test_is_file_missing(self, alpharep):
  2523. root = zipfile.Path(alpharep)
  2524. assert not root.joinpath('missing.txt').is_file()
  2525. @pass_alpharep
  2526. def test_iterdir_on_file(self, alpharep):
  2527. root = zipfile.Path(alpharep)
  2528. a, b, g = root.iterdir()
  2529. with self.assertRaises(ValueError):
  2530. a.iterdir()
  2531. @pass_alpharep
  2532. def test_subdir_is_dir(self, alpharep):
  2533. root = zipfile.Path(alpharep)
  2534. assert (root / 'b').is_dir()
  2535. assert (root / 'b/').is_dir()
  2536. assert (root / 'g').is_dir()
  2537. assert (root / 'g/').is_dir()
  2538. @pass_alpharep
  2539. def test_open(self, alpharep):
  2540. root = zipfile.Path(alpharep)
  2541. a, b, g = root.iterdir()
  2542. with a.open(encoding="utf-8") as strm:
  2543. data = strm.read()
  2544. self.assertEqual(data, "content of a")
  2545. with a.open('r', "utf-8") as strm: # not a kw, no gh-101144 TypeError
  2546. data = strm.read()
  2547. self.assertEqual(data, "content of a")
  2548. def test_open_encoding_utf16(self):
  2549. in_memory_file = io.BytesIO()
  2550. zf = zipfile.ZipFile(in_memory_file, "w")
  2551. zf.writestr("path/16.txt", "This was utf-16".encode("utf-16"))
  2552. zf.filename = "test_open_utf16.zip"
  2553. root = zipfile.Path(zf)
  2554. (path,) = root.iterdir()
  2555. u16 = path.joinpath("16.txt")
  2556. with u16.open('r', "utf-16") as strm:
  2557. data = strm.read()
  2558. self.assertEqual(data, "This was utf-16")
  2559. with u16.open(encoding="utf-16") as strm:
  2560. data = strm.read()
  2561. self.assertEqual(data, "This was utf-16")
  2562. def test_open_encoding_errors(self):
  2563. in_memory_file = io.BytesIO()
  2564. zf = zipfile.ZipFile(in_memory_file, "w")
  2565. zf.writestr("path/bad-utf8.bin", b"invalid utf-8: \xff\xff.")
  2566. zf.filename = "test_read_text_encoding_errors.zip"
  2567. root = zipfile.Path(zf)
  2568. (path,) = root.iterdir()
  2569. u16 = path.joinpath("bad-utf8.bin")
  2570. # encoding= as a positional argument for gh-101144.
  2571. data = u16.read_text("utf-8", errors="ignore")
  2572. self.assertEqual(data, "invalid utf-8: .")
  2573. with u16.open("r", "utf-8", errors="surrogateescape") as f:
  2574. self.assertEqual(f.read(), "invalid utf-8: \udcff\udcff.")
  2575. # encoding= both positional and keyword is an error; gh-101144.
  2576. with self.assertRaisesRegex(TypeError, "encoding"):
  2577. data = u16.read_text("utf-8", encoding="utf-8")
  2578. # both keyword arguments work.
  2579. with u16.open("r", encoding="utf-8", errors="strict") as f:
  2580. # error during decoding with wrong codec.
  2581. with self.assertRaises(UnicodeDecodeError):
  2582. f.read()
  2583. def test_encoding_warnings(self):
  2584. """EncodingWarning must blame the read_text and open calls."""
  2585. code = '''\
  2586. import io, zipfile
  2587. with zipfile.ZipFile(io.BytesIO(), "w") as zf:
  2588. zf.filename = '<test_encoding_warnings in memory zip file>'
  2589. zf.writestr("path/file.txt", b"Spanish Inquisition")
  2590. root = zipfile.Path(zf)
  2591. (path,) = root.iterdir()
  2592. file_path = path.joinpath("file.txt")
  2593. unused = file_path.read_text() # should warn
  2594. file_path.open("r").close() # should warn
  2595. '''
  2596. proc = assert_python_ok('-X', 'warn_default_encoding', '-c', code)
  2597. warnings = proc.err.splitlines()
  2598. self.assertEqual(len(warnings), 2, proc.err)
  2599. self.assertRegex(warnings[0], rb"^<string>:8: EncodingWarning:")
  2600. self.assertRegex(warnings[1], rb"^<string>:9: EncodingWarning:")
  2601. def test_open_write(self):
  2602. """
  2603. If the zipfile is open for write, it should be possible to
  2604. write bytes or text to it.
  2605. """
  2606. zf = zipfile.Path(zipfile.ZipFile(io.BytesIO(), mode='w'))
  2607. with zf.joinpath('file.bin').open('wb') as strm:
  2608. strm.write(b'binary contents')
  2609. with zf.joinpath('file.txt').open('w', encoding="utf-8") as strm:
  2610. strm.write('text file')
  2611. def test_open_extant_directory(self):
  2612. """
  2613. Attempting to open a directory raises IsADirectoryError.
  2614. """
  2615. zf = zipfile.Path(add_dirs(build_alpharep_fixture()))
  2616. with self.assertRaises(IsADirectoryError):
  2617. zf.joinpath('b').open()
  2618. @pass_alpharep
  2619. def test_open_binary_invalid_args(self, alpharep):
  2620. root = zipfile.Path(alpharep)
  2621. with self.assertRaises(ValueError):
  2622. root.joinpath('a.txt').open('rb', encoding='utf-8')
  2623. with self.assertRaises(ValueError):
  2624. root.joinpath('a.txt').open('rb', 'utf-8')
  2625. def test_open_missing_directory(self):
  2626. """
  2627. Attempting to open a missing directory raises FileNotFoundError.
  2628. """
  2629. zf = zipfile.Path(add_dirs(build_alpharep_fixture()))
  2630. with self.assertRaises(FileNotFoundError):
  2631. zf.joinpath('z').open()
  2632. @pass_alpharep
  2633. def test_read(self, alpharep):
  2634. root = zipfile.Path(alpharep)
  2635. a, b, g = root.iterdir()
  2636. assert a.read_text(encoding="utf-8") == "content of a"
  2637. a.read_text("utf-8") # No positional arg TypeError per gh-101144.
  2638. assert a.read_bytes() == b"content of a"
  2639. @pass_alpharep
  2640. def test_joinpath(self, alpharep):
  2641. root = zipfile.Path(alpharep)
  2642. a = root.joinpath("a.txt")
  2643. assert a.is_file()
  2644. e = root.joinpath("b").joinpath("d").joinpath("e.txt")
  2645. assert e.read_text(encoding="utf-8") == "content of e"
  2646. @pass_alpharep
  2647. def test_joinpath_multiple(self, alpharep):
  2648. root = zipfile.Path(alpharep)
  2649. e = root.joinpath("b", "d", "e.txt")
  2650. assert e.read_text(encoding="utf-8") == "content of e"
  2651. @pass_alpharep
  2652. def test_traverse_truediv(self, alpharep):
  2653. root = zipfile.Path(alpharep)
  2654. a = root / "a.txt"
  2655. assert a.is_file()
  2656. e = root / "b" / "d" / "e.txt"
  2657. assert e.read_text(encoding="utf-8") == "content of e"
  2658. @pass_alpharep
  2659. def test_traverse_simplediv(self, alpharep):
  2660. """
  2661. Disable the __future__.division when testing traversal.
  2662. """
  2663. code = compile(
  2664. source="zipfile.Path(alpharep) / 'a'",
  2665. filename="(test)",
  2666. mode="eval",
  2667. dont_inherit=True,
  2668. )
  2669. eval(code)
  2670. @pass_alpharep
  2671. def test_pathlike_construction(self, alpharep):
  2672. """
  2673. zipfile.Path should be constructable from a path-like object
  2674. """
  2675. zipfile_ondisk = self.zipfile_ondisk(alpharep)
  2676. pathlike = pathlib.Path(str(zipfile_ondisk))
  2677. zipfile.Path(pathlike)
  2678. @pass_alpharep
  2679. def test_traverse_pathlike(self, alpharep):
  2680. root = zipfile.Path(alpharep)
  2681. root / pathlib.Path("a")
  2682. @pass_alpharep
  2683. def test_parent(self, alpharep):
  2684. root = zipfile.Path(alpharep)
  2685. assert (root / 'a').parent.at == ''
  2686. assert (root / 'a' / 'b').parent.at == 'a/'
  2687. @pass_alpharep
  2688. def test_dir_parent(self, alpharep):
  2689. root = zipfile.Path(alpharep)
  2690. assert (root / 'b').parent.at == ''
  2691. assert (root / 'b/').parent.at == ''
  2692. @pass_alpharep
  2693. def test_missing_dir_parent(self, alpharep):
  2694. root = zipfile.Path(alpharep)
  2695. assert (root / 'missing dir/').parent.at == ''
  2696. @pass_alpharep
  2697. def test_mutability(self, alpharep):
  2698. """
  2699. If the underlying zipfile is changed, the Path object should
  2700. reflect that change.
  2701. """
  2702. root = zipfile.Path(alpharep)
  2703. a, b, g = root.iterdir()
  2704. alpharep.writestr('foo.txt', 'foo')
  2705. alpharep.writestr('bar/baz.txt', 'baz')
  2706. assert any(child.name == 'foo.txt' for child in root.iterdir())
  2707. assert (root / 'foo.txt').read_text(encoding="utf-8") == 'foo'
  2708. (baz,) = (root / 'bar').iterdir()
  2709. assert baz.read_text(encoding="utf-8") == 'baz'
  2710. HUGE_ZIPFILE_NUM_ENTRIES = 2 ** 13
  2711. def huge_zipfile(self):
  2712. """Create a read-only zipfile with a huge number of entries entries."""
  2713. strm = io.BytesIO()
  2714. zf = zipfile.ZipFile(strm, "w")
  2715. for entry in map(str, range(self.HUGE_ZIPFILE_NUM_ENTRIES)):
  2716. zf.writestr(entry, entry)
  2717. zf.mode = 'r'
  2718. return zf
  2719. def test_joinpath_constant_time(self):
  2720. """
  2721. Ensure joinpath on items in zipfile is linear time.
  2722. """
  2723. root = zipfile.Path(self.huge_zipfile())
  2724. entries = jaraco.itertools.Counter(root.iterdir())
  2725. for entry in entries:
  2726. entry.joinpath('suffix')
  2727. # Check the file iterated all items
  2728. assert entries.count == self.HUGE_ZIPFILE_NUM_ENTRIES
  2729. # @func_timeout.func_set_timeout(3)
  2730. def test_implied_dirs_performance(self):
  2731. data = ['/'.join(string.ascii_lowercase + str(n)) for n in range(10000)]
  2732. zipfile.CompleteDirs._implied_dirs(data)
  2733. @pass_alpharep
  2734. def test_read_does_not_close(self, alpharep):
  2735. alpharep = self.zipfile_ondisk(alpharep)
  2736. with zipfile.ZipFile(alpharep) as file:
  2737. for rep in range(2):
  2738. zipfile.Path(file, 'a.txt').read_text(encoding="utf-8")
  2739. @pass_alpharep
  2740. def test_subclass(self, alpharep):
  2741. class Subclass(zipfile.Path):
  2742. pass
  2743. root = Subclass(alpharep)
  2744. assert isinstance(root / 'b', Subclass)
  2745. @pass_alpharep
  2746. def test_filename(self, alpharep):
  2747. root = zipfile.Path(alpharep)
  2748. assert root.filename == pathlib.Path('alpharep.zip')
  2749. @pass_alpharep
  2750. def test_root_name(self, alpharep):
  2751. """
  2752. The name of the root should be the name of the zipfile
  2753. """
  2754. root = zipfile.Path(alpharep)
  2755. assert root.name == 'alpharep.zip' == root.filename.name
  2756. @pass_alpharep
  2757. def test_suffix(self, alpharep):
  2758. """
  2759. The suffix of the root should be the suffix of the zipfile.
  2760. The suffix of each nested file is the final component's last suffix, if any.
  2761. Includes the leading period, just like pathlib.Path.
  2762. """
  2763. root = zipfile.Path(alpharep)
  2764. assert root.suffix == '.zip' == root.filename.suffix
  2765. b = root / "b.txt"
  2766. assert b.suffix == ".txt"
  2767. c = root / "c" / "filename.tar.gz"
  2768. assert c.suffix == ".gz"
  2769. d = root / "d"
  2770. assert d.suffix == ""
  2771. @pass_alpharep
  2772. def test_suffixes(self, alpharep):
  2773. """
  2774. The suffix of the root should be the suffix of the zipfile.
  2775. The suffix of each nested file is the final component's last suffix, if any.
  2776. Includes the leading period, just like pathlib.Path.
  2777. """
  2778. root = zipfile.Path(alpharep)
  2779. assert root.suffixes == ['.zip'] == root.filename.suffixes
  2780. b = root / 'b.txt'
  2781. assert b.suffixes == ['.txt']
  2782. c = root / 'c' / 'filename.tar.gz'
  2783. assert c.suffixes == ['.tar', '.gz']
  2784. d = root / 'd'
  2785. assert d.suffixes == []
  2786. e = root / '.hgrc'
  2787. assert e.suffixes == []
  2788. @pass_alpharep
  2789. def test_stem(self, alpharep):
  2790. """
  2791. The final path component, without its suffix
  2792. """
  2793. root = zipfile.Path(alpharep)
  2794. assert root.stem == 'alpharep' == root.filename.stem
  2795. b = root / "b.txt"
  2796. assert b.stem == "b"
  2797. c = root / "c" / "filename.tar.gz"
  2798. assert c.stem == "filename.tar"
  2799. d = root / "d"
  2800. assert d.stem == "d"
  2801. @pass_alpharep
  2802. def test_root_parent(self, alpharep):
  2803. root = zipfile.Path(alpharep)
  2804. assert root.parent == pathlib.Path('.')
  2805. root.root.filename = 'foo/bar.zip'
  2806. assert root.parent == pathlib.Path('foo')
  2807. @pass_alpharep
  2808. def test_root_unnamed(self, alpharep):
  2809. """
  2810. It is an error to attempt to get the name
  2811. or parent of an unnamed zipfile.
  2812. """
  2813. alpharep.filename = None
  2814. root = zipfile.Path(alpharep)
  2815. with self.assertRaises(TypeError):
  2816. root.name
  2817. with self.assertRaises(TypeError):
  2818. root.parent
  2819. # .name and .parent should still work on subs
  2820. sub = root / "b"
  2821. assert sub.name == "b"
  2822. assert sub.parent
  2823. @pass_alpharep
  2824. def test_inheritance(self, alpharep):
  2825. cls = type('PathChild', (zipfile.Path,), {})
  2826. for alpharep in self.zipfile_alpharep():
  2827. file = cls(alpharep).joinpath('some dir').parent
  2828. assert isinstance(file, cls)
  2829. class EncodedMetadataTests(unittest.TestCase):
  2830. file_names = ['\u4e00', '\u4e8c', '\u4e09'] # Han 'one', 'two', 'three'
  2831. file_content = [
  2832. "This is pure ASCII.\n".encode('ascii'),
  2833. # This is modern Japanese. (UTF-8)
  2834. "\u3053\u308c\u306f\u73fe\u4ee3\u7684\u65e5\u672c\u8a9e\u3067\u3059\u3002\n".encode('utf-8'),
  2835. # This is obsolete Japanese. (Shift JIS)
  2836. "\u3053\u308c\u306f\u53e4\u3044\u65e5\u672c\u8a9e\u3067\u3059\u3002\n".encode('shift_jis'),
  2837. ]
  2838. def setUp(self):
  2839. self.addCleanup(unlink, TESTFN)
  2840. # Create .zip of 3 members with Han names encoded in Shift JIS.
  2841. # Each name is 1 Han character encoding to 2 bytes in Shift JIS.
  2842. # The ASCII names are arbitrary as long as they are length 2 and
  2843. # not otherwise contained in the zip file.
  2844. # Data elements are encoded bytes (ascii, utf-8, shift_jis).
  2845. placeholders = ["n1", "n2"] + self.file_names[2:]
  2846. with zipfile.ZipFile(TESTFN, mode="w") as tf:
  2847. for temp, content in zip(placeholders, self.file_content):
  2848. tf.writestr(temp, content, zipfile.ZIP_STORED)
  2849. # Hack in the Shift JIS names with flag bit 11 (UTF-8) unset.
  2850. with open(TESTFN, "rb") as tf:
  2851. data = tf.read()
  2852. for name, temp in zip(self.file_names, placeholders[:2]):
  2853. data = data.replace(temp.encode('ascii'),
  2854. name.encode('shift_jis'))
  2855. with open(TESTFN, "wb") as tf:
  2856. tf.write(data)
  2857. def _test_read(self, zipfp, expected_names, expected_content):
  2858. # Check the namelist
  2859. names = zipfp.namelist()
  2860. self.assertEqual(sorted(names), sorted(expected_names))
  2861. # Check infolist
  2862. infos = zipfp.infolist()
  2863. names = [zi.filename for zi in infos]
  2864. self.assertEqual(sorted(names), sorted(expected_names))
  2865. # check getinfo
  2866. for name, content in zip(expected_names, expected_content):
  2867. info = zipfp.getinfo(name)
  2868. self.assertEqual(info.filename, name)
  2869. self.assertEqual(info.file_size, len(content))
  2870. self.assertEqual(zipfp.read(name), content)
  2871. def test_read_with_metadata_encoding(self):
  2872. # Read the ZIP archive with correct metadata_encoding
  2873. with zipfile.ZipFile(TESTFN, "r", metadata_encoding='shift_jis') as zipfp:
  2874. self._test_read(zipfp, self.file_names, self.file_content)
  2875. def test_read_without_metadata_encoding(self):
  2876. # Read the ZIP archive without metadata_encoding
  2877. expected_names = [name.encode('shift_jis').decode('cp437')
  2878. for name in self.file_names[:2]] + self.file_names[2:]
  2879. with zipfile.ZipFile(TESTFN, "r") as zipfp:
  2880. self._test_read(zipfp, expected_names, self.file_content)
  2881. def test_read_with_incorrect_metadata_encoding(self):
  2882. # Read the ZIP archive with incorrect metadata_encoding
  2883. expected_names = [name.encode('shift_jis').decode('koi8-u')
  2884. for name in self.file_names[:2]] + self.file_names[2:]
  2885. with zipfile.ZipFile(TESTFN, "r", metadata_encoding='koi8-u') as zipfp:
  2886. self._test_read(zipfp, expected_names, self.file_content)
  2887. def test_read_with_unsuitable_metadata_encoding(self):
  2888. # Read the ZIP archive with metadata_encoding unsuitable for
  2889. # decoding metadata
  2890. with self.assertRaises(UnicodeDecodeError):
  2891. zipfile.ZipFile(TESTFN, "r", metadata_encoding='ascii')
  2892. with self.assertRaises(UnicodeDecodeError):
  2893. zipfile.ZipFile(TESTFN, "r", metadata_encoding='utf-8')
  2894. def test_read_after_append(self):
  2895. newname = '\u56db' # Han 'four'
  2896. expected_names = [name.encode('shift_jis').decode('cp437')
  2897. for name in self.file_names[:2]] + self.file_names[2:]
  2898. expected_names.append(newname)
  2899. expected_content = (*self.file_content, b"newcontent")
  2900. with zipfile.ZipFile(TESTFN, "a") as zipfp:
  2901. zipfp.writestr(newname, "newcontent")
  2902. self.assertEqual(sorted(zipfp.namelist()), sorted(expected_names))
  2903. with zipfile.ZipFile(TESTFN, "r") as zipfp:
  2904. self._test_read(zipfp, expected_names, expected_content)
  2905. with zipfile.ZipFile(TESTFN, "r", metadata_encoding='shift_jis') as zipfp:
  2906. self.assertEqual(sorted(zipfp.namelist()), sorted(expected_names))
  2907. for i, (name, content) in enumerate(zip(expected_names, expected_content)):
  2908. info = zipfp.getinfo(name)
  2909. self.assertEqual(info.filename, name)
  2910. self.assertEqual(info.file_size, len(content))
  2911. if i < 2:
  2912. with self.assertRaises(zipfile.BadZipFile):
  2913. zipfp.read(name)
  2914. else:
  2915. self.assertEqual(zipfp.read(name), content)
  2916. def test_write_with_metadata_encoding(self):
  2917. ZF = zipfile.ZipFile
  2918. for mode in ("w", "x", "a"):
  2919. with self.assertRaisesRegex(ValueError,
  2920. "^metadata_encoding is only"):
  2921. ZF("nonesuch.zip", mode, metadata_encoding="shift_jis")
  2922. def test_cli_with_metadata_encoding(self):
  2923. errmsg = "Non-conforming encodings not supported with -c."
  2924. args = ["--metadata-encoding=shift_jis", "-c", "nonesuch", "nonesuch"]
  2925. with captured_stdout() as stdout:
  2926. with captured_stderr() as stderr:
  2927. self.assertRaises(SystemExit, zipfile.main, args)
  2928. self.assertEqual(stdout.getvalue(), "")
  2929. self.assertIn(errmsg, stderr.getvalue())
  2930. with captured_stdout() as stdout:
  2931. zipfile.main(["--metadata-encoding=shift_jis", "-t", TESTFN])
  2932. listing = stdout.getvalue()
  2933. with captured_stdout() as stdout:
  2934. zipfile.main(["--metadata-encoding=shift_jis", "-l", TESTFN])
  2935. listing = stdout.getvalue()
  2936. for name in self.file_names:
  2937. self.assertIn(name, listing)
  2938. def test_cli_with_metadata_encoding_extract(self):
  2939. os.mkdir(TESTFN2)
  2940. self.addCleanup(rmtree, TESTFN2)
  2941. # Depending on locale, extracted file names can be not encodable
  2942. # with the filesystem encoding.
  2943. for fn in self.file_names:
  2944. try:
  2945. os.stat(os.path.join(TESTFN2, fn))
  2946. except OSError:
  2947. pass
  2948. except UnicodeEncodeError:
  2949. self.skipTest(f'cannot encode file name {fn!r}')
  2950. zipfile.main(["--metadata-encoding=shift_jis", "-e", TESTFN, TESTFN2])
  2951. listing = os.listdir(TESTFN2)
  2952. for name in self.file_names:
  2953. self.assertIn(name, listing)
  2954. if __name__ == "__main__":
  2955. unittest.main()