I want to compress a directory recursively into a password encrypted 7zip archive in a platform-independent way.
I considered using these approaches, but none of them I found acceptable:
Calling 7zip executable directly using subprocess
- This works great, but it's not platform-independent.
Using the pylzma
/py7zlib
modules - They work only on data already in memory, I can't imagine how to use them to compress directories.
As far as I can see, it's not possible. But if these directories and folders are only going to be manipulated by software that you write, you can create your own "home-made" version of 7zip using the technique shown at compress-a-folder-recursively-as-7z-with-pylzma-and-py7zlib. This uses the tarfile
module to gather the directory tree into a single file and then using the lzma / pyliblzma module to do the compression.
Unfortunately, the common Python LZMA modules do not offer password protection / encryption. But you can add your own, simply by encrypting the tarfile data before compressing it. See Encrypt & Decrypt using PyCrypto AES 256 for details.