Browse Source

Fix creation of password protected ZIP files

test_fixup
Vadim Lebedev 2 years ago
parent
commit
f1a4504aae
2 changed files with 23 additions and 4 deletions
  1. +4
    -0
      libarchive/__init__.py
  2. +19
    -4
      libarchive/zip.py

+ 4
- 0
libarchive/__init__.py View File

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


def set_initila_options(self):
pass

def init(self): def init(self):
if self.mode == 'r': if self.mode == 'r':
self._a = _libarchive.archive_read_new() self._a = _libarchive.archive_read_new()
@@ -506,6 +509,7 @@ class Archive(object):
self._a = _libarchive.archive_write_new() self._a = _libarchive.archive_write_new()
self.format_func(self._a) self.format_func(self._a)
self.filter_func(self._a) self.filter_func(self._a)
self.set_initila_options()
if self.mode == 'r': if self.mode == 'r':
if self.password: if self.password:
self.add_passphrase(self.password) self.add_passphrase(self.password)


+ 19
- 4
libarchive/zip.py View File

@@ -1,5 +1,5 @@
import os, time import os, time
from libarchive import is_archive, Entry, SeekableArchive
from libarchive import is_archive, Entry, SeekableArchive, _libarchive
from zipfile import ZIP_STORED, ZIP_DEFLATED from zipfile import ZIP_STORED, ZIP_DEFLATED




@@ -60,19 +60,34 @@ class ZipEntry(Entry):
CRC = property(_get_missing, _set_missing) CRC = property(_get_missing, _set_missing)
compress_size = property(_get_missing, _set_missing) compress_size = property(_get_missing, _set_missing)


# encryption is one of (traditional = zipcrypt, aes128, aes256)
class ZipFile(SeekableArchive): class ZipFile(SeekableArchive):
def __init__(self, f, mode='r', compression=ZIP_DEFLATED, allowZip64=False, password=None):
def __init__(self, f, mode='r', compression=ZIP_DEFLATED, allowZip64=False, password=None,
encryption=None):
super(ZipFile, self).__init__( super(ZipFile, self).__init__(
f, mode=mode, format='zip', entry_class=ZipEntry, encoding='CP437', password=password 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: if mode == 'w' and compression == ZIP_STORED:
# Disable compression for writing. # Disable compression for writing.
_libarchive.archive_write_set_format_option(self.archive._a, "zip", "compression", "store") _libarchive.archive_write_set_format_option(self.archive._a, "zip", "compression", "store")
self.compression = compression


getinfo = SeekableArchive.getentry getinfo = SeekableArchive.getentry


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")
if self.mode == 'w' and self.password:
if not self.encryption:
self.encryption = "traditional"
_libarchive.archive_write_set_format_option(self._a, "zip", "encryption", self.encryption)

def namelist(self): def namelist(self):
return list(self.iterpaths()) return list(self.iterpaths())




Loading…
Cancel
Save