1313from random import randint , random
1414from unittest import skipUnless
1515
16- from test .support import TESTFN , run_unittest , findfile , unlink , requires_zlib
16+ from test .support import TESTFN , run_unittest , findfile , unlink , requires_zlib , requires_bz2
1717
1818TESTFN2 = TESTFN + "2"
1919TESTFNDIR = TESTFN + "d"
@@ -313,6 +313,54 @@ def test_low_compression(self):
313313 self .assertEqual (openobj .read (1 ), b'1' )
314314 self .assertEqual (openobj .read (1 ), b'2' )
315315
316+ @requires_bz2
317+ def test_bzip2 (self ):
318+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
319+ self .zip_test (f , zipfile .ZIP_BZIP2 )
320+
321+ @requires_bz2
322+ def test_open_bzip2 (self ):
323+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
324+ self .zip_open_test (f , zipfile .ZIP_BZIP2 )
325+
326+ @requires_bz2
327+ def test_random_open_bzip2 (self ):
328+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
329+ self .zip_random_open_test (f , zipfile .ZIP_BZIP2 )
330+
331+ @requires_bz2
332+ def test_readline_read_bzip2 (self ):
333+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
334+ self .zip_readline_read_test (f , zipfile .ZIP_BZIP2 )
335+
336+ @requires_bz2
337+ def test_readline_bzip2 (self ):
338+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
339+ self .zip_readline_test (f , zipfile .ZIP_BZIP2 )
340+
341+ @requires_bz2
342+ def test_readlines_bzip2 (self ):
343+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
344+ self .zip_readlines_test (f , zipfile .ZIP_BZIP2 )
345+
346+ @requires_bz2
347+ def test_iterlines_bzip2 (self ):
348+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
349+ self .zip_iterlines_test (f , zipfile .ZIP_BZIP2 )
350+
351+ @requires_bz2
352+ def test_low_compression_bzip2 (self ):
353+ """Check for cases where compressed data is larger than original."""
354+ # Create the ZIP archive
355+ with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_BZIP2 ) as zipfp :
356+ zipfp .writestr ("strfile" , '12' )
357+
358+ # Get an open object for strfile
359+ with zipfile .ZipFile (TESTFN2 , "r" , zipfile .ZIP_BZIP2 ) as zipfp :
360+ with zipfp .open ("strfile" ) as openobj :
361+ self .assertEqual (openobj .read (1 ), b'1' )
362+ self .assertEqual (openobj .read (1 ), b'2' )
363+
316364 def test_absolute_arcnames (self ):
317365 with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_STORED ) as zipfp :
318366 zipfp .write (TESTFN , "/absolute" )
@@ -453,6 +501,13 @@ def test_writestr_compression_deflated(self):
453501 info = zipfp .getinfo ('b.txt' )
454502 self .assertEqual (info .compress_type , zipfile .ZIP_DEFLATED )
455503
504+ @requires_bz2
505+ def test_writestr_compression_bzip2 (self ):
506+ zipfp = zipfile .ZipFile (TESTFN2 , "w" )
507+ zipfp .writestr ("b.txt" , "hello world" , compress_type = zipfile .ZIP_BZIP2 )
508+ info = zipfp .getinfo ('b.txt' )
509+ self .assertEqual (info .compress_type , zipfile .ZIP_BZIP2 )
510+
456511 def zip_test_writestr_permissions (self , f , compression ):
457512 # Make sure that writestr creates files with mode 0600,
458513 # when it is passed a name rather than a ZipInfo instance.
@@ -626,6 +681,11 @@ def test_deflated(self):
626681 for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
627682 self .zip_test (f , zipfile .ZIP_DEFLATED )
628683
684+ @requires_bz2
685+ def test_bzip2 (self ):
686+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
687+ self .zip_test (f , zipfile .ZIP_BZIP2 )
688+
629689 def test_absolute_arcnames (self ):
630690 with zipfile .ZipFile (TESTFN2 , "w" , zipfile .ZIP_STORED ,
631691 allowZip64 = True ) as zipfp :
@@ -754,6 +814,18 @@ class OtherTests(unittest.TestCase):
754814 b'\x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x80 \x01 \x00 \x00 \x00 '
755815 b'\x00 afilePK\x05 \x06 \x00 \x00 \x00 \x00 \x01 \x00 '
756816 b'\x01 \x00 3\x00 \x00 \x00 3\x00 \x00 \x00 \x00 \x00 ' ),
817+ zipfile .ZIP_BZIP2 : (
818+ b'PK\x03 \x04 \x14 \x03 \x00 \x00 \x0c \x00 nu\x0c =FA'
819+ b'KE8\x00 \x00 \x00 n\x00 \x00 \x00 \x05 \x00 \x00 \x00 af'
820+ b'ileBZh91AY&SY\xd4 \xa8 \xca '
821+ b'\x7f \x00 \x00 \x0f \x11 \x80 @\x00 \x06 D\x90 \x80 \x00 \xa5 '
822+ b'P\xd9 !\x03 \x03 \x13 \x13 \x13 \x89 \xa9 \xa9 \xc2 u5:\x9f '
823+ b'\x8b \xb9 "\x9c (HjTe?\x80 PK\x01 \x02 \x14 '
824+ b'\x03 \x14 \x03 \x00 \x00 \x0c \x00 nu\x0c =FAKE8'
825+ b'\x00 \x00 \x00 n\x00 \x00 \x00 \x05 \x00 \x00 \x00 \x00 \x00 \x00 \x00 \x00 '
826+ b'\x00 \x80 \x80 \x81 \x00 \x00 \x00 \x00 afilePK'
827+ b'\x05 \x06 \x00 \x00 \x00 \x00 \x01 \x00 \x01 \x00 3\x00 \x00 \x00 [\x00 '
828+ b'\x00 \x00 \x00 \x00 ' ),
757829 }
758830
759831 def test_unicode_filenames (self ):
@@ -1007,6 +1079,10 @@ def test_testzip_with_bad_crc_stored(self):
10071079 def test_testzip_with_bad_crc_deflated (self ):
10081080 self .check_testzip_with_bad_crc (zipfile .ZIP_DEFLATED )
10091081
1082+ @requires_bz2
1083+ def test_testzip_with_bad_crc_bzip2 (self ):
1084+ self .check_testzip_with_bad_crc (zipfile .ZIP_BZIP2 )
1085+
10101086 def check_read_with_bad_crc (self , compression ):
10111087 """Tests that files with bad CRCs raise a BadZipFile exception when read."""
10121088 zipdata = self .zips_with_bad_crc [compression ]
@@ -1035,6 +1111,10 @@ def test_read_with_bad_crc_stored(self):
10351111 def test_read_with_bad_crc_deflated (self ):
10361112 self .check_read_with_bad_crc (zipfile .ZIP_DEFLATED )
10371113
1114+ @requires_bz2
1115+ def test_read_with_bad_crc_bzip2 (self ):
1116+ self .check_read_with_bad_crc (zipfile .ZIP_BZIP2 )
1117+
10381118 def check_read_return_size (self , compression ):
10391119 # Issue #9837: ZipExtFile.read() shouldn't return more bytes
10401120 # than requested.
@@ -1055,6 +1135,10 @@ def test_read_return_size_stored(self):
10551135 def test_read_return_size_deflated (self ):
10561136 self .check_read_return_size (zipfile .ZIP_DEFLATED )
10571137
1138+ @requires_bz2
1139+ def test_read_return_size_bzip2 (self ):
1140+ self .check_read_return_size (zipfile .ZIP_BZIP2 )
1141+
10581142 def test_empty_zipfile (self ):
10591143 # Check that creating a file in 'w' or 'a' mode and closing without
10601144 # adding any files to the archives creates a valid empty ZIP file
@@ -1196,6 +1280,11 @@ def test_deflated(self):
11961280 for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
11971281 self .zip_test (f , zipfile .ZIP_DEFLATED )
11981282
1283+ @requires_bz2
1284+ def test_bzip2 (self ):
1285+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1286+ self .zip_test (f , zipfile .ZIP_BZIP2 )
1287+
11991288 def zip_open_test (self , f , compression ):
12001289 self .make_test_archive (f , compression )
12011290
@@ -1236,6 +1325,11 @@ def test_open_deflated(self):
12361325 for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
12371326 self .zip_open_test (f , zipfile .ZIP_DEFLATED )
12381327
1328+ @requires_bz2
1329+ def test_open_bzip2 (self ):
1330+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1331+ self .zip_open_test (f , zipfile .ZIP_BZIP2 )
1332+
12391333 def zip_random_open_test (self , f , compression ):
12401334 self .make_test_archive (f , compression )
12411335
@@ -1264,6 +1358,11 @@ def test_random_open_deflated(self):
12641358 for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
12651359 self .zip_random_open_test (f , zipfile .ZIP_DEFLATED )
12661360
1361+ @requires_bz2
1362+ def test_random_open_bzip2 (self ):
1363+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1364+ self .zip_random_open_test (f , zipfile .ZIP_BZIP2 )
1365+
12671366
12681367@requires_zlib
12691368class TestsWithMultipleOpens (unittest .TestCase ):
@@ -1483,6 +1582,31 @@ def test_iterlines_deflated(self):
14831582 for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
14841583 self .iterlines_test (f , zipfile .ZIP_DEFLATED )
14851584
1585+ @requires_bz2
1586+ def test_read_bzip2 (self ):
1587+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1588+ self .read_test (f , zipfile .ZIP_BZIP2 )
1589+
1590+ @requires_bz2
1591+ def test_readline_read_bzip2 (self ):
1592+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1593+ self .readline_read_test (f , zipfile .ZIP_BZIP2 )
1594+
1595+ @requires_bz2
1596+ def test_readline_bzip2 (self ):
1597+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1598+ self .readline_test (f , zipfile .ZIP_BZIP2 )
1599+
1600+ @requires_bz2
1601+ def test_readlines_bzip2 (self ):
1602+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1603+ self .readlines_test (f , zipfile .ZIP_BZIP2 )
1604+
1605+ @requires_bz2
1606+ def test_iterlines_bzip2 (self ):
1607+ for f in (TESTFN2 , TemporaryFile (), io .BytesIO ()):
1608+ self .iterlines_test (f , zipfile .ZIP_BZIP2 )
1609+
14861610 def tearDown (self ):
14871611 for sep , fn in self .arcfiles .items ():
14881612 os .remove (fn )
0 commit comments