3

recently started to learn python and there was a problem .... I need to unzip the rar file, which is password protected, but the password is written in the file name, which is in the archive. And so ~ 300 times. How, with what and where can this be implemented? For example, there is an archive 383442389.rar, it has two files, pass-1337643656.data (the name contains the archive password) and the following archive)

1 Answer 1

6

I'm not sure of all the context of your question, but I will try to answer you.

  1. You said that you need to unzip a file .rar, but that is not possible, if the file is .rar you need to unrar

  2. If you need just to unzip a .zip

something like that should work

from zipfile import ZipFile
with ZipFile('test1.zip', 'r') as myzip:
    myzip.extractall(pwd='<password>')
  1. In the case of .rar

pip install rarfile (installed only in python 3)

from rarfile import RarFile
with RarFile('test2.rar', 'r') as myrar:
    myrar.extractall(pwd='<password>')
  1. you mention that the password comes in the .rar like a name of file
    • pass-1337643656.data
    • you can list the names of the files using

RarFile.namelist() Return a list of archive members by name.

So you can extract the password using split

s = 'pass-1337643656.data'
s.split('-')[1].split('.')[0]
print(s)

'1337643656'

you can use also a regular expression to extract that

Conclusion.

A complete solution using .rar could be

from rarfile import RarFile
with RarFile('test2.rar', 'r') as myrar:
    file_names = myrar.namelist()
    pass_file = list(filter(lambda k: 'pass' in k, l))[0]
    pass = pass_file.split('-')[1].split('.')[0]
    myrar.extractall(pwd=pass)

Reference.

4
  • Hello! Yes, I was a little mistaken with the words, I need a case with .rar. In this case, I still need to do the code execution up to 300 times (so that only the final result appears in C: \ Users \ Usero \ Desktop). Still, how can I specify the path to test2.rar? Here is an example of such a rar file, you can look at the link: myfile.is/h0325b00o8/Enclosed_rar (Also, when compiling, an error occurs: Syntax error in C: \ Users \ Usero \ script.py File "C: \ Users \ script.py", line 5 pass = pass_file..split ('-') [1] .split ('.') [0] ^ SyntaxError: invalid syntax Commented May 17, 2020 at 22:39
  • @IgorBezrodnyy I set the path relative to the current folder where I have my script in this line with RarFile('test2.rar', 'r') as myrar:``, I you read the referece that I let you regarding to rarfile you can see the specification of the constructor of the object RarFile, as you can see here rarfile.RarFile(file[, mode='r'])``` where file should be a path to a file Commented May 18, 2020 at 2:15
  • Regarding to the syntax error in the message you can see there are 2 unnecessary points pass_file..split ('-') [1] .split ('.') [0], maybe an error of copy and paste , because of in the answer I didn't see that error, should be pass_file.split ('-') [1] .split ('.') [0] Commented May 18, 2020 at 2:20
  • @IgorBezrodnyy The idea of stackoverflow isn't to solve all your problem, just clarify something more specifically, that maybe you don't know or a weird I know that you are a new user of stackoverflow, I think that you need to double check this page stackoverflow.com/tour Commented May 18, 2020 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.