miurahr / py7zr

7zip in python3 with ZStandard, PPMd, LZMA2, LZMA1, Delta, BCJ, BZip2, and Deflate compressions, and AES encryption.
https://pypi.org/project/py7zr/
GNU Lesser General Public License v2.1
461 stars 74 forks source link

Password setter method for SevenZipFile object and/or password argument for extract/read #618

Closed boulwabd closed 1 month ago

boulwabd commented 1 month ago

It is probably not a very common use case but I frequently work with 7z files that I need to try several passwords for or set a password after I have already initialized the object. Setting the password after initializing the SevenZipFile object currently requires updating the password for all the Folder objects within the ArchiveFile objects of each archive member.

It would be nice to have a setpassword function similar to the ZipFile library and/or accept a password argument for the extract and read methods.

EDIT: Well apparently there is some kind of _lzma.LZMAError: Internal error in the decompression when you attempt to read or extract after trying with an incorrect password already. I'm guessing something to do with a file pointer somewhere so maybe it is just easier to instantiate a new object regardless.

miurahr commented 1 month ago

@boulwabd you can find a detailed specification of 7zip file format at https://py7zr.readthedocs.io/en/latest/archive_format.html

7zip format allow, and usually does, compressing header block in 7zip compressing format, and the 7zip compressing format can also be encrypted. When py7zr reading header, which is just after checking signature header, that is a timing a creating object, it requires password if the archive is encrypted.

If you want rewind everythings before reading header, it is almost as same as creating new object.