Menu

Tree [6c55d8] default tip /
 History

Read Only access


File Date Author Commit
 _test 2017-01-01 Anthon van der Neut Anthon van der Neut [886ba6] initial version
 .hgtags 2020-03-30 Anthon van der Neut Anthon van der Neut [6c55d8] Added tag 0.1.1 for changeset a4b4bb72a9a9
 LICENSE 2020-03-30 Anthon van der Neut Anthon van der Neut [a4b4bb] update setup.py for 3.8
 Makefile 2017-01-01 Anthon van der Neut Anthon van der Neut [886ba6] initial version
 README.rst 2017-01-01 Anthon van der Neut Anthon van der Neut [886ba6] initial version
 __init__.py 2020-03-30 Anthon van der Neut Anthon van der Neut [a4b4bb] update setup.py for 3.8
 setup.py 2020-03-30 Anthon van der Neut Anthon van der Neut [a4b4bb] update setup.py for 3.8
 tox.ini 2020-03-30 Anthon van der Neut Anthon van der Neut [a4b4bb] update setup.py for 3.8

Read Me

ruamel.std.zipfile

package ruamel.std.zipfile is a drop-in improvements over the standard zipfile package

You can just replace:

import zipfile

with:

import ruamel.std.zipfile as zipfile

The package includes InMemoryZipFile, which allows easy creation of ZIP files in memory, and allows for streaming or writing out to disc after full creation:

with InMemoryZipFile() as imz:
    imz.append("test.txt", "Another test").append("test2.txt", "Still another")
    with open('some_file.zip', 'wb') as fp:
        fp.write(imz.data)

which will write a two file ZIP file, the first file of which is named test.txt with content Another test. The .data content can also be dynamically returned to a web browser, etc.

File deletion from ZIP

Taking advantage of the delayed writing of InMemoryZipFile, the function delete_from_zip_file(file_name, pattern, file_names), takes a string or pathlib.Path as file_name.

Any files matching the pattern, if provided, are deleted from the file, as well as are any files matching file_names (a list of string/Path, single non-list instances are allowed).

The following deletes any files ending in .pth and the file tmp/README.rst from a ZIP file test.zip:

delete_from_zip_file('test.zip', pattern='.*.pth', file_names=['README.rst'])

or:

delete_from_zip_file('test.zip', pattern='.*.pth', file_names='README.rst')

or:

delete_from_zip_file('test.zip', pattern=re.compile('.*.pth'), file_names='README.rst')

Please note that this a ``re`` pattern not a ``glob`` pattern. You can, but don't have to provide a pattern compiled with re.compile()

The ZIP file is recreated and recompressed, take this into account when deleting files (restrict the size of files you handle, combine patterns instead of doing multiple calls).

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.