Password protect zip

Share your advanced PureBasic knowledge/code with the community.
drgolf
Enthusiast
Enthusiast
Posts: 119
Joined: Tue Mar 03, 2009 3:40 pm
Location: france

Password protect zip

Post by drgolf »

Hello,

For create zip files with protected content :

Code: Select all

; PB6.30 B3 x64 - Windows 11 Pro
; DrGolf - Oct 2025
; Creation zip file with password protect
; Files needed : libzip.dll and libzip.dll.a
; Here : https://www.freebasic-portal.de/downloads/bibliotheken/libzip-For-freebasic-417.html
;
;;;;;;;;;;;;;;;;;;;;;;;
;#ZIP_EM_AES_128= $0101 ;'/* Winzip AES encryption */
;#ZIP_EM_AES_192= $0102
#ZIP_EM_AES_256= $0103

#ZIP_CREATE = 1
#ZIP_FL_OVERWRITE = $8192
;
ImportC "libzip.dll.a"
      zip_open(fname.p-utf8, mode.i, fl.i) 
      zip_source_buffer(hzip.i, *buffer, buflen.i, fl.i)
      zip_close(hzip.i) 
      zip_file_add(hzip.i, fname.p-utf8, *zip_source, zip_flags.i)
      zip_file_set_encryption(hzip.i, index.i , method.i , password.p-utf8)
EndImport
    
;    
   
    hzip=zip_open(GetPathPart(ProgramFilename())+"test1.zip",#zip_create,0)
    s$="aaaaaaaaaaaaaaaaaaaaaaaaaaaa"
    pz=zip_source_buffer(hzip,@s$,Len(s$),0)
    zip_file_add(hzip,"text1.txt",pz,#ZIP_FL_OVERWRITE)
    zip_file_set_encryption(hzip,0,#ZIP_EM_AES_256,"password")
    zip_close(hzip)
    

For multi files in zip use zip_file_add...

Hope this can help...