Python Zip File Example – Working With Zip Files In Python

Hey Everyone, welcome to Python Zip File Example Tutorial. In this tutorial you will learn what is zip file, why we use them, how to create a zip file, how to write files to a zip file and many more.

Python Zip File Example – An Introduction To Zip Files

What Is Zip File ?

  • A file with the zip file extension is called zip file.
  • ZIP is an archive file format that supports lossless data compression. A ZIP file may contain one or more files or directories that may have been compressed.
  • The ZIP file format permits a number of compression algorithms, though DEFLATE is the most common.
  • A ZIP file, like other archive file formats, is simply a collection of one or more files and/or folders but is compressed into a single file for easy transportation and compression.

Why We Use Zip Files ?

These are the most common use of Zip files –

Downloading softwares

  • The most common use for ZIP files is for software downloads.
  • Zipping a software program saves storage space on the server, decreases the time it takes for you to download it to your computer, and keeps the hundreds or thousands of files nicely organized in the single ZIP file.

Downloading And Sharing Photos

  • Instead of sending each image individually over email or saving each image one by one from a website, the sender can put the files in a ZIP archive so that only one file needs to be transferred.

Python Zip File Example – Creating, Writing, Reading

So guys in this section you will see how to create and read/write to zip files in python. Let’s see how to do that.

Creating New Project

So now open your python IDE(check best IDEs), and create a new project and inside this project create a new python file.

Python Zip File Example
Python Zip File Example

zipfile Module

The first thing you need to work with zip files in python is zipfile module. This module provides tools to create, read, write, append, and list a ZIP file.

This module does not currently handle multi-disk ZIP files. It can handle ZIP files that use the ZIP64 extensions (ZIP files that are more than 4 GByte in size).

It supports decryption of encrypted files in ZIP archives, but it currently cannot create an encrypted file.

Decryption is extremely slow as it is implemented in native Python rather than C.

Zipfile is an inbuilt python module so you don’t need to install it via pip install.

shutil Module

  • shutil is the second module, by using this you can work with zip files using python.
  • This module allows for quickly compressing an entire directory into a zip archive.

But in this tutorial we will work only with zipfile module.

Creating Zip File

So now you will learn how to create a zip file using python. So write the following code.

Code Explanation

  • The first thing you need to do is importing zipfile module.
  • ZipFile is a class which is used for reading and writing zip files.
  • zipfile.ZipFile will create a zip file and inside this class you have to pass name of zip file that you want to create with full path and then mode and compression.
  • In mode, you have to pass w because you are writing a new file.
  • compression is the ZIP compression method to use when writing the archive. ZIP_DEFLATED is the numeric constant for the usual ZIP compression method. This requires the zlib module.
  • Now you have to close the zip file. You must call close() before exiting your program or essential records will not be written.

Now run the above code. You will see your zipfile has been created. Let’s see my zip file named NewZipfile.

Python Zip File Example
Python Zip File Example

Writing To Zip File

Now you need to write into the zip file which you have created. So for this write the following code.

  • write command is used to write files into zip files.
  • It overrides all the existing files in the Zip.
  • Now you have to create file which you want to write to the zip file.
  • Now run the program and let’s see what happens.
Python Zip File Example
Python Zip File Example
  • You can see two text files has been written into zip file.

Reading Zip Files

Now to read zip files, you have to open your file in reading(r) mode.

  • read command is used for reading zip files.

Now write the following code for reading zip file.

Now run the above code and check the output.

Python Zip File Example
Python Zip File Example

Let’s learn An Introduction To MongoDB With Python

Python Zip File Example – Extracting Zip Files

You can extract zip file using two ways –

  • extract( )
  • extractall( )

extract( )

Syntax

  • It extract a member from the archive to the current working directory.
  • member must be its full name or a ZipInfo object.
  • path specifies a different directory to extract to.
  • pwd is the password used for encrypted files.

Let’s see how to implement it. So write the following code.

And the output of this code is following.

Python Zip File Example
Python Zip File Example

Now you can see in your project directory that your file is extracted.

Python Zip File Example
Python Zip File Example

extractall( )

  • It extract all members from the archive to the current working directory.

So write the following code to implement this.

Now let’s run this.

Python Zip File Example
Python Zip File Example

Go to your project directory and check the extracted files. In my case it is as follows –

Python Zip File Example
Python Zip File Example

Have You Checked Speech Recognition Python Tutorial ?

Extracting  Zip File With Password

So now, to extract  a zip file with password, you have to pass the value to pwd which is positional argument of extract(pwd = password) or extractall(pwd = password) methods. So let’s see it with an example.

  • Remember one thing, You must have to  pass a password which is in bytes. Built-in method bytes with utf-8 encode format is used to convert a str to bytes.

Now run the code and check your project directory.

Python Zip File Example
Python Zip File Example

You can see the locked file is extracted.

Conclusion

So guys this was all about Python Zip File Example. In this tutorial, you have see working with zip files in python. I hope, you have learned a lot from this tutorial, if so then must share with others and help them learning python. And if you have query regarding this post then feel free to ask me in comment section. Your queries are most welcome. Thanks

Leave a Comment