Skip to content
Related Articles

Related Articles

Improve Article

Create Password Protected Zip of a file using Python

  • Last Updated : 24 Jan, 2021

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:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :