-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path7Zip.pbi
371 lines (322 loc) · 9.23 KB
/
7Zip.pbi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
; +--------------+
; | 7-Zip Helper |
; +--------------+
; | 2015.10.13 . Added Add7ZipFile and password/flags
; | 2017.01.06 . Added Examine Files/Folders, Extract File
; | .03.16 . Made file multiple-include safe, rewrote demo
; | .04.06 . Delete temporary unzip folder
;-
CompilerIf (Not Defined(__7Zip_Included, #PB_Constant))
#__7Zip_Included = #True
CompilerIf (#PB_Compiler_IsMainFile)
EnableExplicit
CompilerEndIf
;- Constants (Public)
; Can't start a constant's name with a number, like #7Zip_
#SevenZip_IncludeVersion = 20170316
Enumeration ; 7Zip Flags
#SevenZip_EncryptNames = $01
EndEnumeration
;-
;- Structures (Private)
Structure __7ZIPSTRUCT
Init.i
Executable.s
ExitCode.i
Output.s
VersionS.s
VersionI.i
Password.s
Flags.i
;
nFiles.i
FileList.s
nFolders.i
FolderList.s
EndStructure
;-
;- Globals (Private)
Global __7Zip.__7ZIPSTRUCT
;-
;- Procedures (Private)
Procedure.i __7Zip_Run(Executable.s, Parameter.s = "", Directory.s = "")
Protected PID.i = #Null
With __7Zip
\ExitCode = 0
\Output = ""
PID = RunProgram(Executable, Parameter, Directory, #PB_Program_Hide | #PB_Program_Open | #PB_Program_Read)
If (PID)
While (ProgramRunning(PID))
While (AvailableProgramOutput(PID))
CompilerIf (#PB_Compiler_Version < 540)
\Output + ReadProgramString(PID, #PB_Unicode) + #LF$
CompilerElse
\Output + ReadProgramString(PID, #PB_UTF8) + #LF$
CompilerEndIf
Wend
Delay(1)
Wend
\ExitCode = ProgramExitCode(PID)
CloseProgram(PID)
EndIf
EndWith
ProcedureReturn (Bool(PID))
EndProcedure
;-
;- Procedures (Public)
Procedure Reset7Zip()
With __7Zip
\Password = ""
\Flags = #Null
EndWith
EndProcedure
Procedure Set7ZipPassword(Password.s)
With __7Zip
\Password = Password
EndWith
EndProcedure
Procedure Set7ZipFlags(Flags.i)
With __7Zip
\Flags = Flags
EndWith
EndProcedure
Procedure.i Init7Zip(Executable.s = "")
With __7Zip
If (Not \Init)
If (Executable = "")
Executable = GetCurrentDirectory() + "7za.exe"
EndIf
If (__7Zip_Run(Executable))
Protected i.i = FindString(\Output, "7-Zip (A) ", 1, #PB_String_NoCase)
If (i)
\Executable = Executable
\VersionS = StringField(Trim(Mid(\Output, i + 10)), 1, " ")
\VersionI = Round(100.0 * ValF(\VersionS), #PB_Round_Nearest)
Reset7Zip()
\Init = #True
EndIf
EndIf
EndIf
ProcedureReturn (\Init)
EndWith
EndProcedure
Procedure.s Get7ZipVersion()
With __7Zip
If (\Init)
ProcedureReturn (\VersionS)
Else
ProcedureReturn ("")
EndIf
EndWith
EndProcedure
Procedure.i Get7ZipBuildNumber()
With __7Zip
If (\Init)
ProcedureReturn (\VersionI)
Else
ProcedureReturn (0)
EndIf
EndWith
EndProcedure
Procedure.i Add7ZipFile(Archive.s, File.s)
Protected Result.i = #False
With __7Zip
If (\Init)
If (FileSize(File) >= 0)
Protected Param.s = "a"
Param + " " + #DQUOTE$ + Archive + #DQUOTE$
Param + " " + #DQUOTE$ + File + #DQUOTE$
If (\Password)
Param + " -p" + \Password
If (\Flags & #SevenZip_EncryptNames)
Param + " -mhe"
EndIf
EndIf
If (__7Zip_Run(\Executable, Param, GetPathPart(Archive)))
If (\ExitCode = 0)
Result = #True
EndIf
EndIf
EndIf
EndIf
EndWith
ProcedureReturn (Result)
EndProcedure
Procedure.i Extract7ZipFile(Archive.s, File.s, Destination.s = "")
Protected Result.i = #False
With __7Zip
If (\Init)
If ((FileSize(Archive) >= 0) And (File))
If (GetPathPart(Archive) = "")
Archive = GetCurrentDirectory() + Archive
EndIf
If (Destination)
If (GetPathPart(Destination) = "")
Destination = GetPathPart(Archive) + Destination
EndIf
Else
Destination = GetPathPart(Archive)
EndIf
If (FileSize(Destination) = -2)
Destination = RTrim(Destination, "\") + "\" + File
EndIf
Protected TempDir.s = GetTemporaryDirectory() + "7ZPB" + "\"
CreateDirectory(TempDir)
Protected TempFile.s = TempDir + GetFilePart(File)
DeleteFile(TempFile)
Protected Param.s = "e"
Param + " " + #DQUOTE$ + Archive + #DQUOTE$
Param + " -o" + #DQUOTE$ + TempDir + #DQUOTE$
Param + " " + #DQUOTE$ + File + #DQUOTE$
Param + " -y"
If (\Password)
Param + " -p" + \Password
If (\Flags & #SevenZip_EncryptNames)
Param + " -mhe"
EndIf
EndIf
If (__7Zip_Run(\Executable, Param, GetPathPart(Archive)))
If (\ExitCode = 0)
CreateDirectory(GetPathPart(Destination))
DeleteFile(Destination)
If (RenameFile(TempFile, Destination))
Result = #True
Else
DeleteFile(TempFile)
EndIf
EndIf
EndIf
DeleteDirectory(TempDir, "")
EndIf
EndIf
EndWith
ProcedureReturn (Result)
EndProcedure
Procedure.i Examine7ZipFiles(Archive.s)
Protected Result.i = 0
With __7Zip
If (\Init)
If (FileSize(Archive) >= 0)
Protected Param.s = "l"
Param + " " + #DQUOTE$ + Archive + #DQUOTE$
If (\Password)
Param + " -p" + \Password
EndIf
If (__7Zip_Run(\Executable, Param, GetPathPart(Archive)))
If (\ExitCode = 0)
Protected Lines.i = 1 + CountString(\Output, #LF$)
\nFiles = 0
\FileList = ""
\nFolders = 0
\FolderList = ""
Protected i.i
Protected InList.i = #False
Protected NameOffset.i
For i = 1 To Lines
Protected Line.s = StringField(\Output, i, #LF$)
If (InList)
If (Left(Line, 19) = "-------------------")
Break
Else
If (Mid(Line, 21, 1) = "D")
\FolderList + #LF$ + Mid(Line, NameOffset)
\nFolders + 1
Else
\FileList + #LF$ + Mid(Line, NameOffset)
\nFiles + 1
EndIf
EndIf
Else
If Right(Line, 6) = " Name"
NameOffset = Len(Line) - 4 + 1
ElseIf (Left(Line, 19) = "-------------------")
InList = #True
EndIf
EndIf
Next i
\FileList = Mid(\FileList, 2)
\FolderList = Mid(\FolderList, 2)
Result = #True
EndIf
EndIf
EndIf
EndIf
EndWith
ProcedureReturn (Result)
EndProcedure
Procedure.i Get7ZipFileCount()
ProcedureReturn (__7Zip\nFiles)
EndProcedure
Procedure.s Get7ZipFileList()
ProcedureReturn (__7Zip\FileList)
EndProcedure
Procedure.i Get7ZipFolderCount()
ProcedureReturn (__7Zip\nFolders)
EndProcedure
Procedure.s Get7ZipFolderList()
ProcedureReturn (__7Zip\FolderList)
EndProcedure
;-
;- Demo Program
CompilerIf (#PB_Compiler_IsMainFile)
DisableExplicit
If (Not Init7Zip())
File.s = OpenFileRequester("7-Zip Commandline", GetHomeDirectory() + "7za.exe", "Executables (*.exe)|*.exe", 0)
If (File)
Init7Zip(File)
Else
End
EndIf
EndIf
If (Init7Zip())
Debug "Initialized..."
Debug "Creating dummy files..."
CreateDirectory(GetTemporaryDirectory() + "7Zip_Demo")
SetCurrentDirectory(GetTemporaryDirectory() + "7Zip_Demo")
DeleteFile("simple.7z")
DeleteFile("password.7z")
DeleteFile("encrypted.7z")
If CreateFile(0, "text.txt")
WriteString(0, "Hello World!!!")
CloseFile(0)
EndIf
If CreateFile(0, "chars.bin")
For i = 0 To 255
WriteAsciiCharacter(0, i)
Next i
CloseFile(0)
EndIf
Debug "Creating simple archive..."
Add7ZipFile("simple.7z", "text.txt")
Add7ZipFile("simple.7z", "chars.bin")
Debug "Creating archive with password..."
Set7ZipPassword("pWord")
Add7ZipFile("password.7z", "text.txt")
Add7ZipFile("password.7z", "chars.bin")
Debug "Creating archive with encrypted names..."
Set7ZipFlags(#SevenZip_EncryptNames)
Add7ZipFile("encrypted.7z", "text.txt")
Add7ZipFile("encrypted.7z", "chars.bin")
Debug "Examining encrypted archive..."
Set7ZipPassword("pWord")
If (Examine7ZipFiles("encrypted.7z"))
CreateDirectory("Extracted")
n = Get7ZipFileCount()
FileList.s = Get7ZipFileList()
For i = 1 To n
File.s = StringField(FileList, i, #LF$)
Debug "Extracting '" + File + "'..."
Extract7ZipFile("encrypted.7z", File, "Extracted\" + File)
Next i
EndIf
Reset7Zip()
DeleteFile("text.txt")
DeleteFile("chars.bin")
Debug "Done"
RunProgram(GetTemporaryDirectory() + "7Zip_Demo")
Else
Debug "7-Zip could not be initialized"
EndIf
CompilerEndIf
CompilerEndIf
;-