0

I have a folder Structure like this :

MainFolder
    |
   project.xml 
   SubFolder 1
      |
      Files
        |  
        wallpaper.png
        imp.docx
        pdf_1.pdf
    SubFolder 2
        |
        CategoryFolder
          |  
          Files
             |
             something.txt 

So the above is my folder structure. I have to create a zipFile to the "MainFolder" with password and zipFile should contain all the files and folders as i have shown above. I was able to create zipFile without password but the requirement is zipFile with password. So how can i Achieve this. pls help

2 Answers 2

1

According to the documentation, the python zip library only uses password for extracting zip file (and not for creating them):

pwd is the password used to decrypt encrypted ZIP files.

There are some other packages that claim to allow that. Here's a piece of code that zips all the files in a folder named 'main':

from pathlib import Path
import pyminizip

# replace 'main' is your main folder. 
files = [f for f in Path("main").rglob("*") if f.is_file()]

paths = [str(f.parent) for f in files]
file_names = [str(f) for f in files]

# 123456 is the password. 3 is the compression level. 
pyminizip.compress_multiple(file_names, paths, "my.zip", "123456", 3)

The alternative solution would be to call the zip utility as a separate process using system, or preferably Popen.

2
  • I used pyminizip for creating zipfile but it takes only source file ex: .txt but in my case its whole directory. So i got stuck here. Commented Jun 10, 2020 at 20:25
  • Thank u for the answer. Actually i used pyminizip package to do zip with password. Created a zipfile using "shutil" then used "pyminizip" for the rest. Commented Jun 10, 2020 at 21:17
1

You can look in some packages as pyzipper. According to documentation, this is

Modification of Python’s zipfile to read and write AES encrypted zip files.

Source

Not the answer you're looking for? Browse other questions tagged or ask your own question.