Mimic 7zip with python

2020-07-17 06:52发布

问题:

I am using Python 3.6, and currently I subprocess out to my 7zip program to get the compression I need.

subprocess.call('7z a -t7z -ms=off {0} *'.format(filename))

I know the zipfile class has ‘ZIP_LZMA’ compression, but the application I am passing this too says the output file isn’t correct. So what else do I have to add to the ZipFile class to make it mimic the above command?

回答1:

If you do not care much for Windows, then perhaps libarchive could help. In Ubuntu, for example:

$ sudo apt install python3-libarchive-c

Then:

import libarchive
with libarchive.file_writer('test.7z', '7zip') as archive:
    archive.add_files('first.file', 'second.file', 'third.file')

Then there is the pylib7zip library, which wraps the existing 7z.dll and seems to offer a Windows-only alternative.



标签: python 7zip lzma