Unzip rar file python

rarfile — Work with RAR archives¶

The RAR file format is a common archive and compression standard. This module provides tools to read and list a RAR file.

This module is based on the UnRAR library (provided by RARLAB) through a ctypes wrapper, and inspired on Python’s ZipFile.

The module defines the following items:

exception rarfile. BadRarFile ¶

The error raised for bad RAR files.

The class for reading RAR files. See section RarFile Objects for constructor details.

Class used to represent information about a member of an archive. Instances of this class are returned by the getinfo() and infolist() methods of RarFile objects. Most users of the rarfile module will not need to create these, but only use those created by this module. header should be a RARHeaderDataEx instance as returned by unrarlib ; the fields are described in section RarInfo Objects .

rarfile. is_rarfile ( filename ) ¶

Returns True if filename is a valid RAR file based on its magic number, otherwise returns False .

RARLAB Official RAR site. RAR addons RAR addons where you can download UnRAR library sources.

RarFile Objects¶

Open a RAR file, where file should be a path to a file (a string). The mode parameter should be ‘r’ to read an existing file (only allowed mode at the moment).

Return a RarInfo object with information about the archive member name. Calling getinfo() for a name not currently contained in the archive will raise a KeyError .

Return a list containing a RarInfo object for each member of the archive. The objects are in the same order as their entries in the actual RAR file on disk if an existing archive was opened.

Return a list of archive members by name.

RarFile. open ( member [ , pwd ] ) ¶

Extract a member from the archive as a file-like object (see Python’s io.BytesIO ). member is the name of the file in the archive, or a RarInfo object. pwd is the password used for encrypted files.

Return the bytes of the file member in the archive. member is the name of the file in the archive, or a RarInfo object. pwd is the password used for encrypted files and, if specified, it will override the default password set with setpassword() .

Extract a member from the archive to the current working directory; member must be its full name or a RarInfo object). Its file information is extracted as accurately as possible. path specifies a different directory to extract to. member can be a filename or a RarInfo object. pwd is the password used for encrypted files.

RarFile. extractall ( path=None, members=None, pwd=None ) ¶

Читайте также:  Java stream collect to map example

Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist() . pwd is the password used for encrypted files.

Never extract archives from untrusted sources without prior inspection. It is possible that files are created outside of path, e.g. members that have absolute filenames starting with «/» or filenames with two dots «..» .

Print a table of contents for the archive to sys.stdout .

Set pwd as default password to extract encrypted files.

Read all the files in the archive and check their CRC’s and file headers. Return the name of the first bad file, or else return None .

The following data attribute is also available:

The comment text associated with the RAR file, if any.

RarInfo Objects¶

Instances of the RarInfo class are returned by the getinfo() and infolist() methods of RarFile objects. Each object stores information about a single member of the RAR archive.

Instances have the following attributes:

Name of the file in the archive.

The time and date of the last modification to the archive member. This is a tuple of six values:

Index Value
0 Year (>= 1980)
1 Month (one-based)
2 Day of month (one-based)
3 Hours (zero-based)
4 Minutes (zero-based)
5 Seconds (zero-based)

The RAR file format does not support timestamps before 1980.

Type of compression for the archive member.

Comment for the individual archive member.

System which created RAR archive.

RAR version needed to extract archive.

CRC-32 of the uncompressed file.

Size of the compressed data.

Size of the uncompressed file.

© Copyright 2012, Matias Bordese. Revision b1ac46cb .

Versions latest stable v0.3 v0.2 Downloads pdf htmlzip epub On Read the Docs Project Home Builds Free document hosting provided by Read the Docs.

Источник

python-unrar

Work with RAR archive files through unrar library using ctypes.

Install UnRAR library

python-unrar requires UnRAR library. You can download UnRAR library sources from:

http://www.rarlab.com/rar_add.htm 

and compile (you may need to rename the makefile that you want to use according to your OS) and install it from there:

$ make lib $ make install-lib 

