Skip to content

Commit 1b7d75a

Browse files
committed
Introduce _move_entry_data and rework chunk_size passing
1 parent c3c8345 commit 1b7d75a

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

Lib/zipfile/__init__.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1369,8 +1369,9 @@ def close(self):
13691369

13701370
class _ZipRepacker:
13711371
"""Class for ZipFile repacking."""
1372-
def __init__(self, debug=0):
1372+
def __init__(self, *, chunk_size=2**20, debug=0):
13731373
self.debug = debug # Level of printing: 0 through 3
1374+
self.chunk_size = chunk_size
13741375

13751376
def _debug(self, level, *msg):
13761377
if self.debug >= level:
@@ -1443,7 +1444,7 @@ def repack(self, zfile):
14431444
with zfile._lock:
14441445
self._repack(zfile)
14451446

1446-
def _repack(self, zfile, *, chunk_size=2**20):
1447+
def _repack(self, zfile):
14471448
fp = zfile.fp
14481449

14491450
# get a sorted filelist by header offset, in case the dir order
@@ -1473,14 +1474,7 @@ def _repack(self, zfile, *, chunk_size=2**20):
14731474
if entry_offset > 0:
14741475
old_header_offset = info.header_offset
14751476
info.header_offset -= entry_offset
1476-
read_size = 0
1477-
while read_size < used_entry_size:
1478-
fp.seek(old_header_offset + read_size)
1479-
data = fp.read(min(used_entry_size - read_size, chunk_size))
1480-
fp.seek(info.header_offset + read_size)
1481-
fp.write(data)
1482-
fp.flush()
1483-
read_size += len(data)
1477+
self._move_entry_data(fp, old_header_offset, info.header_offset, used_entry_size)
14841478

14851479
if info._end_offset is not None:
14861480
info._end_offset = info.header_offset + used_entry_size
@@ -1700,6 +1694,16 @@ def _calc_local_file_entry_size(self, fp, zinfo):
17001694
dd_size
17011695
)
17021696

1697+
def _move_entry_data(self, fp, old_offset, new_offset, size):
1698+
read_size = 0
1699+
while read_size < size:
1700+
fp.seek(old_offset + read_size)
1701+
data = fp.read(min(size - read_size, self.chunk_size))
1702+
fp.seek(new_offset + read_size)
1703+
fp.write(data)
1704+
fp.flush()
1705+
read_size += len(data)
1706+
17031707

17041708
class ZipFile:
17051709
""" Class with methods to open, read, write, close, list zip files.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy