1

I am trying to unrar files uploaded by users in django web application. I have gone through different approaches but none of them are working for me. The below methods work fine in my local OS (Ubuntu 18.04), but n't working in the server. I am using Python 3.6.

  1. import rarfile - here 'rarfile' is imported from python3.6/site-packages/rarfile. It is giving this error -

    bsdtar: Error opening archive: Failed to open '--'

    Following the steps suggested here, I installed 'unrar'.

  2. from unrar import rarfile - here it's being imported from python3.6/site-packages/unrar/

    But this gave the error - LookupError: Couldn't find path to unrar library.

    So to solve the issue, I followed the steps mentioned here, and created UnRaR executable, and also set the path in my environment. This works fine in local, but unable to understand how to do the same in 'production environment'.

  3. import patoolib - importing from patool

    I also tried implementing patool, which worked fine in local but not in production and I am getting this error - patoolib.util.PatoolError: could not find an executable program to extract format rar; candidates are (rar,unrar,7z),

This has become a big blocker in my application, can anyone please explain how to solve the problem.

Code snippet for rarfile -

import rarfile
.....        
rf = rarfile.RarFile(file_path)
rf.extractall(extract_to_path)

Code snippet for patool -

import patoolib
...
patoolib.extract_archive(file_path, outdir=extract_to_path)
Tom Wojcik
  • 5,471
  • 4
  • 32
  • 44
gowthz
  • 400
  • 2
  • 8
  • 22

1 Answers1

0

Patool is the good python library it is simple and easy. A simple example of it is here:

pip install patool

Use pip install patool to install the library

import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")

Now, in your case the problem is probably that you don't have your unraring tool in the path, indicating it can not be called from the command line (which is exactly what patool does). Just put your rar file folder to the path and you're fine or give the absolute path to the rar file.

Shahid Tariq
  • 886
  • 6
  • 19
  • Thanks for the reply. From what I understand, patool calls the unraring tool from command line. I am getting confused in setting up this 'unraring tool'. Can you please explain that part. Or a link to a step by step guide with be of great help! – gowthz Dec 03 '20 at 17:11