@@ -13,21 +13,47 @@ def __init__(self, ziptoolpath:str, zipfilepath:str):
1313 self .using7z = True
1414 if len (self .ziptoolpath )== 0 :
1515 self .using7z = False
16- def getzipfile (self ):
17- if self .using7z :
18- with open ('blankfile' ,'w' ) as f :
16+ def setpassword (self ,password :str ):
17+ self .password = password
18+ def getzipfile (self ):#create a blank zip file
19+ if self .using7z : #using outside 7z.exe
20+ with open ('blankfile' ,'w' ) as f : #create a blankfile to compress
1921 f .write ('\n ' )
20- command = ' a ' + self .zipfilepath + ' ' + os .getcwd ()+ '\\ blankfile -p' + ZIP_PASS
22+ command = ' a ' + self .zipfilepath + ' ' + os .getcwd ()+ '\\ blankfile'
23+ if len (self .password )!= 0 :
24+ command += ' -p' + self .password
2125 print (self .ziptoolpath + command )
2226 code = os .system (self .ziptoolpath + command )
2327 return code
28+ else :#using zipfile pack, with no password
29+ try :
30+ azip = zipfile .ZipFile (self .zipfilepath , 'w' )
31+ azip .write ('blankfile' , compress_type = zipfile .ZIP_LZMA )
32+ azip .close ()
33+ return 0
34+ except Exception as e :
35+ azip .close ()
36+ return str (e )
2437 def genzipfile (self ,folder_path :str ):
2538 if self .using7z :
2639 command = " a -t7z -scsUTF-8 " + self .zipfilepath + " " + folder_path + "\\ * -p" + ZIP_PASS
2740 print (self .ziptoolpath + command )
2841 code = os .system (self .ziptoolpath + command )
2942 return code
30-
43+ else :
44+ try :
45+ azip = zipfile .ZipFile (self .zipfilepath , 'w' )
46+ for current_path , subfolders , filesname in os .walk (folder_path ):
47+ print (current_path , subfolders , filesname )
48+ # filesname是一个列表,我们需要里面的每个文件名和当前路径组合
49+ for file in filesname :
50+ # 将当前路径与当前路径下的文件名组合,就是当前文件的绝对路径
51+ azip .write (os .path .join (current_path , file ))
52+ azip .close ()
53+ return 0
54+ except Exception as e :
55+ azip .close ()
56+ return str (e )
3157
3258def load_serval (str_serval :str ): #load a serval file
3359 lines = str_serval .split ('\n ' )
0 commit comments