Modified version of ruamel.std.zipfile that adds support for encrypted zip archives.
Project description
ruamel.std.encryptedzip is a modified library that adds support for encrypted zip archives to ruamel.std.zipfile; linked below.
https://pypi.org/project/ruamel.std.zipfile/
It is super simple, it just adds an extra parameter to the API:
def delete_from_zip_file(self, pattern=None, file_names=None, password=None): #Take extra parameter for password.
"""
zip_file can be a string or a zipfile.ZipFile object, the latter will be closed
any name in file_names is deleted, all file_names provided have to be in the ZIP
archive or else an IOError is raised
"""
if pattern and isinstance(pattern, string_type):
import re
pattern = re.compile(pattern)
if file_names:
if not isinstance(file_names, list):
file_names = [str(file_names)]
else:
file_names = [str(f) for f in file_names]
else:
file_names = []
with zipfile.ZipFile(self._file_name) as zf:
zf.setpassword(password) #Set the password for the zip file.
for l in zf.infolist():
if l.filename in file_names:
file_names.remove(l.filename)
continue
if pattern and pattern.match(l.filename):
continue
self.append(l.filename, zf.read(l))
if file_names:
raise IOError('[Errno 2] No such file{}: {}'.format(
'' if len(file_names) == 1 else 's',
', '.join([repr(f) for f in file_names])))
def delete_from_zip_file(file_name, pattern=None, file_names=None, password=None): #Take extra parameter for password (overloaded function).
with InMemoryZipFile(file_name) as imz:
imz.delete_from_zip_file(pattern, file_names, password) #Parse the extra parameter to the class' method.
It may look scary, but it is very simple. Instead of calling the original function with just a pattern or file_names array, you add an extra parameter where needed:
#Code for zip file stuff goes here
password_zip = b'testing123'
delete_from_zip_file("test.zip", file_names=['test.txt', 'test2.txt'], password=password_zip)
If I remember correctly, Python will throw a TypeError if your password is not encoded into UTF-8 bytes. At the moment, there is no support for directly giving the method your zipfile.ZipFile object, but I will hopefully add support for that in the future.
All credit for code goes to Anthon at Ruamel, all I did was add in a few words and things here and there to add some extra support.
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
Hashes for ruamel.std.encryptedzip-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | dce4eab000202df2b62cc104f78655bb2c1ad41bc74f686ec6901c4637d3d97b |
|
MD5 | c2692effe09daa6830ec60c16fde5aeb |
|
BLAKE2b-256 | 728ce6bacc3bc64079668040aeec4108d8aac399017686ba835af27dc5412362 |
Hashes for ruamel.std.encryptedzip-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 00ea86b0288bcef91f2a4c063bcddbbf8bb5b01804e8e1ff65d00fe56528a722 |
|
MD5 | 7d718aae4071d546c39aa5d5141df966 |
|
BLAKE2b-256 | f20ad42310a06df38991687e1923f240edf1847f786fc7dc25014e321d0e8392 |