METADATA
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
Metadata-Version: 2.0
Name: ruamel.std.zipfile
Version: 0.1.0
Summary: improvements over the standard zipfile package
Home-page: https://bitbucket.org/ruamel/std.zipfile
Author: Anthon van der Neut
Author-email: a.van.der.neut@ruamel.eu
License: MIT License
Platform: UNKNOWN
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
ruamel.std.zipfile
==================
`package ruamel.std.zipfile <https://bitbucket.org/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).