From c5658c78d7a21980c7246ab48eceb592938025d0 Mon Sep 17 00:00:00 2001 From: Vadim Lebedev Date: Thu, 11 Aug 2022 18:40:36 +0200 Subject: [PATCH] Fixes to create password protected ZIP files --- libarchive/__init__.py | 13 +++++++++---- libarchive/zip.py | 11 ++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/libarchive/__init__.py b/libarchive/__init__.py index 2ea051a..9dbb434 100644 --- a/libarchive/__init__.py +++ b/libarchive/__init__.py @@ -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) diff --git a/libarchive/zip.py b/libarchive/zip.py index e19b4f6..a200aa7 100644 --- a/libarchive/zip.py +++ b/libarchive/zip.py @@ -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)