How to extract rar files inside google colab

Question:

I have a dataset in google drive which I want to use in google colab.But I can’t unrar the rar files by any means.So far I have tried installing python libraries and also ubuntu packages like “unrar ,rar ,unrar-free ,unar ,unp” and I just can’t open the damn file.Here are the results of each command:

!rar x data.rar

RAR 5.40   Copyright (c) 1993-2016 Alexander Roshal   15 Aug 2016
Trial version             Type RAR -? for help


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4

!unrar data.rar

UNRAR 5.50 freeware      Copyright (c) 1993-2017 Alexander Roshal


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4

!unp meta-data.rar

RAR 5.40   Copyright (c) 1993-2016 Alexander Roshal   15 Aug 2016
Trial version             Type RAR -? for help


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4

UNRAR 5.50 freeware      Copyright (c) 1993-2017 Alexander Roshal


Extracting from meta-data.rar

Cannot create meta-data/sample_submission.csv
No such file or directory
Cannot create meta-data/test.csv
No such file or directory
Cannot create meta-data/train.csv
No such file or directory
Cannot create directory meta-data
Input/output error
Total errors: 4
Can't exec "file": No such file or directory at /usr/bin/unp line 419.
Failed to detect file type of meta-data.rar.
WARNING: There were errors while processing files!

None of the others a re working either so any ideas are welcomed.

Asked By: dhrumil barot

||

Answers:

The following code snippet worked for me:

get_ipython().system_raw("unrar x file_name")
Answered By: Arka Saha

You can write a simple python code to extract the zip-file directly in your google drive from google colab.

Note: For this code to work, you’ll need to install a module named rarfile in colab. You can do so by the following code snippet:

pip install rarfile

Without getting into the details of how it works, go ahead and copy the code snippet below into google colab and run the cell.

def unrar(dpath,xpath):
  for rar in os.listdir(dpath):
    filepath = os.path.join(dpath, rar)
    with rarfile.RarFile(filepath) as opened_rar:
      for f in opened_rar.infolist():
        print (f.filename, f.file_size)
        opened_rar.extractall(xpath)

unrar(dpath,xpath)

Here, dpath is the path directory where your .rar file is located. xpath is where you want to extract it.

Answered By: Mayank Khanna

I have tried lots of solutions since then, but the best one has been to get the file from drive to collab’s storage using “rsync” Linux command (install rsync using “!apt install rsync”) and then “unzip” command. It is lightning fast after that (71.32MB/s).

Answered By: dhrumil barot

Try this:

pip install patool
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")
Answered By: Suguru Naresh
!pip install pyunpack
!pip install patool
from pyunpack import Archive
Archive('Location of the rar file').extractall('Location where you want to have the folder')

Check out this code snippet

Really simple and super fast

Answered By: Srivatsav Raghu

The following code snippet worked for me:

!pip install unrar
!unrar x file_path
Answered By: Arun
!unrar x "{Complete path to rar file}"

This worked for me

Answered By: Somnath Chatterjee

Below Code Will Work

!unrar x dataset/Hnd.rar
Answered By: Bhautik Sudani
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.