How to extract rar files inside google colab
Asked Answered
H

7

14

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.

Heeling answered 13/5, 2018 at 12:12 Comment(0)
H
-3

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).

Heeling answered 28/10, 2019 at 10:26 Comment(0)
E
16

The following code snippet worked for me:

!pip install unrar
!unrar x file_path
Elater answered 24/8, 2020 at 6:25 Comment(0)
S
6

The following code snippet worked for me:

get_ipython().system_raw("unrar x file_name")
Sammie answered 14/10, 2018 at 7:42 Comment(0)
S
6
!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

Subjoinder answered 20/4, 2020 at 19:39 Comment(0)
M
0

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.

Mollescent answered 26/10, 2019 at 12:43 Comment(0)
K
0

Try this:

pip install patool
import patoolib
patoolib.extract_archive("foo_bar.rar", outdir="path here")
Kiowa answered 27/12, 2019 at 19:40 Comment(0)
P
0
!unrar x "{Complete path to rar file}"

This worked for me

Pirri answered 30/1, 2021 at 19:12 Comment(1)
I think no need to give the path inside double quotes.Cobia
H
-3

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).

Heeling answered 28/10, 2019 at 10:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.