For Windows you can also download the already compiled library (http://www.rarlab.com/rar/UnRARDLL.exe).

If you prefer not to install the library, you should make it «findable» by adding the library file to a directory where libraries are searched (or change required environment variable).

As an alternative, you can also set UNRAR_LIB_PATH variable in your environment to the library path and python-unrar will try to load the UnRAR library from there.

Testing without installing

You can build from source with:

And then run a Python shell with unrar available:

$ PYTHONPATH=`pwd`/build/lib python 

Or you could also directly add the unrar directory from this repo to your PYTHONPATH . In any case you will still need to make the unrar library available as mentioned above.

Install python-unrar

Examples

>>> from unrar import rarfile >>> rar = rarfile.RarFile('sample.rar') >>> rar.namelist() [u'test_file.txt'] >>> rar.printdir() File Name Modified Size test_file.txt 2013-04-14 08:20:28 17 >>> rar.testrar() >>> info = rar.infolist()[0] >>> info.filename u'test_file.txt' >>> info.file_size 17L >>> info.date_time (2013L, 4L, 14L, 8L, 20L, 28L) >>> rar.extractall() >>> rar.read('test_file.txt') 'This is for test.' 

Источник

Different ways to unzip a file in Python

Learn Algorithms and become a National Programmer

When working with archived files there are various ways in which you can unzip your file. In this article we will learn how to extract single or multiple files from a zip file, unzip the whole file etc.

Читайте также:  Setting properties file java

Different ways to unzip a file:

  1. Extracting all the members(content) of an archived file. OR Extracting all the members(content) of the archived file in the current directory.
  2. Extracting all the members(content) of an archived file in another directory.
  3. Extracting only some specific member(content) of an archived file based on different conditions.

Module used:

zipfile: ZIP file format being the most common archive and compression standard the zipfile module is used to access functionalities which would help us to create,read,write,extract and list a ZIP file.

Opening an archive file in read mode

We are going to work on the ZIP file format so we first need to learn how to read a zip file in python or rather open a zip file in read mode.We will use the ZipFile() function of the zipfle module to open a zip file in read mode.

ZipFile():This function is used to open in read,write or append mode or to create a new zip file.It provides us with a way to read ,write and append to a zip file.It creates a zipfile object of the specified file.

Syntax:ZipFile(file,,mode=»read/write/append/create»,compression=»compression method»,allowZip64=yes)

Parameters

  • file: This argument should be a filename or the path to a zip file or a file object or a path object.
  • mode:This argument defines the mode in which the zip file needs to be accessed.
    There are four modes:
  1. r:This mode is used to read an existing file.
  2. w:This mode is used to write to a file.
  3. a:This mode is used to append to a file.
  4. x:This mode is used to exclusively create a new file and write to it.
  • compression:This argument stores the compression method for the zip file.it can have the following values:
  1. ZIP_STORED
  2. ZIP_DEFLATED
  3. ZIP_BZIP2
  4. ZIP_LZMA
  • allowZip64:This argument stores a boolean value that is either true or false.If the zipfile is larger than 4 GiB then it requires ZIP64 extension an in this case allowZip64 is set to true otherwise it is d=set to false.

Extracting all the members(content) of the archived file

Extracting all the members(content) of an archived file in the current directory

We can use the functionalities provided by the zipfile module to extract all the content of an archived file.Using the extractall() function of the zipfile module we can extract all the content of the archived file at once.

extractall():

This function takes the path where the archive needs to be extracted,the members(content) that needs to be extracted and the password of the archive if it is encrypted and extracts all the members of archive file if the member argument is set to none.

Syntax:extractall(path,members,pwd)

Parameters:

path:This argument stores a path to directory where the zip files need to be extracted, if it is not specified the file is extracted in the current working directory.

members:This argument should be a list containing the names of the members that we want to extract from the zip file.

pwd:This argument is a string containing the password of the zip file if the file is encrypted.

Example program:

import zipfile import os #Printing list of item in the current working directory before extracting contents of zip file print("List of items in the directory before extraction") for item in os.listdir(path='.'): print(item) print("\n\n") #opening the zip file in read mode. #Extracting all the content of the zip file in current working directory. with zipfile.ZipFile("logbackup.zip","r") as zf: zf.extractall() #Printing list of item in the current working directory before extracting contents print("List of items in the directory after extraction") for item in os.listdir(path='.'): print(item) 

Output:

List of items in the directory before extraction a b c file6 logbackup.zip zptry.py List of items in the directory after extraction a b c file1.txt file4 file5 file6 logbackup.zip metadata.txt zptry.py 

Extracting all the members(content) of an archived file in another directory.

Now that we know how to extract the contents of an archived file into the current directory we can learn how to extract the contents into another directory .We might need this feature when are need the files to be organized into a new directory or to be extracted into a particular directory.We will use the extractall() function with the argument path=»the path of the directory where we need to extract the content of zip file».

Читайте также:  Https ssl curl php

Example program:

import zipfile import os #Opening the zip file in read mode #extracting all the content of the zip file in directory 'trial'. with zipfile.ZipFile("logbackup.zip","r") as zf: zf.extractall('trial') print("List of items in the specified directory after extraction") pth=os.path.join(os.getcwd(),'trial') for item in os.listdir(path=pth): print(item) 

Output:

List of items in the specified directory after extraction file1.txt file4 file5 metadata.txt 

Extracting only some specific member (content) of an archived file based on different conditions.

1.Extracting a list of files from all the files in the archive

We can extract only the files which we want by passing a list of names of the files we want to extract .For this article we assume we have a zip file which contains 12 files name january.txt,february.txt. upto december.txt respectively and we need to extract only the files name january,july,august so we create a list containing the names of the required files and pass this list as an argument in the extractall() function .

Example program:

import zipfile import os flist=['january.txt','july.txt','august.txt'] with zipfile.ZipFile("logbackup.zip","r") as zf: zf.extractall('months',members=flist) print("List of items in the specified directory after extraction") pth=os.path.join(os.getcwd(),'months') for item in os.listdir(path=pth): print(item) 

Output:

List of items in the specified directory after extraction august.txt january.txt july.txt 

There can be various condition which we can use to extract members of the archive like size of file,ate and time of last modification,file name start with ‘XXX’,whether the member is a directory or not etc.

With this article at OpenGenus, you must have a complete idea of Different ways to unzip a file in Python.

Devansh Thapa

Devansh Thapa

Intern at OpenGenus | Student at Shivalik College of Engineering

OpenGenus Tech Review Team

Software Engineering

Everything about Constructors in C++

A constructor is a special member function which enables the object of a class to initialize itself when it is created. We have explored different types of constructors, constructor overloading and constructor with default arguments.

zipfile module in Python

We have explored zipfile module in Python in depth. It is used for all operations related to ZIP file and used to create such ZIP files.

Devansh Thapa

Devansh Thapa

Источник

Оцените статью