#๐Ÿ”’ what is best way to unarchive archives in python?

18 messages ยท Page 1 of 1 (latest)

upbeat groveBOT
#

@remote sundial

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lone onyx
#

i can not think of a universal method for handling these different formats, but i do know two things:

  1. RAR is not an open format so you'll need to use a binary from RARLabs to unarchive these files (or a 3rd party library that uses such binary)
  2. python has the zipfile module for zip archives, but afaik it does not support encrypted files (password protected) edit: it can but can not create encrypted archives
remote sundial
# lone onyx i can not think of a universal method for handling these different formats, but ...
def unarchive(file_path, destination, password=None):
    if password is not None:
        password = password.encode()  # convert password to bytes

    if file_path.endswith('.zip'):
        with zipfile.ZipFile(file_path, 'r') as zip_ref:
            zip_ref.extractall(destination, pwd=password)
    elif file_path.endswith('.rar'):
        with rarfile.RarFile(file_path, 'r') as rar_ref:
            rar_ref.extractall(destination, pwd=password)
    elif file_path.endswith('.7z'):
        with py7zr.SevenZipFile(file_path, mode='r', password=password) as z:
            z.extractall(destination)
    else:
        print("Unsupported file format")
upbeat groveBOT
#

Hey @remote sundial!

It looks like you pasted Python code without syntax highlighting.

Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.

To do this, use the following method:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
lone onyx
#

if you've found a good library for each format, that is probably a fine method

remote sundial
#

because then im using password.encode(), 7z not work but .zip works

lone onyx
#

according to the python documentation, ZipFile.open requires a bytes object for the pwd argument

#

but you'll have to check the docs of the 7z library as well

remote sundial
#

not sure about rar, but i will test

#

i found out that in python so annoying

#

i though you can use 7z library and unarchive any extension archives

lone onyx
#

each archive "extension" uses a different format that needs to be supported

remote sundial
upbeat groveBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ what is best way to unarchive archives in python?