mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Introduce _calc_initial_entry_offset and refactor
This commit is contained in:
parent
a4b410b9f6
commit
a9e85c654c
1 changed files with 11 additions and 10 deletions
|
|
@ -1446,21 +1446,13 @@ def _repack(self, zfile, *, chunk_size=2**20):
|
||||||
# doesn't match the actual entry order
|
# doesn't match the actual entry order
|
||||||
filelist = sorted(zfile.filelist, key=lambda x: x.header_offset)
|
filelist = sorted(zfile.filelist, key=lambda x: x.header_offset)
|
||||||
|
|
||||||
# calculate the starting entry offset (bytes to skip)
|
|
||||||
entry_offset = 0
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
data_offset = filelist[0].header_offset
|
data_offset = filelist[0].header_offset
|
||||||
except IndexError:
|
except IndexError:
|
||||||
data_offset = zfile.start_dir
|
data_offset = zfile.start_dir
|
||||||
|
|
||||||
if data_offset > 0:
|
# calculate the starting entry offset (bytes to skip)
|
||||||
if self.debug > 2:
|
entry_offset = self._calc_initial_entry_offset(fp, data_offset)
|
||||||
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
|
|
||||||
|
|
||||||
# move file entries
|
# move file entries
|
||||||
for i, info in enumerate(filelist):
|
for i, info in enumerate(filelist):
|
||||||
|
|
@ -1502,6 +1494,15 @@ def _repack(self, zfile, *, chunk_size=2**20):
|
||||||
zfile.start_dir -= entry_offset
|
zfile.start_dir -= entry_offset
|
||||||
zfile._didModify = True
|
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):
|
def _iter_scan_signature(self, fp, signature, start_offset, end_offset, chunk_size=4096):
|
||||||
sig_len = len(signature)
|
sig_len = len(signature)
|
||||||
remainder = b''
|
remainder = b''
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue