How to Create an Encrypted Zip File

zipfile: Adding files to encrypted zip

After all of the lovely replies, I did find a workaround for this just in case someone needs the answer!

I did first retry the z.testzip() and it does actually catch the bad passwords, but after seeing that it wasn't reliable (apparently hash collisions that allow for bad passwords to somehow match a small hash), I decided to use the password, extract the first file it sees in the archive, and then extract it. If it works, remove the extracted file, and if it doesn't, no harm done.

Code works as below:

try:
z = zipfile.ZipFile(fileName, 'a') #Set zipfile object
zipPass = bytes(zipPass, encoding='utf-8') #Str to Bytes
z.setpassword(zipPass) #Set password
filesInArray = z.namelist() #Get all files
testfile = filesInArray[0] #First archive in list
z.extract(testfile, pwd=zipPass) #Extract first file
os.remove(testfile) #remove file if successfully extracted
except Exception as e:
print("Exception occurred: ",repr(e))
return None #Return to mainGUI - this exits the function without further processing

Thank you guys for the comments and answers!

How to create a password protected ZIP file in Windows using C?

You can use minizip library on Windows. It can be compiled via CMake.

This is how you can use it to create a password-protected zip file:

#include "mz.h"
#include "mz_os.h"
#include "mz_strm.h"
#include "mz_strm_buf.h"
#include "mz_strm_split.h"
#include "mz_zip.h"
#include "mz_zip_rw.h"
#include <stdio.h>

int main() {
void *writer = NULL;
int32_t err = MZ_OK;

mz_zip_writer_create(&writer);
mz_zip_writer_set_password(writer, "mypassword");
mz_zip_writer_set_compress_method(writer, MZ_COMPRESS_METHOD_DEFLATE);
mz_zip_writer_set_compress_level(writer, MZ_COMPRESS_LEVEL_DEFAULT);
err = mz_zip_writer_open_file(writer, "output.zip", 0, 0);
if (err != MZ_OK) {
printf("Could not open zip file for writing.");
return 1;
}
err = mz_zip_writer_add_path(writer, "text1.txt", NULL, 0, 0);
if (err != MZ_OK) {
printf("Failed to add file to the archive.");
}
mz_zip_writer_close(writer);
mz_zip_writer_delete(&writer);
return 0;
}

This code will create a output.zip file with a password-protected file text1.txt.

For more information and uses refer to the minizip.c sample file.

I hope it helps.

Write in-memory data to an encrypted ZIP or 7Z (without using an on-disk temp file)

7z.exe has the -si flag, which lets it read data for a file from stdin. This way you could still use 7z's commandline from a subprocess even without an extra file:

from subprocess import Popen, PIPE

# inputs
szip_exe = r"C:\Program Files\7-Zip\7z.exe" # ... get from registry maybe
memfiles = {"data.dat" : b'ab' * 1000000000}
arch_filename = "myarchive.zip"
arch_password = "Swordfish"

for filename, data in memfiles.items():
args = [szip_exe, "a", "-mem=AES256", "-y", "-p{}".format(arch_password),
"-si{}".format(filename), output_filename]
proc = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
proc.stdin.write(data)
proc.stdin.close() # causes 7z to terminate
# proc.stdin.flush() # instead of close() when on Mac, see comments
proc.communicate() # wait till it actually has

The write() takes somewhat above 40 seconds on my machine, which is quite a lot. I can't say though if that's due to any inefficiencies from piping the whole data through stdin or if it's just how long compressing and encrypting a 2GB file takes. EDIT: Packing the file from HDD took 47 seconds on my machine, which speaks for the latter.



Related Topics



Leave a reply



Submit