mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
Sync with danny0838/zipremove@86a240bf01
This commit is contained in:
parent
72673e0709
commit
e926a954ed
2 changed files with 1 additions and 67 deletions
|
|
@ -2020,64 +2020,9 @@ def test_repack_prepended_bytes(self):
|
||||||
fh.write(b'dummy ')
|
fh.write(b'dummy ')
|
||||||
fh.write(fz.read())
|
fh.write(fz.read())
|
||||||
with zipfile.ZipFile(TESTFN, 'a', self.compression) as zh:
|
with zipfile.ZipFile(TESTFN, 'a', self.compression) as zh:
|
||||||
self.assertEqual(zh.data_offset, 6)
|
|
||||||
for i in ii:
|
for i in ii:
|
||||||
zh.remove(self.test_files[i][0])
|
zh.remove(self.test_files[i][0])
|
||||||
zh.repack()
|
zh.repack()
|
||||||
if hasattr(zh, 'data_offset'):
|
|
||||||
self.assertEqual(zh.data_offset, 6)
|
|
||||||
|
|
||||||
# check infolist
|
|
||||||
self.assertEqual(
|
|
||||||
[ComparableZipInfo(zi) for zi in zh.infolist()],
|
|
||||||
[ComparableZipInfo(zi) for zi in expected_zinfos],
|
|
||||||
)
|
|
||||||
|
|
||||||
# check file size
|
|
||||||
self.assertEqual(os.path.getsize(TESTFN), expected_size)
|
|
||||||
|
|
||||||
# make sure the zip file is still valid
|
|
||||||
with zipfile.ZipFile(TESTFN) as zh:
|
|
||||||
self.assertIsNone(zh.testzip())
|
|
||||||
|
|
||||||
def test_repack_prepended_file_entry(self):
|
|
||||||
for ii in ([0], [0, 1], [0, 1, 2]):
|
|
||||||
with self.subTest(remove=ii):
|
|
||||||
# calculate the expected results
|
|
||||||
fz = io.BytesIO()
|
|
||||||
test_files = [data for j, data in enumerate(self.test_files) if j not in ii]
|
|
||||||
self._prepare_zip_from_test_files(fz, test_files)
|
|
||||||
fz.seek(0)
|
|
||||||
with open(TESTFN, 'wb') as fh:
|
|
||||||
fh.write(b'dummy ')
|
|
||||||
fh.write(fz.read())
|
|
||||||
with zipfile.ZipFile(TESTFN) as zh:
|
|
||||||
expected_zinfos = list(zh.infolist())
|
|
||||||
expected_size = os.path.getsize(TESTFN)
|
|
||||||
|
|
||||||
# do the removal and check the result
|
|
||||||
fz = io.BytesIO()
|
|
||||||
with zipfile.ZipFile(fz, 'w') as zh:
|
|
||||||
for j, (file, data) in enumerate(self.test_files):
|
|
||||||
if j in ii:
|
|
||||||
zh.writestr(file, data)
|
|
||||||
fz.seek(0)
|
|
||||||
prefix = fz.read()
|
|
||||||
|
|
||||||
fz = io.BytesIO()
|
|
||||||
test_files = [data for j, data in enumerate(self.test_files) if j not in ii]
|
|
||||||
self._prepare_zip_from_test_files(fz, test_files)
|
|
||||||
fz.seek(0)
|
|
||||||
|
|
||||||
with open(TESTFN, 'wb') as fh:
|
|
||||||
fh.write(b'dummy ')
|
|
||||||
fh.write(prefix)
|
|
||||||
fh.write(fz.read())
|
|
||||||
|
|
||||||
with zipfile.ZipFile(TESTFN, 'a', self.compression) as zh:
|
|
||||||
self.assertEqual(zh.data_offset, 6 + len(prefix))
|
|
||||||
zh.repack()
|
|
||||||
self.assertEqual(zh.data_offset, 6)
|
|
||||||
|
|
||||||
# check infolist
|
# check infolist
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|
@ -2267,10 +2212,8 @@ def test_repack_removed_prepended_bytes(self):
|
||||||
fh.write(b'dummy ')
|
fh.write(b'dummy ')
|
||||||
fh.write(fz.read())
|
fh.write(fz.read())
|
||||||
with zipfile.ZipFile(TESTFN, 'a', self.compression) as zh:
|
with zipfile.ZipFile(TESTFN, 'a', self.compression) as zh:
|
||||||
self.assertEqual(zh.data_offset, 6)
|
|
||||||
zinfos = [zh.remove(self.test_files[i][0]) for i in ii]
|
zinfos = [zh.remove(self.test_files[i][0]) for i in ii]
|
||||||
zh.repack(zinfos)
|
zh.repack(zinfos)
|
||||||
self.assertEqual(zh.data_offset, 6)
|
|
||||||
|
|
||||||
# check infolist
|
# check infolist
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
|
|
||||||
|
|
@ -1453,8 +1453,7 @@ def repack(self, zfile, removed=None):
|
||||||
|
|
||||||
Side effects:
|
Side effects:
|
||||||
- Modifies the ZIP file in place.
|
- Modifies the ZIP file in place.
|
||||||
- Updates zfile.start_dir and zfile.data_offset to account for
|
- Updates zfile.start_dir to account for removed data.
|
||||||
removed data.
|
|
||||||
- Sets zfile._didModify to True.
|
- Sets zfile._didModify to True.
|
||||||
- Updates header_offset and clears _end_offset of referenced
|
- Updates header_offset and clears _end_offset of referenced
|
||||||
ZipInfo instances.
|
ZipInfo instances.
|
||||||
|
|
@ -1559,14 +1558,6 @@ def repack(self, zfile, removed=None):
|
||||||
zfile.start_dir -= entry_offset
|
zfile.start_dir -= entry_offset
|
||||||
zfile._didModify = True
|
zfile._didModify = True
|
||||||
|
|
||||||
if zfile._data_offset:
|
|
||||||
try:
|
|
||||||
offset = filelist[0].header_offset
|
|
||||||
except IndexError:
|
|
||||||
offset = zfile.start_dir
|
|
||||||
if offset < zfile._data_offset:
|
|
||||||
zfile._data_offset = offset
|
|
||||||
|
|
||||||
for zinfo in filelist:
|
for zinfo in filelist:
|
||||||
zinfo._end_offset = None
|
zinfo._end_offset = None
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue