Debian Codegraph @

plugins/wcx/sevenzip/src/SevenZipOpt.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 SevenZip archiver plugin, compression options
5
6 Copyright (C) 2014-2017 Alexander Koblov ([email protected])
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 }
22
23 unit SevenZipOpt;
24
25 {$mode delphi}
26
27 interface
28
29 uses
30 Classes, SysUtils, Windows, IniFiles, JclCompression, SevenZip;
31
32 const
33 cKilo = 1024;
34 cMega = cKilo * cKilo;
35 cGiga = cKilo * cKilo * cKilo;
36
37 const
38 kNoSolidBlockSize = 0;
39 kSolidBlockSize = 64;
40
41 const
42 DeflateDict: array[0..0] of PtrInt =
43 (
44 cKilo * 32
45 );
46
47 Deflate64Dict: array[0..0] of PtrInt =
48 (
49 cKilo * 64
50 );
51
52 Bzip2Dict: array[0..8] of PtrInt =
53 (
54 cKilo * 100,
55 cKilo * 200,
56 cKilo * 300,
57 cKilo * 400,
58 cKilo * 500,
59 cKilo * 600,
60 cKilo * 700,
61 cKilo * 800,
62 cKilo * 900
63 );
64
65 LZMADict: array[0..21] of PtrInt =
66 (
67 cKilo * 64,
68 cMega,
69 cMega * 2,
70 cMega * 3,
71 cMega * 4,
72 cMega * 6,
73 cMega * 8,
74 cMega * 12,
75 cMega * 16,
76 cMega * 24,
77 cMega * 32,
78 cMega * 48,
79 cMega * 64,
80 cMega * 96,
81 cMega * 128,
82 cMega * 192,
83 cMega * 256,
84 cMega * 384,
85 cMega * 512,
86 cMega * 768,
87 cMega * 1024,
88 cMega * 1536
89 );
90
91 PPMdDict: array[0..19] of PtrInt =
92 (
93 cMega,
94 cMega * 2,
95 cMega * 3,
96 cMega * 4,
97 cMega * 6,
98 cMega * 8,
99 cMega * 12,
100 cMega * 16,
101 cMega * 24,
102 cMega * 32,
103 cMega * 48,
104 cMega * 64,
105 cMega * 96,
106 cMega * 128,
107 cMega * 192,
108 cMega * 256,
109 cMega * 384,
110 cMega * 512,
111 cMega * 768,
112 cMega * 1024
113 );
114
115 DeflateWordSize: array[0..11] of PtrInt =
116 (8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 258);
117
118 Deflate64WordSize: array[0..11] of PtrInt =
119 (8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 257);
120
121 LZMAWordSize: array[0..11] of PtrInt =
122 (8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 273);
123
124 PPMdWordSize: array[0..14] of PtrInt =
125 (2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16);
126
127 // Stored as block size / 1024
128 SolidBlock: array[0..16] of PtrInt =
129 (
130 cKilo,
131 cKilo * 2,
132 cKilo * 4,
133 cKilo * 8,
134 cKilo * 16,
135 cKilo * 32,
136 cKilo * 64,
137 cKilo * 128,
138 cKilo * 256,
139 cKilo * 512,
140 cMega,
141 cMega * 2,
142 cMega * 4,
143 cMega * 8,
144 cMega * 16,
145 cMega * 32,
146 cMega * 64
147 );
148
149 const
150 TargetCPU = {$I %FPCTARGETCPU%}; // Target CPU of FPC
151
152 type
153
154 TCompressionLevel =
155 (
156 clStore = 0,
157 clFastest = 1,
158 clFast = 3,
159 clNormal = 5,
160 clMaximum = 7,
161 clUltra = 9
162 );
163
164 TArchiveFormat = (afSevenZip, afBzip2, afGzip, afTar, afWim, afXz, afZip);
165
166 PPasswordData = ^TPasswordData;
167 TPasswordData = record
168 EncryptHeader: Boolean;
169 Password: array[0..MAX_PATH] of WideChar;
170 end;
171
172 TFormatOptions = record
173 Level: PtrInt;
174 Method: PtrInt;
175 Dictionary: PtrInt;
176 WordSize: PtrInt;
177 SolidSize: PtrInt;
178 ThreadCount: PtrInt;
179 ArchiveCLSID: PGUID;
180 Parameters: WideString;
181 end;
182
183 function GetNumberOfProcessors: LongWord;
184 function FormatFileSize(ASize: Int64; AGiga: Boolean = True): String;
185
186 procedure SetArchiveOptions(AJclArchive: IInterface);
187
188 procedure LoadConfiguration;
189 procedure SaveConfiguration;
190
191 const
192 DefaultIniName = 'sevenzip.ini';
193
194 var
195 ConfigFile: AnsiString;
196 LibraryPath: AnsiString;
197
198 const
199 ArchiveExtension: array[TArchiveFormat] of WideString =
200 ('7z', 'bzip2', 'gzip', 'tar', 'wim', 'xz', 'zip');
201
202 const
203 MethodName: array [TJclCompressionMethod] of WideString =
204 (kCopyMethodName, kDeflateMethodName, kDeflate64MethodName,
205 kBZip2MethodName, kLZMAMethodName, kLZMA2MethodName, kPPMdMethodName);
206
207 const
208 DefaultConfig: array[TArchiveFormat] of TFormatOptions =
209 (
210 (Level: PtrInt(clNormal); Method: PtrInt(cmLZMA2); Dictionary: cMega * 16; WordSize: 32; SolidSize: cMega * 2; ThreadCount: 2; ArchiveCLSID: @CLSID_CFormat7z; Parameters: '';),
211 (Level: PtrInt(clNormal); Method: PtrInt(cmBZip2); Dictionary: cKilo * 900; WordSize: 0; SolidSize: 0; ThreadCount: 2; ArchiveCLSID: @CLSID_CFormatBZ2; Parameters: '';),
212 (Level: PtrInt(clNormal); Method: PtrInt(cmDeflate); Dictionary: cKilo * 32; WordSize: 32; SolidSize: 0; ThreadCount: 1; ArchiveCLSID: @CLSID_CFormatGZip; Parameters: '';),
213 (Level: PtrInt(clStore); Method: 0; Dictionary: 0; WordSize: 0; SolidSize: 0; ThreadCount: 1; ArchiveCLSID: @CLSID_CFormatTar; Parameters: '';),
214 (Level: PtrInt(clStore); Method: 0; Dictionary: 0; WordSize: 0; SolidSize: 0; ThreadCount: 1; ArchiveCLSID: @CLSID_CFormatWim; Parameters: '';),
215 (Level: PtrInt(clNormal); Method: PtrInt(cmLZMA2); Dictionary: cMega * 16; WordSize: 32; SolidSize: 0; ThreadCount: 2; ArchiveCLSID: @CLSID_CFormatXz; Parameters: '';),
216 (Level: PtrInt(clNormal); Method: PtrInt(cmDeflate); Dictionary: cKilo * 32; WordSize: 32; SolidSize: 0; ThreadCount: 2; ArchiveCLSID: @CLSID_CFormatZip; Parameters: '';)
217 );
218
219 var
220 PluginConfig: array[TArchiveFormat] of TFormatOptions;
221
222 implementation
223
224 uses
225 ActiveX, LazUTF8, SevenZipAdv, SevenZipCodecs;
226
227 function GetNumberOfProcessors: LongWord;
228 var
229 SystemInfo: TSYSTEMINFO;
230 begin
231 GetSystemInfo(@SystemInfo);
232 Result:= SystemInfo.dwNumberOfProcessors;
233 end;
234
235 function FormatFileSize(ASize: Int64; AGiga: Boolean): String;
236 begin
237 if ((ASize div cGiga) > 0) and AGiga then
238 Result:= IntToStr(ASize div cGiga) + ' GB'
239 else
240 if (ASize div cMega) >0 then
241 Result:= IntToStr(ASize div cMega) + ' MB'
242 else
243 if (ASize div cKilo) > 0 then
244 Result:= IntToStr(ASize div cKilo) + ' KB'
245 else
246 Result:= IntToStr(ASize);
247 end;
248
249 procedure SetArchiveCustom(AJclArchive: IInterface; AFormat: TArchiveFormat);
250 var
251 Index: Integer;
252 Start: Integer = 1;
253 Parameters: WideString;
254 MethodStandard: Boolean;
255 Method: TJclCompressionMethod;
256 JclArchive: TJclCompressionArchive;
257
258 procedure AddProperty(const Name: WideString; const Value: TPropVariant);
259 begin
260 with JclArchive do
261 begin
262 SetLength(PropNames, Length(PropNames) + 1);
263 PropNames[High(PropNames)] := Name;
264 SetLength(PropValues, Length(PropValues) + 1);
265 PropValues[High(PropValues)] := Value;
266 end;
267 end;
268
269 procedure AddCardinalProperty(const Name: WideString; Value: Cardinal);
270 var
271 PropValue: TPropVariant;
272 begin
273 PropValue.vt := VT_UI4;
274 PropValue.ulVal := Value;
275 AddProperty(Name, PropValue);
276 end;
277
278 procedure AddWideStringProperty(const Name: WideString; const Value: WideString);
279 var
280 PropValue: TPropVariant;
281 begin
282 PropValue.vt := VT_BSTR;
283 PropValue.bstrVal := SysAllocString(PWideChar(Value));
284 AddProperty(Name, PropValue);
285 end;
286
287 procedure AddOption(Finish: Integer);
288 var
289 C: WideChar;
290 IValue: Int64;
291 PropValue: TPropVariant;
292 Option, Value: WideString;
293 begin
294 Option:= Copy(Parameters, Start, Finish - Start);
295 Start:= Pos('=', Option);
296 if Start = 0 then
297 begin
298 PropValue.vt:= VT_EMPTY;
299 C:= Option[Length(Option)];
300 if C = '+' then
301 Variant(PropValue):= True
302 else if C = '-' then begin
303 Variant(PropValue):= False;
304 end;
305 if (PropValue.vt <> VT_EMPTY) then
306 begin
307 Delete(Option, Length(Option), 1);
308 end;
309 AddProperty(Option, PropValue);
310 end
311 else begin
312 Value:= Copy(Option, Start + 1, MaxInt);
313 SetLength(Option, Start - 1);
314 if TryStrToInt64(AnsiString(Value), IValue) then
315 AddCardinalProperty(Option, IValue)
316 else
317 AddWideStringProperty(Option, Value);
318 end;
319 end;
320
321 begin
322 JclArchive:= AJclArchive as TJclCompressionArchive;
323 // Parse additional parameters
324 Parameters:= Trim(PluginConfig[AFormat].Parameters);
325 if Length(Parameters) > 0 then
326 begin
327 for Index:= 1 to Length(Parameters) do
328 begin
329 if Parameters[Index] = #32 then
330 begin
331 AddOption(Index);
332 Start:= Index + 1;
333 end;
334 end;
335 AddOption(MaxInt);
336 end;
337 Parameters:= WideUpperCase(Parameters);
338 MethodStandard:= PluginConfig[AFormat].Method <= cmMaximum;
339 // Set word size parameter
340 if MethodStandard then
341 begin
342 Method:= TJclCompressionMethod(PluginConfig[AFormat].Method);
343 case Method of
344 cmLZMA, cmLZMA2,
345 cmDeflate, cmDeflate64:
346 begin
347 if (Pos('FB=', Parameters) = 0) and (Pos('1=', Parameters) = 0) then
348 AddCardinalProperty('fb', PluginConfig[AFormat].WordSize);
349 end;
350 cmPPMd:
351 begin
352 if Pos('O=', Parameters) = 0 then
353 AddCardinalProperty('o', PluginConfig[AFormat].WordSize);
354 end;
355 end;
356 end;
357 // Set 7-zip compression method
358 if IsEqualGUID(CLSID_CFormat7z, PluginConfig[AFormat].ArchiveCLSID^) then
359 begin
360 if Pos('0=', Parameters) = 0 then
361 begin
362 if MethodStandard then
363 AddWideStringProperty('0', MethodName[Method])
364 else begin
365 AddWideStringProperty('0', GetCodecName(PluginConfig[AFormat].Method));
366 end;
367 end;
368 if MethodStandard then
369 begin
370 if (Method > cmCopy) and (Method < cmPPMd) and (Pos('D=', Parameters) = 0) then
371 begin
372 AddWideStringProperty('D', WideString(IntToStr(PluginConfig[AFormat].Dictionary) + 'B'));
373 end
374 else if (Method = cmPPMd) and (Pos('MEM=', Parameters) = 0) then
375 begin
376 AddWideStringProperty('MEM', WideString(IntToStr(PluginConfig[AFormat].Dictionary) + 'B'));
377 end;
378 end;
379 end;
380 end;
381
382 procedure SetArchiveOptions(AJclArchive: IInterface);
383 var
384 MethodStd: Boolean;
385 ArchiveCLSID: TGUID;
386 SolidBlockSize: Int64;
387 Index: TArchiveFormat;
388 Solid: IJclArchiveSolid;
389 CompressHeader: IJclArchiveCompressHeader;
390 DictionarySize: IJclArchiveDictionarySize;
391 CompressionLevel: IJclArchiveCompressionLevel;
392 MultiThreadStrategy: IJclArchiveNumberOfThreads;
393 CompressionMethod: IJclArchiveCompressionMethod;
394 SaveCreationDateTime: IJclArchiveSaveCreationDateTime;
395 SaveLastAccessDateTime: IJclArchiveSaveLastAccessDateTime;
396 begin
397 if AJclArchive is TJclSevenzipCompressArchive then
398 ArchiveCLSID:= (AJclArchive as TJclSevenzipCompressArchive).ArchiveCLSID
399 else begin
400 ArchiveCLSID:= (AJclArchive as TJclSevenzipUpdateArchive).ArchiveCLSID
401 end;
402 for Index:= Low(PluginConfig) to High(PluginConfig) do
403 begin
404 if IsEqualGUID(ArchiveCLSID, PluginConfig[Index].ArchiveCLSID^) then
405 begin
406 MethodStd:= (PluginConfig[Index].Method <= cmMaximum);
407
408 if MethodStd and Supports(AJclArchive, IJclArchiveCompressionMethod, CompressionMethod) and Assigned(CompressionMethod) then
409 CompressionMethod.SetCompressionMethod(TJclCompressionMethod(PluginConfig[Index].Method));
410
411 if Supports(AJclArchive, IJclArchiveCompressionLevel, CompressionLevel) and Assigned(CompressionLevel) then
412 CompressionLevel.SetCompressionLevel(PluginConfig[Index].Level);
413
414 if PluginConfig[Index].Level <> PtrInt(clStore) then
415 begin
416 if MethodStd and Supports(AJclArchive, IJclArchiveDictionarySize, DictionarySize) and Assigned(DictionarySize) then
417 DictionarySize.SetDictionarySize(PluginConfig[Index].Dictionary);
418
419 if Supports(AJclArchive, IJclArchiveSolid, Solid) and Assigned(Solid) then
420 begin
421 SolidBlockSize:= Int64(PluginConfig[Index].SolidSize);
422 if SolidBlockSize <> kSolidBlockSize then SolidBlockSize:= SolidBlockSize * cKilo;
423 Solid.SetSolidBlockSize(SolidBlockSize);
424 end;
425
426 if Supports(AJclArchive, IJclArchiveNumberOfThreads, MultiThreadStrategy) and Assigned(MultiThreadStrategy) then
427 MultiThreadStrategy.SetNumberOfThreads(PluginConfig[Index].ThreadCount);
428
429 if Supports(AJclArchive, IJclArchiveSaveCreationDateTime, SaveCreationDateTime) and Assigned(SaveCreationDateTime) then
430 SaveCreationDateTime.SetSaveCreationDateTime(False);
431
432 if Supports(AJclArchive, IJclArchiveSaveLastAccessDateTime, SaveLastAccessDateTime) and Assigned(SaveLastAccessDateTime) then
433 SaveLastAccessDateTime.SetSaveLastAccessDateTime(False);
434
435 if Supports(AJclArchive, IJclArchiveCompressHeader, CompressHeader) and Assigned(CompressHeader) then
436 CompressHeader.SetCompressHeader(True);
437 end;
438
439 try
440 SetArchiveCustom(AJclArchive, Index);
441 except
442 on E: Exception do
443 MessageBoxW(0, PWideChar(UTF8ToUTF16(E.Message)), nil, MB_OK or MB_ICONERROR);
444 end;
445
446 Exit;
447 end;
448 end;
449 end;
450
451 procedure LoadConfiguration;
452 var
453 Ini: TIniFile;
454 Section: AnsiString;
455 ArchiveFormat: TArchiveFormat;
456 begin
457 try
458 Ini:= TIniFile.Create(ConfigFile);
459 try
460 LibraryPath:= Ini.ReadString('Library', TargetCPU, EmptyStr);
461 LibraryPath:= Utf16ToUtf8(ExpandEnvironmentStrings(UTF8ToUTF16(LibraryPath)));
462 for ArchiveFormat:= Low(TArchiveFormat) to High(TArchiveFormat) do
463 begin
464 Section:= GUIDToString(PluginConfig[ArchiveFormat].ArchiveCLSID^);
465 PluginConfig[ArchiveFormat].Level:= Ini.ReadInteger(Section, 'Level', DefaultConfig[ArchiveFormat].Level);
466 PluginConfig[ArchiveFormat].Method:= Ini.ReadInteger(Section, 'Method', DefaultConfig[ArchiveFormat].Method);
467 PluginConfig[ArchiveFormat].Dictionary:= Ini.ReadInteger(Section, 'Dictionary', DefaultConfig[ArchiveFormat].Dictionary);
468 PluginConfig[ArchiveFormat].WordSize:= Ini.ReadInteger(Section, 'WordSize', DefaultConfig[ArchiveFormat].WordSize);
469 PluginConfig[ArchiveFormat].SolidSize:= Ini.ReadInteger(Section, 'SolidSize', DefaultConfig[ArchiveFormat].SolidSize);
470 PluginConfig[ArchiveFormat].ThreadCount:= Ini.ReadInteger(Section, 'ThreadCount', DefaultConfig[ArchiveFormat].ThreadCount);
471 PluginConfig[ArchiveFormat].Parameters:= Ini.ReadString(Section, 'Parameters', DefaultConfig[ArchiveFormat].Parameters);
472 end;
473 finally
474 Ini.Free;
475 end;
476 except
477 on E: Exception do
478 MessageBoxW(0, PWideChar(UTF8ToUTF16(E.Message)), nil, MB_OK or MB_ICONERROR);
479 end;
480 end;
481
482 procedure SaveConfiguration;
483 var
484 Ini: TIniFile;
485 Section: AnsiString;
486 ArchiveFormat: TArchiveFormat;
487 begin
488 try
489 Ini:= TIniFile.Create(ConfigFile);
490 try
491 for ArchiveFormat:= Low(TArchiveFormat) to High(TArchiveFormat) do
492 begin
493 Section:= GUIDToString(PluginConfig[ArchiveFormat].ArchiveCLSID^);
494 Ini.WriteString(Section, 'Format', ArchiveExtension[ArchiveFormat]);
495 Ini.WriteInteger(Section, 'Level', PluginConfig[ArchiveFormat].Level);
496 Ini.WriteInteger(Section, 'Method', PluginConfig[ArchiveFormat].Method);
497 Ini.WriteInteger(Section, 'Dictionary', PluginConfig[ArchiveFormat].Dictionary);
498 Ini.WriteInteger(Section, 'WordSize', PluginConfig[ArchiveFormat].WordSize);
499 Ini.WriteInteger(Section, 'SolidSize', PluginConfig[ArchiveFormat].SolidSize);
500 Ini.WriteInteger(Section, 'ThreadCount', PluginConfig[ArchiveFormat].ThreadCount);
501 Ini.WriteString(Section, 'Parameters', PluginConfig[ArchiveFormat].Parameters);
502 end;
503 finally
504 Ini.Free;
505 end;
506 except
507 on E: Exception do
508 MessageBoxW(0, PWideChar(UTF8ToUTF16(E.Message)), nil, MB_OK or MB_ICONERROR);
509 end;
510 end;
511
512 initialization
513 CopyMemory(@PluginConfig[Low(PluginConfig)],
514 @DefaultConfig[Low(DefaultConfig)], SizeOf(PluginConfig));
515
516 end.
517