A zip file is a type of archive file format that is used for compressing and storing one or more files and/or directories.
Zip files use lossless compression algorithms, no data is lost during the process. Zip files provide a convenient way to package and distribute files as a single unit. By compressing multiple files to a zip which to manage storage space and it's easy to transfer a single zip file instead of multiple files in a sub folders.
What is zipfile in python
Zipfile is a in-build python module, it can be used to create, read, write, append, and list a ZIP file. This module can be used with other modules or framework, for example suppose if you want to send multiple files as an email from backend server django or flask, instead of attaching each file you can zip those files and attach it to the email client.'
To perform compression and extraction of files we can use the zipfile module which can be imported
1from zipfile import zipFile 2 3files_to_zip = ["note.tsxt", "note.json", "tracking.json"]
The with
statement in Python is used for simplifying resource management, particularly in scenarios where you need to acquire and release resources like files, network connections, or database connections. It helps ensure that resources are properly managed and released, even in the presence of exceptions or errors.
By using with we don't have to mention explicitly to close the file, it will close by auto after the block execution.
Reading a zip file without using with
keyword, we have to explicitly mention to close the opened file, it will not auto close by default.
1myfile = ZipFile("test.zip", mode="r") 2# read metadata 3myfile.printdir() 4# close file 5myfile.close()
Using write mode, ZipFile will create a new zip file in this name test.zip
which will be store in current working directory.
1with ZipFile("test.zip", mode="w") as myfile: 2 myfile.write(files_to_zip[0])
To compress multiple files, pass the file path to write action one by one which will create a new zip file.
1with ZipFile("test.zip", mode="w") as myfile: 2 for file in files_to_zip: 3 myfile.write(file)
Suppose if you want to add a new file to the existing zip file without extracting it, it can be do by using file append mode.
1with ZipFile("test.zip", mode="a") as myfile: 2 myfile.write(files_to_zip[1])
Without extracting the zip file data inside it can be accessed, we can directly read a file from zip file. Using the read mode read the zip as an object then access the particular file using read method then pass the it as argument to json loads method to read the json data from the file.
1import json 2 3with ZipFile("test.zip", mode="r") as myfile: 4 json_data = json.loads(myfile.read("file.json"))
To extract the data from zip file zipfile module have a method called extractall
when calling this, it will extract the file, in the below example it will uncompress the file to current working directory.
1with ZipFile("test.zip", mode="r") as myfile: 2 myfile.extractall("")
By giving path as a argument to extractall method, zip file can be extracted in that particular directory.
1with ZipFile("test.zip", mode="r") as myfile: 2 myfile.extractall("folder/")
To check if a file exists in python we can use exists
method to check
1from os.path import exists 2exists = exists(path_to_file)
similar to exists
, zipfile
module have is_zipfile
method to check is zip file.
1from zipfile import is_zipfile 2 3is_zipfile("test.zip")
printdi
r method will print last modified, file name and size
1with ZipFile("test.zip", mode="r") as myfile: 2 myfile.printdir()
To get metadata for each individual file infolist
can be used to get file size, compression size, etc.
1with ZipFile("test.zip", mode="r") as myfile: 2 for metadata in myfile.infolist(): 3 print(metadata.filename) 4 print(metadata.date_time) 5 print(metadata.file_size) 6 print(metadata.compress_size)
BadZipfile
exception raise when the given file is corrupted
LargeZipFile
exception raise when the given file require ZIP64 functionality but that has not been enabled.
1import json 2from zipfile import BadZipfile, LargeZipFile 3 4try: 5 with ZipFile("testBadFile.zip", mode="r") as myfile: 6 json_data = json.loads(myfile.read("file.json")) 7except BadZipfile as e: 8 print("BadZipfile Error:", str(e)) 9except LargeZipFile as e: 10 print("LargeZipFile Error:", str(e)) 11except Exception as e: 12 print("Error:", str(e))