Introduce _calc_initial_entry_offset and refactor

This commit is contained in:
Danny Lin 2025-05-25 06:03:08 +08:00
parent a4b410b9f6
commit a9e85c654c

View file

@ -1446,21 +1446,13 @@ def _repack(self, zfile, *, chunk_size=2**20):
# doesn't match the actual entry order
filelist = sorted(zfile.filelist, key=lambda x: x.header_offset)
# calculate the starting entry offset (bytes to skip)
entry_offset = 0
try:
data_offset = filelist[0].header_offset
except IndexError:
data_offset = zfile.start_dir
if data_offset > 0:
if self.debug > 2:
print('scanning file signatures before:', data_offset)
for pos in self._iter_scan_signature(fp, stringFileHeader, 0, data_offset):
if self._starts_consecutive_file_entries(fp, pos, data_offset):
entry_offset = data_offset - pos
break
# calculate the starting entry offset (bytes to skip)
entry_offset = self._calc_initial_entry_offset(fp, data_offset)
# move file entries
for i, info in enumerate(filelist):
@ -1502,6 +1494,15 @@ def _repack(self, zfile, *, chunk_size=2**20):
zfile.start_dir -= entry_offset
zfile._didModify = True
def _calc_initial_entry_offset(self, fp, data_offset):
if data_offset > 0:
if self.debug > 2:
print('scanning file signatures before:', data_offset)
for pos in self._iter_scan_signature(fp, stringFileHeader, 0, data_offset):
if self._starts_consecutive_file_entries(fp, pos, data_offset):
return data_offset - pos
return 0
def _iter_scan_signature(self, fp, signature, start_offset, end_offset, chunk_size=4096):
sig_len = len(signature)
remainder = b''