@@ -2001,8 +2001,8 @@ def test_repack_prepended_bytes(self):
2001
2001
for ii in ([], [0 ], [0 , 1 ], [1 ], [2 ]):
2002
2002
with self .subTest (remove = ii ):
2003
2003
# calculate the expected results
2004
- test_files = [data for j , data in enumerate (self .test_files ) if j not in ii ]
2005
2004
fz = io .BytesIO ()
2005
+ test_files = [data for j , data in enumerate (self .test_files ) if j not in ii ]
2006
2006
self ._prepare_zip_from_test_files (fz , test_files )
2007
2007
fz .seek (0 )
2008
2008
with open (TESTFN , 'wb' ) as fh :
@@ -2020,9 +2020,64 @@ def test_repack_prepended_bytes(self):
2020
2020
fh .write (b'dummy ' )
2021
2021
fh .write (fz .read ())
2022
2022
with zipfile .ZipFile (TESTFN , 'a' , self .compression ) as zh :
2023
+ self .assertEqual (zh .data_offset , 6 )
2023
2024
for i in ii :
2024
2025
zh .remove (self .test_files [i ][0 ])
2025
2026
zh .repack ()
2027
+ if hasattr (zh , 'data_offset' ):
2028
+ self .assertEqual (zh .data_offset , 6 )
2029
+
2030
+ # check infolist
2031
+ self .assertEqual (
2032
+ [ComparableZipInfo (zi ) for zi in zh .infolist ()],
2033
+ [ComparableZipInfo (zi ) for zi in expected_zinfos ],
2034
+ )
2035
+
2036
+ # check file size
2037
+ self .assertEqual (os .path .getsize (TESTFN ), expected_size )
2038
+
2039
+ # make sure the zip file is still valid
2040
+ with zipfile .ZipFile (TESTFN ) as zh :
2041
+ self .assertIsNone (zh .testzip ())
2042
+
2043
+ def test_repack_prepended_file_entry (self ):
2044
+ for ii in ([0 ], [0 , 1 ], [0 , 1 , 2 ]):
2045
+ with self .subTest (remove = ii ):
2046
+ # calculate the expected results
2047
+ fz = io .BytesIO ()
2048
+ test_files = [data for j , data in enumerate (self .test_files ) if j not in ii ]
2049
+ self ._prepare_zip_from_test_files (fz , test_files )
2050
+ fz .seek (0 )
2051
+ with open (TESTFN , 'wb' ) as fh :
2052
+ fh .write (b'dummy ' )
2053
+ fh .write (fz .read ())
2054
+ with zipfile .ZipFile (TESTFN ) as zh :
2055
+ expected_zinfos = list (zh .infolist ())
2056
+ expected_size = os .path .getsize (TESTFN )
2057
+
2058
+ # do the removal and check the result
2059
+ fz = io .BytesIO ()
2060
+ with zipfile .ZipFile (fz , 'w' ) as zh :
2061
+ for j , (file , data ) in enumerate (self .test_files ):
2062
+ if j in ii :
2063
+ zh .writestr (file , data )
2064
+ fz .seek (0 )
2065
+ prefix = fz .read ()
2066
+
2067
+ fz = io .BytesIO ()
2068
+ test_files = [data for j , data in enumerate (self .test_files ) if j not in ii ]
2069
+ self ._prepare_zip_from_test_files (fz , test_files )
2070
+ fz .seek (0 )
2071
+
2072
+ with open (TESTFN , 'wb' ) as fh :
2073
+ fh .write (b'dummy ' )
2074
+ fh .write (prefix )
2075
+ fh .write (fz .read ())
2076
+
2077
+ with zipfile .ZipFile (TESTFN , 'a' , self .compression ) as zh :
2078
+ self .assertEqual (zh .data_offset , 6 + len (prefix ))
2079
+ zh .repack ()
2080
+ self .assertEqual (zh .data_offset , 6 )
2026
2081
2027
2082
# check infolist
2028
2083
self .assertEqual (
@@ -2212,8 +2267,10 @@ def test_repack_removed_prepended_bytes(self):
2212
2267
fh .write (b'dummy ' )
2213
2268
fh .write (fz .read ())
2214
2269
with zipfile .ZipFile (TESTFN , 'a' , self .compression ) as zh :
2270
+ self .assertEqual (zh .data_offset , 6 )
2215
2271
zinfos = [zh .remove (self .test_files [i ][0 ]) for i in ii ]
2216
2272
zh .repack (zinfos )
2273
+ self .assertEqual (zh .data_offset , 6 )
2217
2274
2218
2275
# check infolist
2219
2276
self .assertEqual (
0 commit comments