Skip to main content

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 ZipInfo instance.

    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; the ZIP file size remains unchanged. Call ZipFile.repack afterwards to reclaim space.

    The archive must be opened with mode 'w', 'x' or 'a'.

    Returns the removed ZipInfo instance.

    Calling remove on a closed ZipFile will raise a ValueError.

  • 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 ZipInfo objects 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:

    1. 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.
    2. 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=True can 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 repack on a closed ZipFile will raise a ValueError.

  • 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 ZipInfo instance.

    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 ZipInfo instance.

    Calling copy on a closed ZipFile will raise a ValueError.

Examples

Remove files and reclaim space

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

Remove files under a directory and reclaim space

import os
import zipremove as zipfile

with zipfile.ZipFile('archive.zip', 'w') as zh:
    zh.writestr('file0', 'content0')
    zh.writestr('folder/file1', 'content1')
    zh.writestr('folder/file2', 'content2')
    zh.writestr('folder/file3', 'content3')

print(os.path.getsize('archive.zip'))  # 440

with zipfile.ZipFile('archive.zip', 'a') as zh:
    zinfos = [zh.remove(n) for n in zh.namelist() if n.startswith('folder/')]
    zh.repack(zinfos)

print(os.path.getsize('archive.zip'))  # 116

Rename files under a directory and reclaim space

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zipremove-0.1.0.tar.gz (17.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zipremove-0.1.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file zipremove-0.1.0.tar.gz.

File metadata

  • Download URL: zipremove-0.1.0.tar.gz
  • Upload date:
  • Size: 17.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for zipremove-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7a94a70ec2ac29cd06e5f0a0d5a69ef49935bb4889b30beaf470a6948f82e03e
MD5 76a5396dc85b36e8ff592bcd31880aab
BLAKE2b-256 f445e6275f44e9f775ec2a7bb68d104e3c773c6fa059022935886afa86574a59

See more details on using hashes here.

File details

Details for the file zipremove-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: zipremove-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.3

File hashes

Hashes for zipremove-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4bca3bd0321926c223386cbb162927404a1adbb1361b623bcd2cd27558717851
MD5 3805c2c8faf989eacdcc8e09d5ced7b6
BLAKE2b-256 a63f1a363901646bc6a905acaabe81d43d654b2eb7034c7e609adc7c1761bfea

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page