Browse Source

Fixes to create password protected ZIP files

test_fixup
Vadim Lebedev 2 years ago
parent
commit
c5658c78d7
2 changed files with 13 additions and 11 deletions
  1. +9
    -4
      libarchive/__init__.py
  2. +4
    -7
      libarchive/zip.py

+ 9
- 4
libarchive/__init__.py View File

@@ -499,7 +499,7 @@ class Archive(object):
def __del__(self):
self.close()

def set_initila_options(self):
def set_initial_options(self):
pass

def init(self):
@@ -509,10 +509,14 @@ class Archive(object):
self._a = _libarchive.archive_write_new()
self.format_func(self._a)
self.filter_func(self._a)
self.set_initila_options()
self.set_initial_options()
if self.mode == 'r':
if self.password:
self.add_passphrase(self.password)
if isinstance(self.password, list):
for pwd in self.password:
self.add_passphrase(pwd)
else:
self.add_passphrase(self.password)
call_and_check(_libarchive.archive_read_open_fd, self._a, self._a, self.f.fileno(), self.blocksize)
else:
if self.password:
@@ -594,7 +598,8 @@ class Archive(object):
def write(self, member, data=None):
'''Writes a string buffer to the archive as the given entry.'''
if isinstance(member, str):
member = self.entry_class(pathname=member, encoding=self.encoding)
member = self.entry_class(pathname=member, encoding=self.encoding,
mtime=time.time(), mode=stat.S_IFREG)
if data:
member.size = len(data)
member.to_archive(self)


+ 4
- 7
libarchive/zip.py View File

@@ -64,14 +64,11 @@ class ZipEntry(Entry):
class ZipFile(SeekableArchive):
def __init__(self, f, mode='r', compression=ZIP_DEFLATED, allowZip64=False, password=None,
encryption=None):
self.compression = compression
self.encryption = encryption
super(ZipFile, self).__init__(
f, mode=mode, format='zip', entry_class=ZipEntry, encoding='CP437', password=password
)
self.compression = compression
self.encryption = encryption
if mode == 'w' and compression == ZIP_STORED:
# Disable compression for writing.
_libarchive.archive_write_set_format_option(self.archive._a, "zip", "compression", "store")

getinfo = SeekableArchive.getentry
@@ -79,7 +76,7 @@ class ZipFile(SeekableArchive):
def set_initial_options(self):
if self.mode == 'w' and self.compression == ZIP_STORED:
# Disable compression for writing.
_libarchive.archive_write_set_format_option(self.archive._a, "zip", "compression", "store")
_libarchive.archive_write_set_format_option(self._a, "zip", "compression", "store")
if self.mode == 'w' and self.password:
if not self.encryption:
@@ -124,7 +121,7 @@ class ZipFile(SeekableArchive):
return super(ZipFile, self).read(name)

def writestr(self, member, data, compress_type=None):
if compress_type != self.compression:
if compress_type != self.compression and not (compress_type is None):
raise Exception('Cannot change compression type for individual entries.')
return self.write(member, data)



Loading…
Cancel
Save