Extend `zipfile` with `remove`-related functionalities
Project description
This package extends zipfile with remove-related functionalities.
API
-
ZipFile.remove(zinfo_or_arcname)Removes a member from the archive. zinfo_or_arcname may be the full path of the member or a
ZipInfoinstance.If multiple members share the same full path, only one is removed when a path is provided.
This does not physically remove the local file entry from the archive. Call
ZipFile.repackafterwards to reclaim space.The archive must be opened with mode
'w','x'or'a'.Returns the removed
ZipInfoinstance.Calling
removeon a closed ZipFile will raise aValueError. -
ZipFile.repack(removed=None, *, strict_descriptor=False[, chunk_size])Rewrites the archive to remove stale local file entries, shrinking the ZIP file size.
If removed is provided, it must be a sequence of
ZipInfoobjects representing removed entries; only their corresponding local file entries will be removed.If removed is not provided, local file entries no longer referenced in the central directory will be removed. The algorithm assumes that local file entries are stored consecutively:
- Data before the first referenced entry is removed only when it appears to be a sequence of consecutive entries with no extra following bytes; extra preceeding bytes are preserved.
- Data between referenced entries is removed only when it appears to be a sequence of consecutive entries with no extra preceding bytes; extra following bytes are preserved.
strict_descriptor=Truecan be provided to skip the slower scan for an unsigned data descriptor (deprecated in the latest ZIP specification and is only used by legacy tools) when checking for bytes resembling a valid local file entry. This improves performance, but may cause some stale local file entries to be preserved, as any entry using an unsigned descriptor cannot be detected.chunk_size may be specified to control the buffer size when moving entry data (default is 1 MiB).
The archive must be opened with mode
'a'.Calling
repackon a closed ZipFile will raise aValueError. -
ZipFile.copy(zinfo_or_arcname, new_arcname[, chunk_size])Copies a member zinfo_or_arcname to new_arcname in the archive. zinfo_or_arcname may be the full path of the member or a
ZipInfoinstance.chunk_size may be specified to control the buffer size when copying entry data (default is 1 MiB).
The archive must be opened with mode
'w','x'or'a', and the underlying stream must be seekable.Returns the original version of the copied
ZipInfoinstance.Calling
copyon a closed ZipFile will raise aValueError.
Examples
Remove entries and reclaim space
Call repack after removes to reclaim the space of the removed entries:
import os
import zipremove as zipfile
with zipfile.ZipFile('archive.zip', 'w') as zh:
zh.writestr('file1', 'content1')
zh.writestr('file2', 'content2')
zh.writestr('file3', 'content3')
zh.writestr('file4', 'content4')
print(os.path.getsize('archive.zip')) # 398
with zipfile.ZipFile('archive.zip', 'a') as zh:
zh.remove('file1')
zh.remove('file2')
zh.remove('file3')
zh.repack()
print(os.path.getsize('archive.zip')) # 116 (would be 245 without `repack`)
Alternatively, pass the ZipInfo objects of the removed entries, for better performance and error-proofing:
import os
import zipremove as zipfile
with zipfile.ZipFile('archive.zip', 'w') as zh:
zh.writestr('file1', 'content1')
zh.writestr('file2', 'content2')
zh.writestr('file3', 'content3')
zh.writestr('file4', 'content4')
print(os.path.getsize('archive.zip')) # 398
with zipfile.ZipFile('archive.zip', 'a') as zh:
zinfos = []
zinfos.append(zh.remove('file1'))
zinfos.append(zh.remove('file2'))
zinfos.append(zh.remove('file3'))
zh.repack(zinfos)
print(os.path.getsize('archive.zip')) # 116 (would be 245 without `repack`)
Move entries under a folder and reclaim space
Moving entries in a ZIP file must be done as a combination of copy, remove,
and optionally repack, because every local file entry contains the filename
and requires rewriting.
import os
import zipremove as zipfile
with zipfile.ZipFile('archive.zip', 'w') as zh:
zh.writestr('file0', 'content0')
zh.writestr('folder1/file1', 'content1')
zh.writestr('folder1/file2', 'content2')
zh.writestr('folder1/file3', 'content3')
print(os.path.getsize('archive.zip')) # 446
with zipfile.ZipFile('archive.zip', 'a') as zh:
for n in zh.namelist():
if n.startswith('folder1/'):
n2 = 'folder2/' + n[len('folder1/'):]
zh.copy(n, n2)
zh.remove(n)
zh.repack()
print(os.path.getsize('archive.zip')) # 446 (would be 599 without `repack`)
Similarly, pass the ZipInfo objects of the copied/removed entries for better performance and error-proofing:
import os
import zipremove as zipfile
with zipfile.ZipFile('archive.zip', 'w') as zh:
zh.writestr('file0', 'content0')
zh.writestr('folder1/file1', 'content1')
zh.writestr('folder1/file2', 'content2')
zh.writestr('folder1/file3', 'content3')
print(os.path.getsize('archive.zip')) # 446
with zipfile.ZipFile('archive.zip', 'a') as zh:
zinfos = []
for n in zh.namelist():
if n.startswith('folder1/'):
n2 = 'folder2/' + n[len('folder1/'):]
zinfos.append(zh.remove(zh.copy(n, n2)))
zh.repack(zinfos)
print(os.path.getsize('archive.zip')) # 446 (would be 599 without `repack`)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file zipremove-0.3.0.tar.gz.
File metadata
- Download URL: zipremove-0.3.0.tar.gz
- Upload date:
- Size: 19.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23a1be62f635eed412acd25ce43d22d02cb70b3b78034d57f8184b725bc78754
|
|
| MD5 |
36f7a0ca735be80e6d50a831073cb0e6
|
|
| BLAKE2b-256 |
b1fa815cc477786d59fa9a610972084c81c0c3d587e2719568746481a34f9e77
|
File details
Details for the file zipremove-0.3.0-py3-none-any.whl.
File metadata
- Download URL: zipremove-0.3.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
80e9dab9f76000239af174f9500795224784a75140f6c69a8a6012fac7e25a36
|
|
| MD5 |
706ffa3ebcaa358fa11a447ad1bf3d50
|
|
| BLAKE2b-256 |
7f80fcce20f769964987b83208e6a3cabe21bddfaa838c332b4a2eed2d05a201
|