Zipping is an elegant way of compressing several files and combining them into a single one.

This saves not only storage space but also bandwidth when transferring a file over a web connection.

Zip uses lossless compression so that it retains all data when compressing and uncompressing it.

Different file types and their extensions

you’re able to also encrypt and decrypt your zip files.

What Is the zipfile Module?

you’re free to use the standard zipfile module to manipulate zip files using Python.

There are three methods of compression available: Deflate, Bzip2, and LZMA.

It can also decrypt zip archives but not encrypt them.

Import theZipFilemodule from thezipfilepackage to perform the required zip operations.

Also importtheosmodule to select and save files.

Define a functionget_all_file_paths().

It should accept a directory parameter that contains the path to the folder containing the files to zip.

Initialize an empty list that will store the list of file paths.

Use a for loop to get the full contents of the target directory using theos.walk()function.

For each directory, the function returns a three-tuple containing root, dirs, and files.

The root is a string that contains the full path to the directory.

The dirs list includes all subdirectories contained in the directory.

The files list contains all files present in the directory.

Inside the loop, declare anotherloop to iterate over the file list.

Then, append this complete filename to the list you created earlier.

Once this operation finishes, return the list of file paths.

Call the function,get_all_file_paths()that you created earlier and store the value it returns infile_paths.

Iterate over the files and display their names.

Iterate over each file and use the write() function over the writable file to a zip file.

When the operation is complete, output a message explaining that the program zipped all the files successfully.

Import thezipfilepackage from theZipFilemodule and store the name of the zip file you want to extract in a variable.

Use theprintdir()function to display the table of contents of files present in the zip.

Display a message using theprintstatement on the completion of the operation.

Popular websites such as GitHub and Google Drive let you download a collection of files as a zip archive.

The zip program compresses a set of files into a single package.

This is in contrast to a tool like tar which packages files together but does not compress them.

RAR uses the AES-128 algorithm that’s better than the deflate algorithm used by zip files.

Ultimately, choose the one that best meets your need.