In Python's Standard Library, there is a module called zipfile. Using this module, we can do different operations on a zip file.
The Zip Files are archive file format. Zip files are used to get the loss less data compression features, so from the compressed form, it can be reconstructed perfectly. A zip file can hold one or more compressed files in it.
The good features of zip files are, it reduces the storage requirements, and also transfer the speed over standard connections.
In Python we need to import zipfile module, to use zip files.
from zipfile import ZipFile
At first we will see how to create Zip files from a directory. In this case, the main directory holds a file and some other directories.
So at first, we have to crawl whole directory to get all files and folders and store the paths as a list. Then open the Zip File as write mode, and write all from the stored paths.
Example code
#Get all files from directory and subdirectories and convert to zip from zipfile import ZipFile import os defget_all_files(directory): #function to get all files from directory paths = [] for root, dirs, files in os.walk(directory): for f_name in files: path = os.path.join(root, f_name) #get a file and add the total path paths.append(path) return paths #Return the file paths directory = './sample_files' paths = get_all_files(directory) #Display the filenames print('The following files will be stored in the zip file') for file in paths: print(file) #Open zip file as write mode to create zip with ZipFile('my_files.zip', 'w') as zf: for file in paths: zf.write(file) print('Zip is created Successfully')
Output
The following files will be stored in the zip file ./sample_files\TP_python_prev.pdf ./sample_files\text_files\file1.txt ./sample_files\text_files\file2.txt Zip is created Successfully
Now see, how to extract the zip files using zipfile module. To extract a zip, at first, we have to open the zip file as read mode, and then use extract() or extractall() method to extract the contents. The extract() method takes a path to locate the file from the Zip file to extract.
Example code
#Extract the zip files from zipfile import ZipFile zip_file = 'my_files.zip' with ZipFile(zip_file, 'r') as zf: #display the files inside the zip zf.printdir() #Extracting the files from zip file zf.extractall() print('Zip Extraction Completed')
Output
File Name Modified Sizesample_files/TP_python_prev.pdf 2018-10-19 13:19:48 1915882sample_files/text_files/file1.txt 2018-11-06 13:34:46 22sample_files/text_files/file2.txt 2018-11-06 13:35:02 24Zip Extraction Completed
Now, we will discuss about the infolist() method of zipfile module. This provides a list of different information like filename, Modification date, File Size, etc.
Example code
#Get information about the zip file from zipfile import ZipFile import datetime zip_file = 'my_files.zip' with ZipFile(zip_file, 'r') as zf: for detail in zf.infolist(): print('File Name: ' + detail.filename) print('\tModify Date: ' + str(datetime.datetime(*detail.date_time))) print('\tZip Version: ' + str(detail.create_version)) print('\tSystem Version: ' + str(detail.create_system)) #0 for windows print('\tFile Size (Bytes): ' + str(detail.file_size))
Output
File Name: sample_files/TP_python_prev.pdf Modify Date: 2018-10-19 13:19:48 Zip Version: 20 System Version: 0 File Size (Bytes): 1915882File Name: sample_files/text_files/file1.txt Modify Date: 2018-11-06 13:34:46 Zip Version: 20 System Version: 0 File Size (Bytes): 22File Name: sample_files/text_files/file2.txt Modify Date: 2018-11-06 13:35:02 Zip Version: 20 System Version: 0 File Size (Bytes): 24