Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Create Password Protected Zip of a file using Python

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

ZIP is an archive file format that supports lossless data compression. By lossless compression, we mean that the compression algorithm allows the original data to be perfectly reconstructed from the compressed data. So, a ZIP file is a single file containing one or more compressed files, offering an ideal way to make large files smaller and keep related files together.

In this article, we will learn how to Create Password-Protected Zip of a file using Python. For this, we are using pyminizip module from python.

Installation:

 The pyminizip module can be installed using the below command:

pip install pyminizip

For creating zip, we are using compress() method from pyminizip. So, we discuss first its syntax and arguments.

Syntax:

pyminizip.compress(“/srcfile/path.txt”, “file_path_prefix”, “/distfile/path.zip”, “password”, int(compress_level))

Arguments:

  • src file path (string)
  • src file prefix path (string) or None (path to prepend to file)
  • dst file path (string)
  • password (string) or None (to create no-password zip)
  • compress_level(int) between 1 to 9, 1 (more fast) <—> 9 (more compress) or 0 (default)
     

Return value: Always returns None

Implementation:

Input file:

Program:

Python3




# importing module
import pyminizip
  
# input file path
inpt = "./Text.txt"
  
# prefix path
pre = None
  
# output zip file path
oupt = "./output.zip"
  
# set password value
password = "GFG"
  
# compress level
com_lvl = 5
  
# compressing file
pyminizip.compress(inpt, None, oupt,
                   password, com_lvl)

Output:

My Personal Notes arrow_drop_up
Last Updated : 24 Jan, 2021
Like Article
Save Article
Similar Reads
Related Tutorials