Python Archiving helps us to combine several files or directories into a single file. This also reduces the size of the file. The most commonly used archive file format is ZIP and TAR. In archiving we have the same operation as file normal file reading and writing operation as zipping and unzipping the file.
In python has a
zipfile module that helps us to create the ZIP file and extract files from the ZIP file. Before performing any operation on a Zip file we need to open the Zip file object that helps us to perform the operation on that file.
The ZipFile constructor requires a filename and mode in which we want to open the zip file and the compression method is an optional argument. In mode we have an ‘r’ to open the file in reading mode, ‘w’ to open the file in write mode, and ‘a’ for append the content into the file.
How to create a new zip file
To create a new zip file we first need to create the ZipFile object that helps us to write data into the zip file. After creating the object we can add the files into the zip file with the help of the write() method. The write method takes the filename as argument.
from zipfile import ZipFile file = ZipFile("data.zip","w"); file.write("greeting.txt") file.write("mydata.txt") print("Zip File created") file.close();
In above example we create an new zip file data.zip and add two files into that file.
How to list the files available inside zip file
The ZipFile object provides a
namelist() method that returns the list of files available inside the zip file. For that, we need to open that file in reading mode. Before call
namelist() method first, we need to create the ZipFile Object.
from zipfile import ZipFile file = ZipFile("data1.zip","r"); #reading all the available filesname inside zip file files=file.namelist() print(files) file.close();
['greeting.txt', 'mydata.txt', 'NewDir/']
How to extract files from zip file
The ZipFile object provides two methods that help us to extract the files available inside the zip file. The
extract(filename) method extracts the specific file mention as an argument. The
extractall(path) method extract all the file available inside the zip file at the specified path if the path is not specified then extract all the available file inside the current working directory.
In below code shows that how to extract a specific file from the zip file.
from zipfile import ZipFile file = ZipFile("data1.zip","r"); #extract single file from data1.zip file.extract('greeting.txt') file.close();
In below code shows that how to extract all the files from the zip file.
from zipfile import ZipFile file = ZipFile("data1.zip","r"); #extract all files from data1.zip into extractdata directory file.extractall('./extractdata') file.close();
How to make an archive file of the directory tree
In python, another module shutil is available that helps us to make the zip file easily, and also we can easily extract the data from that file.
The shutil module provides the
make_directory(base_name, format, root_dir) a function that helps to make the new archive file that contains the directory structure and this also allows to create of archive file in a different format for example ZIP, TAR. This function takes the three-argument base_name means the file name we want to give to the archive file. The format means the file format of the archive file and root_dir means which directory we want to add inside the archive file. If we did not specify the root_dir then create an archive file that contains all the files and directories available inside the current working directory.
import shutil #create zip file that contain data of NewDir and store inside newdir.zip file shutil.make_archive("newdir","zip","NewDir/")
The shutil module also provides the
unpack_archive(filename, extract_dir) a function used to extract the files from the archive file. This method takes two arguments first is the filename and the second is the target directory name which stores the extracted data.
import shutil #Extract the files from newdir.zip shutil.unpack_archive('newdir.zip',"./extractNewDir")