Prerequisites to Work with Zip Files
- You must know the file handling of Python to understand Zip file handling. If you don't know the file handling, head over to the W3Schools File Handling section to learn.
- Python concepts like conditionals, loops, functions, classes, etc.,
Open this link to download all of the Zip folders which I have used in the upcoming sections.
What is Zip File?
Zip is an archive file format which supports the lossless data compression. The Zip file is a single file containing one or more compressed files.
Uses for Zip File?
- Zip files help you to put all related files in one place.
- Zip files help to reduce the data size.
- Zip files transfer faster than the individual file over many connections.
zipfile Module
Explore all the methods and classes of the zipfile module using dir() method. See the code to get all the classes and methods of the zipfile module.
import zipfile
print(dir(zipfile))
['BZIP2_VERSION', 'BadZipFile', 'BadZipfile', 'DEFAULT_VERSION', 'LZMACompressor', 'LZMADecompressor', 'LZMA_VERSION', 'LargeZipFile', 'MAX_EXTRACT_VERSION', 'PyZipFile', 'ZIP64_LIMIT', 'ZIP64_VERSION', 'ZIP_BZIP2', 'ZIP_DEFLATED', 'ZIP_FILECOUNT_LIMIT', 'ZIP_LZMA', 'ZIP_MAX_COMMENT', 'ZIP_STORED', 'ZipExtFile', 'ZipFile', 'ZipInfo', '_CD64_CREATE_VERSION', '_CD64_DIRECTORY_RECSIZE', '_CD64_DIRECTORY_SIZE', '_CD64_DISK_NUMBER', '_CD64_DISK_NUMBER_START', '_CD64_EXTRACT_VERSION', '_CD64_NUMBER_ENTRIES_THIS_DISK', '_CD64_NUMBER_ENTRIES_TOTAL', '_CD64_OFFSET_START_CENTDIR', '_CD64_SIGNATURE', '_CD_COMMENT_LENGTH', '_CD_COMPRESSED_SIZE', '_CD_COMPRESS_TYPE', '_CD_CRC', '_CD_CREATE_SYSTEM', '_CD_CREATE_VERSION', '_CD_DATE', '_CD_DISK_NUMBER_START', '_CD_EXTERNAL_FILE_ATTRIBUTES', '_CD_EXTRACT_SYSTEM', '_CD_EXTRACT_VERSION', '_CD_EXTRA_FIELD_LENGTH', '_CD_FILENAME_LENGTH', '_CD_FLAG_BITS', '_CD_INTERNAL_FILE_ATTRIBUTES', '_CD_LOCAL_HEADER_OFFSET', '_CD_SIGNATURE', '_CD_TIME', '_CD_UNCOMPRESSED_SIZE', '_ECD_COMMENT', '_ECD_COMMENT_SIZE', '_ECD_DISK_NUMBER', '_ECD_DISK_START', '_ECD_ENTRIES_THIS_DISK', '_ECD_ENTRIES_TOTAL', '_ECD_LOCATION', '_ECD_OFFSET', '_ECD_SIGNATURE', '_ECD_SIZE', '_EndRecData', '_EndRecData64', '_FH_COMPRESSED_SIZE', '_FH_COMPRESSION_METHOD', '_FH_CRC', '_FH_EXTRACT_SYSTEM', '_FH_EXTRACT_VERSION', '_FH_EXTRA_FIELD_LENGTH', '_FH_FILENAME_LENGTH', '_FH_GENERAL_PURPOSE_FLAG_BITS', '_FH_LAST_MOD_DATE', '_FH_LAST_MOD_TIME', '_FH_SIGNATURE', '_FH_UNCOMPRESSED_SIZE', '_SharedFile', '_Tellable', '_ZipDecrypter', '_ZipWriteFile', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_check_compression', '_check_zipfile', '_get_compressor', '_get_decompressor', 'binascii', 'bz2', 'compressor_names', 'crc32', 'error', 'importlib', 'io', 'is_zipfile', 'lzma', 'main', 'os', 're', 'shutil', 'sizeCentralDir', 'sizeEndCentDir', 'sizeEndCentDir64', 'sizeEndCentDir64Locator', 'sizeFileHeader', 'stat', 'stringCentralDir', 'stringEndArchive', 'stringEndArchive64', 'stringEndArchive64Locator', 'stringFileHeader', 'struct', 'structCentralDir', 'structEndArchive', 'structEndArchive64', 'structEndArchive64Locator', 'structFileHeader', 'sys', 'threading', 'time', 'zlib']
You have seen a bunch of classes and methods right. But, you are not going to learn all of them. You will learn only some classes and methods to work with the Zip files.
Let's see some useful Exceptions, Classes, and Methods with brief explanations.
Exceptions
Exception is a message which is used to display the exact error as you like. In Python, you use try, except, finally keywords for the error handling.
If you are not familiar with the error handling, go to Pythons Error Handling documentation to learn the error handling.
Let's see all exceptions in zipfile module.
zipfile.BadZipFile
zipfile.BadZipFile is an exception in the zipfile module. This error will raise for Bad Zip files. See the example below.
import zipfile
def main():
try:
with zipfile.ZipFile('sample_file.zip') as file:
print("Ok")
except zipfile.BadZipFile:
print('Error: Zip file is corrupted')
if __name__ == '__main__': main()
zipfile.LargeZipFile
Suppose if you want to work with a large Zip file, you need to enable the ZIP64 functionality while opening the Zip. If you don't enable it, LargeZipFile will raise. See the example.
import zipfile
def main():
try:
with zipfile.ZipFile('sample_file.zip') as file:
print('File size is compatible')
except zipfile.LargeZipFile:
print('Error: File size if too large')
if __name__ == '__main__': main()
import zipfile
def main():
try:
with zipfile.ZipFile('sample_file.zip', mode = 'r', allowZip64 = True) as file:
print('File size is compatible')
except zipfile.LargeZipFile:
print('Error: File size if too large')
if __name__ == '__main__': main()
Choose a Zip file which best suits for the Exception handling and then tries to run the program. You will get a clear Idea.
Classes
In simple words, class is a set of methods and attributes. You use the class methods and attributes wherever you want by creating the instances of class.
Let's see some classes of the zipfile module.
zipfile.ZipFile
The most common class which is used to work with Zip Files is ZipFile class.
zipfile.ZipFile is used to write and read the Zip files. It has some methods which are used to handle the Zip files.
Now, explore the methods of the ZipFile class using the dir() objects. See the code.
import zipfile
print(dir(zipfile.ZipFile))
['_RealGetContents', '__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_extract_member', '_fpclose', '_open_to_write', '_sanitize_windows_name', '_windows_illegal_name_trans_table', '_write_end_record', '_writecheck', 'close', 'comment', 'extract', 'extractall', 'fp', 'getinfo', 'infolist', 'namelist', 'open', 'printdir', 'read', 'setpassword', 'testzip', 'write', 'writestr']
You've already used the zipfile.ZipFile class to read Zip files in previous examples.
zipfile.ZipFile contains many methods like extract, open getinfo, setpassword, etc., to work the Zip files.
Let's see some methods of the ZipFile class.
import zipfile
def main():
with zipfile.ZipFile('sample_file.zip') as file:
print(file.infolist())
print(file.namelist())
print(file.getinfo(file.namelist()[-1]))
text_file = file.open(name = file.namelist()[-1], mode = 'r')
print(text_file.read())
text_file.close()
file.extractall()
if __name__ == '__main__': main()
[<ZipInfo filename='extra_file.txt' filemode='-rw-rw-rw-' file_size=59>, <ZipInfo filename='READ ME.txt' filemode='-rw-rw-rw-' file_size=59>, <ZipInfo filename='even_odd.py' filemode='-rw-rw-rw-' file_size=129>]
['extra_file.txt', 'READ ME.txt', 'even_odd.py']
<ZipInfo filename='even_odd.py' filemode='-rw-rw-rw-' file_size=129>
b"num = int(input('Enter a Number:- '))\r\nif num % 2 == 0:\r\n\tprint('{} is Even'.fromat(num))\r\nelse:\r\n\tprint('{} is Odd'.fromat(num))"
If you want to learn all of the methods of ZipFile class use the help() function on the method you want to learn.
Or go to the official documentation of Python to learn.
zipfile.ZipInfo
zipfile.ZipInfo class used to represent the member of a Zip folder.
First, explore all the objects of zipfile.ZipInfo class using dir() method. See the code below.
import zipfile
print(dir(zipfile.ZipInfo))
['CRC', 'FileHeader', '__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '_decodeExtra', '_encodeFilenameFlags', '_raw_time', 'comment', 'compress_size', 'compress_type', 'create_system', 'create_version', 'date_time', 'external_attr', 'extra', 'extract_version', 'file_size', 'filename', 'flag_bits', 'from_file', 'header_offset', 'internal_attr', 'is_dir', 'orig_filename', 'reserved', 'volume']
Now, you are going to see some methods of the class zipfile.ZipInfo
import zipfile
def main():
with zipfile.ZipFile('sample_file.zip') as file:
archive = file.infolist()
read_me_file = archive[-1]
print("Name of the file:- {}".format(read_me_file.filename))
print("Size of the file:- {}".format(read_me_file.file_size))
print("Is directory:- {}".format(read_me_file.is_dir()))
print("File created data & time:- {}".format(read_me_file.date_time))
if __name__ == '__main__': main()
Name of the file:- sample_file/READ ME.txt
Size of the file:- 59
Is directory:- False
File created data & time:- (2018, 10, 4, 11, 32, 22)
Go to ZipInfo, if you want to learn more about the objects of ZipInfo.
Methods
Methods are a block of code for a specific functionality in the program. For example, if you want to find the absolute value of a number, you can use the Pythons methods called abs.
You can use it anywhere you want. Let's see some methods of the zipfile module.
zipfile.is_zipfile()
is_zipfile(filename) method of zipfile module returns True if the file is a valid Zip otherwise it returns False.
Let's see an example.
import zipfile
def main():
print(zipfile.is_zipfile('sample_file.zip'))
if __name__ == '__main__': main()
Handling Zip Files
In this section, you will learn how to handle Zip files like Opening, Extracting, Writing, etc..,.
Extracting the files of a Zip file using the extractall method to the current directory.
import zipfile
def main():
file_name = 'sample_file.zip'
with zipfile.ZipFile(file_name, 'r') as file:
print(file.printdir())
print('Extracting all files...')
file.extractall()
print('Done!')
if __name__ == '__main__': main()
File Name Modified Size
sample_file/ 2018-10-04 11:33:22 0
sample_file/even_odd.py 2018-06-29 23:35:54 129
sample_file/READ ME.txt 2018-10-04 11:32:22 59
None
Extracting all files...
Done!
To extract a Zip with a password, you need to pass a value to pwd positional argument of extract(pwd = password) or extractall(pwd = password) methods.
You must pass a password which is in bytes. To convert a str to bytes use the Pythons built-in method bytes with utf-8 encode format.
Let's see an example.
import zipfile
def main():
file_name = 'pswd_file.zip'
pswd = 'datacamp'
with zipfile.ZipFile(file_name) as file:
file.extractall(pwd = bytes(pswd, 'utf-8'))
if __name__ == '__main__': main()
You can also extract files by using the setpassword(pwd = password) method of ZipFile class. See the example below.
import zipfile
def main():
file_name = 'pswd_file.zip'
pswd = 'datacamp'
with zipfile.ZipFile(file_name) as file:
file.setpassword(pwd = bytes(pswd, 'utf-8'))
file.extractall()
if __name__ == '__main__': main()
Creating Zip Files
To create a Zip file, you don't need any extra methods. Just pass the name to the ZipFile class, and it will create an archive in the current directory.
See the below example.
import zipfile
def main():
archive_name = 'example_file.zip'
with zipfile.ZipFile(archive_name, 'w') as file:
print("{} is created.".format(archive_name))
if __name__ == '__main__': main()
example_file.zip is created.
Writing to Zip Files
You have to open Zip files in write mode to write files to the archive file. It overrides all the existing files in the Zip.
Let's an example.
import zipfile
def main():
file_name = 'sample_file.zip'
with zipfile.ZipFile(file_name, 'w') as file:
file.write('extra_file.txt')
print('File overrides the existing files')
with zipfile.ZipFile(file_name, 'r') as file:
print(file.namelist())
if __name__ == '__main__': main()
File overrides the existing files
['extra_file.txt']
Appending Files to Zip
You have to open Zip in append(a) mode in order to append any files to the Zip. It doesn't override the existing files.
Let's see an example.
import zipfile
def main():
file_name = 'sample_file.zip'
with zipfile.ZipFile(file_name, 'a') as file:
file.write('READ ME.txt')
file.write('even_odd.py')
print('Files added to the Zip')
with zipfile.ZipFile(file_name, 'r') as file:
print(file.namelist())
if __name__ == '__main__': main()
Files added to the Zip
['extra_file.txt', 'READ ME.txt', 'even_odd.py']
Till now, you have learned how to handle Zip files. Now, you will able to open, write, append, extract, create, etc.., Zip files. Now, you're going to write a simple program.
Let's see what it is?
Extracting Multiple Sub Zips with Password Using Loops and zipfile
- You have a Zip which contains some sub Zip files in depth. And each of the Zip files has a password which is their name. Our challenge is to unzip all the Zip until you reach the end.
Steps To Solve The Problem
- Extract the Parent File file using its name as the password.
- Get the first child name using the namelist() method. Store it in a variable.
I have created the above Procedure. If you want you can change it according to your files arrangement.
import zipfile
def main():
parent_file_name = '000.zip'
with zipfile.ZipFile(parent_file_name, 'r') as parent_file:
pswd = bytes(parent_file_name.split('.')[0], 'utf-8')
parent_file.extractall(pwd = pswd)
next_zip_name = parent_file.namelist()[0]
while True:
if zipfile.is_zipfile(next_zip_name):
with zipfile.ZipFile(next_zip_name, 'r') as child_file:
pswd = bytes(next_zip_name.split('.')[0], 'utf-8')
child_file.extractall(pwd = pswd)
next_zip_name = child_file.namelist()[0]
else:
break
if __name__ == '__main__': main()
After executing the above program, you'll see all the sub zips are extracted to the current directory.
End Note
Congratulations on completing the tutorial!
Hope you enjoyed the tutorial. This article helps you a lot when you are working with Zip files. Now, you are able to work with the Zip files.
If you have any doubts regarding the article, ask me in the comment section. I will reply as soon as possible.
Again, if you are new to Python take DataCamp's free Intro to Python for Data Science course to learn Python language or read Pythons official documentation.
Happy Coding!
Prerequisites to Work with Zip Files
Open this link to download all of the Zip folders which I have used in the upcoming sections.
What is Zip File?
Zip is an archive file format which supports the lossless data compression. The Zip file is a single file containing one or more compressed files.
Uses for Zip File?
zipfile Module
Explore all the methods and classes of the zipfile module using dir() method. See the code to get all the classes and methods of the zipfile module.
You have seen a bunch of classes and methods right. But, you are not going to learn all of them. You will learn only some classes and methods to work with the Zip files.
Let's see some useful Exceptions, Classes, and Methods with brief explanations.
Exceptions
Exception is a message which is used to display the exact error as you like. In Python, you use try, except, finally keywords for the error handling.
If you are not familiar with the error handling, go to Pythons Error Handling documentation to learn the error handling.
Let's see all exceptions in zipfile module.
zipfile.BadZipFile
zipfile.BadZipFile is an exception in the zipfile module. This error will raise for Bad Zip files. See the example below.
zipfile.LargeZipFile
Suppose if you want to work with a large Zip file, you need to enable the ZIP64 functionality while opening the Zip. If you don't enable it, LargeZipFile will raise. See the example.
Choose a Zip file which best suits for the Exception handling and then tries to run the program. You will get a clear Idea.
Classes
In simple words, class is a set of methods and attributes. You use the class methods and attributes wherever you want by creating the instances of class.
Let's see some classes of the zipfile module.
zipfile.ZipFile
The most common class which is used to work with Zip Files is ZipFile class.
zipfile.ZipFile is used to write and read the Zip files. It has some methods which are used to handle the Zip files.
Now, explore the methods of the ZipFile class using the dir() objects. See the code.
You've already used the zipfile.ZipFile class to read Zip files in previous examples.
zipfile.ZipFile contains many methods like extract, open getinfo, setpassword, etc., to work the Zip files.
Let's see some methods of the ZipFile class.
If you want to learn all of the methods of ZipFile class use the help() function on the method you want to learn.
Or go to the official documentation of Python to learn.
zipfile.ZipInfo
zipfile.ZipInfo class used to represent the member of a Zip folder.
First, explore all the objects of zipfile.ZipInfo class using dir() method. See the code below.
Now, you are going to see some methods of the class zipfile.ZipInfo
Go to ZipInfo, if you want to learn more about the objects of ZipInfo.
Methods
Methods are a block of code for a specific functionality in the program. For example, if you want to find the absolute value of a number, you can use the Pythons methods called abs.
You can use it anywhere you want. Let's see some methods of the zipfile module.
zipfile.is_zipfile()
is_zipfile(filename) method of zipfile module returns True if the file is a valid Zip otherwise it returns False.
Let's see an example.
Handling Zip Files
In this section, you will learn how to handle Zip files like Opening, Extracting, Writing, etc..,.
Extracting a Zip File
Extracting the files of a Zip file using the extractall method to the current directory.
Extracting a Zip with Password
To extract a Zip with a password, you need to pass a value to pwd positional argument of extract(pwd = password) or extractall(pwd = password) methods.
You must pass a password which is in bytes. To convert a str to bytes use the Pythons built-in method bytes with utf-8 encode format.
Let's see an example.
You can also extract files by using the setpassword(pwd = password) method of ZipFile class. See the example below.
Creating Zip Files
To create a Zip file, you don't need any extra methods. Just pass the name to the ZipFile class, and it will create an archive in the current directory.
See the below example.
Writing to Zip Files
You have to open Zip files in write mode to write files to the archive file. It overrides all the existing files in the Zip.
Let's an example.
Appending Files to Zip
You have to open Zip in append(a) mode in order to append any files to the Zip. It doesn't override the existing files.
Let's see an example.
Till now, you have learned how to handle Zip files. Now, you will able to open, write, append, extract, create, etc.., Zip files. Now, you're going to write a simple program.
Let's see what it is?
Extracting Multiple Sub Zips with Password Using Loops and zipfile
Steps To Solve The Problem
Run the loop for Infinite Times.
Check whether the file is Zip or not using is_zipfile() method. If yes do the following.
Open the zip with the name variable.
Get the password of the Zip from the name variable.
Extract the Zip.
Get and store the next Zip name in the name variable.
else
I have created the above Procedure. If you want you can change it according to your files arrangement.
After executing the above program, you'll see all the sub zips are extracted to the current directory.
End Note
Congratulations on completing the tutorial!
Hope you enjoyed the tutorial. This article helps you a lot when you are working with Zip files. Now, you are able to work with the Zip files.
If you have any doubts regarding the article, ask me in the comment section. I will reply as soon as possible.
Again, if you are new to Python take DataCamp's free Intro to Python for Data Science course to learn Python language or read Pythons official documentation.
Happy Coding!