1

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:

  1. Calling 7zip executable directly using subprocess - This works great, but it's not platform-independent.

  2. Using the pylzma/py7zlib modules - They work only on data already in memory, I can't imagine how to use them to compress directories.

Babken Vardanyan
  • 14,090
  • 13
  • 68
  • 87
  • I guess you don't want to walk the directory tree & read everything into memory to compress it. :) Another approach is discussed [here](http://stackoverflow.com/questions/13130634/compress-a-folder-recursively-as-7z-with-pylzma-and-py7zlib) – PM 2Ring Oct 04 '14 at 12:22
  • @PM2Ring No, concatenating files in memory is not my intention. I am simply looking for a script variant of Right Click on directory -> 7-Zip -> Add to archive. The link you posted does not discuss password protection or encryption, and the answer does not mention 7-Zip. – Babken Vardanyan Oct 04 '14 at 13:09
  • No, it's not exactly 7-Zip: the linked method creates an LZMA compressed tar archive, which should offer a similar compression ratio to a standard 7zip file. But of course it requires custom software to read & write. I'm not familiar with the various Python LZMA modules, but I see from a quick Googling that they don't offer password protection / encryption. Of course, adding such protection yourself's not too difficult, but once again, it's not going to be compatible with standard 7-Zip utilities. Oh well. – PM 2Ring Oct 04 '14 at 13:26
  • @PM2Ring, My primary concern is the strong security via AES-256 encryption that 7-Zip format offers (and it's cross-platformness). I can use other archive formats/modules for directory compression, but they don't offer encryption. So if you add your comment as an answer I'll accept it. – Babken Vardanyan Oct 04 '14 at 13:32
  • That's very kind, considering that I didn't actually answer your question. – PM 2Ring Oct 04 '14 at 13:37
  • @PM2Ring Uh, the answer is "It's not possible", which is a valid answer in my opinion :) – Babken Vardanyan Oct 04 '14 at 13:38

2 Answers2

3

Now in Jun, 2020, it can be with py7zr v0.8.0 (https://pypi.org/project/py7zr/).

pip install py7zr


import py7zr
with py7zr.SevenZipFile('target.7z', 'w', password='secret') as arc:
    arc.writeall('.')

then you may observe a target.7z which is compatible with 7z command in Linux, mac and Windows.

Hiroshi Miura
  • 91
  • 1
  • 5
1

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.

Community
  • 1
  • 1
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182