-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathmormot.lib.win7zip.pas
2948 lines (2650 loc) · 88.2 KB
/
mormot.lib.win7zip.pas
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// access to the 7-Zip library on Windows
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.lib.win7zip;
{
*****************************************************************************
Access to the 7-Zip Compression/Decompression DLL on Windows
- Low-Level 7-Zip API Definitions
- I7zReader/I7zWriter High-Level Wrappers
*****************************************************************************
Two meaningful questions:
Can I use the EXE or DLL files from 7-Zip in a commercial application?
Yes, but you are required to specify in documentation for your application:
(1) that you used parts of the 7-Zip program,
(2) that 7-Zip is licensed under the GNU LGPL license and
(3) you must give a link to www.7-zip.org, where the source code can be found.
Can I become insane because of directly calling the 7-Zip public dll API?
Yes, Igor's API is just bloated: the LCL unit has 10,000 lines just for basic
archive work. So don't become insane and use our unit. Better safe than sorry.
}
interface
{$I ..\mormot.defines.inc}
{$ifdef OSPOSIX}
// do-nothing-unit on non Windows system
implementation
{$else}
uses
Windows,
ActiveX,
sysutils,
classes,
types,
mormot.core.base,
mormot.core.os,
mormot.core.unicode,
mormot.core.text,
mormot.core.datetime;
{ ****************** Low-Level 7-Zip API Definitions }
{$Z4} // 32-bit enums
type
T7zFileTimeType = (
fttNotDefined = -1,
fttWindows = 0,
fttUnix,
fttDOS,
ftt1ns);
T7zArcInfoFlags = set of (
aifKeepName,
aifAltStreams,
aifNtSecure,
aifFindSignature,
aifMultiSignature,
aifUseGlobalOffset,
aifStartOpen,
aifPureStartOpen,
aifBackwardOpen,
aifPreArc,
aifSymLinks,
aifHardLinks,
aifByExtOnlyOpen,
aifHashHandler,
aifCTime,
aifCTime_Default,
aifATime,
aifATime_Default,
aifMTime,
aifMTime_Default);
const
kTime_Prec_Mask_bit_index = 0;
kTime_Prec_Mask_num_bits = 26;
kTime_Prec_Default_bit_index = 27;
kTime_Prec_Default_num_bits = 5;
function TIME_PREC_TO_ARC_FLAGS_MASK(x: cardinal): cardinal;
{$ifdef HASINLINE} inline; {$endif}
function TIME_PREC_TO_ARC_FLAGS_TIME_DEFAULT(x: cardinal): cardinal;
{$ifdef HASINLINE} inline; {$endif}
type
T7zHandlerPropID = (
hpiName, // VT_BSTR
hpiClassID, // binary GUID in VT_BSTR
hpiExtension, // VT_BSTR
hpiAddExtension, // VT_BSTR
hpiUpdate, // VT_BOOL
hpiKeepName, // VT_BOOL
hpiSignature, // binary in VT_BSTR
hpiMultiSignature, // binary in VT_BSTR
hpiSignatureOffset, // VT_UI4
hpiAltStreams, // VT_BOOL
hpiNtSecure, // VT_BOOL
hpiFlags, // VT_UI4
hpiTimeFlags); // VT_UI4
T7zExtractAskMode = (
eamExtract,
eamTest,
eamSkip,
eamReadExternal);
T7zExtractOperationResult = (
eorOK,
eorUnsupportedMethod,
eorDataError,
eorCRCError,
eorUnavailable,
eorUnexpectedEnd,
eorDataAfterEnd,
eorIsNotArc,
eorHeadersError,
eorWrongPassword);
T7zEventIndexType = (
eitNoIndex,
eitInArcIndex,
eitBlockIndex,
eitOutArcIndex);
T7zUpdateOperationResult = (
uorOK,
uorError,
uorError_FileChanged);
const
kpidNoProperty = 0;
kpidHandlerItemIndex = 2;
kpidPath = 3; // VT_BSTR
kpidName = 4; // VT_BSTR
kpidExtension = 5; // VT_BSTR
kpidIsFolder = 6; // VT_BOOL
kpidSize = 7; // VT_UI8
kpidPackedSize = 8; // VT_UI8
kpidAttributes = 9; // VT_UI4
kpidCreationTime = 10; // VT_FILETIME
kpidLastAccessTime = 11; // VT_FILETIME
kpidLastWriteTime = 12; // VT_FILETIME
kpidSolid = 13; // VT_BOOL
kpidCommented = 14; // VT_BOOL
kpidEncrypted = 15; // VT_BOOL
kpidSplitBefore = 16; // VT_BOOL
kpidSplitAfter = 17; // VT_BOOL
kpidDictionarySize = 18; // VT_UI4
kpidCRC = 19; // VT_UI4
kpidType = 20; // VT_BSTR
kpidIsAnti = 21; // VT_BOOL
kpidMethod = 22; // VT_BSTR
kpidHostOS = 23; // VT_BSTR
kpidFileSystem = 24; // VT_BSTR
kpidUser = 25; // VT_BSTR
kpidGroup = 26; // VT_BSTR
kpidBlock = 27; // VT_UI4
kpidComment = 28; // VT_BSTR
kpidPosition = 29; // VT_UI4
kpidPrefix = 30; // VT_BSTR
kpidNumSubDirs = 31; // VT_UI4
kpidNumSubFiles = 32; // VT_UI4
kpidUnpackVer = 33; // VT_UI1
kpidVolume = 34; // VT_UI4
kpidIsVolume = 35; // VT_BOOL
kpidOffset = 36; // VT_UI8
kpidLinks = 37; // VT_UI4
kpidNumBlocks = 38; // VT_UI4
kpidNumVolumes = 39; // VT_UI4
kpidTimeType = 40; // VT_UI4
kpidBit64 = 41; // VT_BOOL
kpidBigEndian = 42; // VT_BOOL
kpidCpu = 43; // VT_BSTR
kpidPhySize = 44; // VT_UI8
kpidHeadersSize = 45; // VT_UI8
kpidChecksum = 46; // VT_UI4
kpidCharacts = 47; // VT_BSTR
kpidVa = 48; // VT_UI8
kpidTotalSize = $1100; // VT_UI8
kpidFreeSpace = $1101; // VT_UI8
kpidClusterSize = $1102; // VT_UI8
kpidVolumeName = $1103; // VT_BSTR
kpidLocalName = $1200; // VT_BSTR
kpidProvider = $1201; // VT_BSTR
kpidUserDefined = $10000;
{$Z1} // back to default enum size
type
T7zVariant = variant;
IProgress = interface(IUnknown)
['{23170F69-40C1-278A-0000-000000050000}']
function SetTotal(total: Int64): HRESULT; stdcall;
function SetCompleted(completeValue: PInt64): HRESULT; stdcall;
end;
ICryptoGetTextPassword = interface(IUnknown)
['{23170F69-40C1-278A-0000-000500100000}']
function CryptoGetTextPassword(var password: TBStr): HRESULT; stdcall;
end;
ICryptoGetTextPassword2 = interface(IUnknown)
['{23170F69-40C1-278A-0000-000500110000}']
function CryptoGetTextPassword2(passwordIsDefined: PInteger;
var password: TBStr): HRESULT; stdcall;
end;
ISequentialInStream = interface(IUnknown)
['{23170F69-40C1-278A-0000-000300010000}']
function Read(data: pointer; size: cardinal;
processedSize: PCardinal): HRESULT; stdcall;
end;
ISequentialOutStream = interface(IUnknown)
['{23170F69-40C1-278A-0000-000300020000}']
function Write(data: pointer; size: cardinal;
processedSize: PCardinal): HRESULT; stdcall;
end;
IInStream = interface(ISequentialInStream)
['{23170F69-40C1-278A-0000-000300030000}']
function Seek(offset: Int64; seekOrigin: cardinal;
newPosition: PInt64): HRESULT; stdcall;
end;
IOutStream = interface(ISequentialOutStream)
['{23170F69-40C1-278A-0000-000300040000}']
function Seek(offset: Int64; seekOrigin: cardinal;
newPosition: PInt64): HRESULT; stdcall;
function SetSize(newSize: Int64): HRESULT; stdcall;
end;
IStreamGetSize = interface(IUnknown)
['{23170F69-40C1-278A-0000-000300060000}']
function GetSize(size: PInt64): HRESULT; stdcall;
end;
IOutStreamFlush = interface(IUnknown)
['{23170F69-40C1-278A-0000-000300070000}']
function Flush: HRESULT; stdcall;
end;
IArchiveOpenCallback = interface
['{23170F69-40C1-278A-0000-000600100000}']
function SetTotal(files, bytes: PInt64): HRESULT; stdcall;
function SetCompleted(files, bytes: PInt64): HRESULT; stdcall;
end;
IArchiveExtractCallback = interface(IProgress)
['{23170F69-40C1-278A-0000-000600200000}']
function GetStream(index: cardinal; var outStream: ISequentialOutStream;
askExtractMode: T7zExtractAskMode): HRESULT; stdcall;
function PrepareOperation(
askExtractMode: T7zExtractAskMode): HRESULT; stdcall;
function SetOperationResult(
opResult: T7zExtractOperationResult): HRESULT; stdcall;
end;
IArchiveOpenVolumeCallback = interface
['{23170F69-40C1-278A-0000-000600300000}']
function GetProperty(propID: TPropID; var value: T7zVariant): HRESULT; stdcall;
function GetStream(const name: PWideChar;
var inStream: IInStream): HRESULT; stdcall;
end;
IInArchiveGetStream = interface
['{23170F69-40C1-278A-0000-000600400000}']
function GetStream(index: cardinal;
var stream: ISequentialInStream ): HRESULT; stdcall;
end;
IArchiveOpenSetSubArchiveName = interface
['{23170F69-40C1-278A-0000-000600500000}']
function SetSubArchiveName(name: PWideChar): HRESULT; stdcall;
end;
IInArchive = interface
['{23170F69-40C1-278A-0000-000600600000}']
function Open(stream: IInStream; const maxCheckStartPosition: PInt64;
openArchiveCallback: IArchiveOpenCallback): HRESULT; stdcall;
function Close: HRESULT; stdcall;
function GetNumberOfItems(var numItems: cardinal): HRESULT; stdcall;
function GetProperty(index: cardinal; propID: TPropID;
var value: T7zVariant): HRESULT; stdcall;
function Extract(indices: PCardinalArray; numItems: cardinal;
testMode: integer; extractCallback: IArchiveExtractCallback): HRESULT; stdcall;
function GetArchiveProperty(propID: TPropID; var value: T7zVariant): HRESULT; stdcall;
function GetNumberOfProperties(numProperties: PCardinal): HRESULT; stdcall;
function GetPropertyInfo(index: cardinal;
name: PBSTR; propID: PPropID; varType: PVarType): HRESULT; stdcall;
function GetNumberOfArchiveProperties(var numProperties: cardinal): HRESULT; stdcall;
function GetArchivePropertyInfo(index: cardinal;
name: PBSTR; propID: PPropID; varType: PVarType): HRESULT; stdcall;
end;
IArchiveUpdateCallback = interface(IProgress)
['{23170F69-40C1-278A-0000-000600800000}']
function GetUpdateItemInfo(index: cardinal;
newData, newProperties: PInteger; indexInArchive: PCardinal): HRESULT; stdcall;
function GetProperty(index: cardinal; propID: TPropID;
var value: T7zVariant): HRESULT; stdcall;
function GetStream(index: cardinal;
var inStream: ISequentialInStream): HRESULT; stdcall;
function SetOperationResult(operationResult: integer): HRESULT; stdcall;
end;
IArchiveUpdateCallback2 = interface(IArchiveUpdateCallback)
['{23170F69-40C1-278A-0000-000600820000}']
function GetVolumeSize(index: cardinal; size: PInt64): HRESULT; stdcall;
function GetVolumeStream(index: cardinal;
var volumeStream: ISequentialOutStream): HRESULT; stdcall;
end;
IOutArchive = interface
['{23170F69-40C1-278A-0000-000600A00000}']
function UpdateItems(outStream: ISequentialOutStream; numItems: cardinal;
updateCallback: IArchiveUpdateCallback): HRESULT; stdcall;
function GetFileTimeType(type_: PCardinal): HRESULT; stdcall;
end;
ISetProperties = interface
['{23170F69-40C1-278A-0000-000600030000}']
function SetProperties(names: PPWideChar; values: PVariant;
numProperties: integer): HRESULT; stdcall;
end;
ICompressProgressInfo = interface
['{23170F69-40C1-278A-0000-000400040000}']
function SetRatioInfo(inSize, outSize: PInt64): HRESULT; stdcall;
end;
ICompressCoder = interface
['{23170F69-40C1-278A-0000-000400050000}']
function Code(inStream, outStream: ISequentialInStream;
inSize, outSize: PInt64;
progress: ICompressProgressInfo): HRESULT; stdcall;
end;
ICompressCoder2 = interface
['{23170F69-40C1-278A-0000-000400180000}']
function Code(var inStreams: ISequentialInStream; var inSizes: PInt64;
numInStreams: cardinal; var outStreams: ISequentialOutStream;
var outSizes: PInt64; numOutStreams: cardinal;
progress: ICompressProgressInfo): HRESULT; stdcall;
end;
ICompressSetCoderProperties = interface
['{23170F69-40C1-278A-0000-000400200000}']
function SetCoderProperties(propIDs: PPropID;
properties: PVariant; numProperties: cardinal): HRESULT; stdcall;
end;
ICompressSetDecoderProperties2 = interface
['{23170F69-40C1-278A-0000-000400220000}']
function SetDecoderProperties2(
data: PByte; size: cardinal): HRESULT; stdcall;
end;
ICompressWriteCoderProperties = interface
['{23170F69-40C1-278A-0000-000400230000}']
function WriteCoderProperties(
outStreams: ISequentialOutStream): HRESULT; stdcall;
end;
ICompressGetInStreamProcessedSize = interface
['{23170F69-40C1-278A-0000-000400240000}']
function GetInStreamProcessedSize(value: PInt64): HRESULT; stdcall;
end;
ICompressSetCoderMt = interface
['{23170F69-40C1-278A-0000-000400250000}']
function SetNumberOfThreads(numThreads: cardinal): HRESULT; stdcall;
end;
ICompressGetSubStreamSize = interface
['{23170F69-40C1-278A-0000-000400300000}']
function GetSubStreamSize(subStream: Int64; value: PInt64): HRESULT; stdcall;
end;
ICompressSetInStream = interface
['{23170F69-40C1-278A-0000-000400310000}']
function SetInStream(inStream: ISequentialInStream): HRESULT; stdcall;
function ReleaseInStream: HRESULT; stdcall;
end;
ICompressSetOutStream = interface
['{23170F69-40C1-278A-0000-000400320000}']
function SetOutStream(outStream: ISequentialOutStream): HRESULT; stdcall;
function ReleaseOutStream: HRESULT; stdcall;
end;
ICompressSetInStreamSize = interface
['{23170F69-40C1-278A-0000-000400330000}']
function SetInStreamSize(inSize: PInt64): HRESULT; stdcall;
end;
ICompressSetOutStreamSize = interface
['{23170F69-40C1-278A-0000-000400340000}']
function SetOutStreamSize(outSize: PInt64): HRESULT; stdcall;
end;
ICompressFilter = interface
['{23170F69-40C1-278A-0000-000400400000}']
function Init: HRESULT; stdcall;
function Filter(data: PByte; size: cardinal): cardinal; stdcall;
end;
ICryptoProperties = interface
['{23170F69-40C1-278A-0000-000400800000}']
function SetKey(Data: PByte; size: cardinal): HRESULT; stdcall;
function SetInitVector(data: PByte; size: cardinal): HRESULT; stdcall;
end;
ICryptoSetPassword = interface
['{23170F69-40C1-278A-0000-000400900000}']
function CryptoSetPassword(data: PByte; size: cardinal): HRESULT; stdcall;
end;
ICryptoSetCRC = interface
['{23170F69-40C1-278A-0000-000400A00000}']
function CryptoSetCRC(crc: cardinal): HRESULT; stdcall;
end;
{ ****************** I7zReader/I7zWriter High-Level Wrappers }
type
/// kind of exceptions raised by this unit
E7Zip = class(ESynException)
protected
class procedure RaiseAfterCheck(Caller: TObject; const Context: shortstring;
Res: HResult);
public
class procedure Check(Caller: TObject; const Context: shortstring;
Res: HResult);
{$ifdef HASINLINE} inline; {$endif}
class procedure CheckOk(Caller: TObject; const Context: shortstring;
Res: HResult);
{$ifdef HASINLINE} inline; {$endif}
end;
{
Discaimer:
These wrapper interfaces were inspired by MPL 2.0 Licenced code available at
https://github.com/PascalVault/Lazarus_7zip
}
type
/// the supported archive formats
// - an enumerate is easier and safer to work with than TGuid constants
// - use T7zLib.FormatGuid to retrieve the TGuid of a given archive format
T7zFormatHandler = (
fhUndefined,
fhZip,
fhBZip2,
fhRar,
fhArj,
fhZ,
fhLzh,
fh7z,
fhCab,
fhNsis,
fhlzma,
fhlzma86,
fhxz,
fhppmd,
fhAVB,
fhLP,
fhSparse,
fhAPFS,
fhVhdx,
fhBase64,
fhCOFF,
fhExt,
fhVMDK,
fhVDI,
fhQcow,
fhGPT,
fhRar5,
fhIHex,
fhHxs,
fhTE,
fhUEFIc,
fhUEFIs,
fhSquashFS,
fhCramFS,
fhAPM,
fhMslz,
fhFlv,
fhSwf,
fhSwfc,
fhNtfs,
fhFat,
fhMbr,
fhVhd,
fhPe,
fhElf,
fhMachO,
fhUdf,
fhXar,
fhMub,
fhHfs,
fhDmg,
fhCompound,
fhWim,
fhIso,
fhChm,
fhSplit,
fhRpm,
fhDeb,
fhCpio,
fhTar,
fhGZip);
I7zArchive = interface;
// note: the sender of following events is a I7zReader or I7zWriter
/// event as used by I7zReader.SetPasswordCallback
// - should return true to continue the execution, or false to abort
T7zPasswordCallback = function(const sender: I7zArchive;
var password: RawUtf8): boolean of object;
/// event as used by I7zReader.Extract/ExtractAll methods
// - should return S_OK to continue the execution, or something else to abort
T7zGetStreamCallBack = function(const sender: I7zArchive; index: cardinal;
var outStream: ISequentialOutStream): HRESULT of object;
/// event as used by I7zReader/I7zWriter.SetProgressCallback method
// - should return S_OK to continue the execution, or something else to abort,
// e.g. ERROR_OPERATION_ABORTED
T7zProgressCallback = function(const sender: I7zArchive;
current, total: Int64): HRESULT of object;
/// the parent interface of both I7zReader and I7zWriter
I7zArchive = interface
['{9C0C0C8C-883A-49ED-B608-A6D66D36D530}']
// -- some internal methods used as property getters
function GetSize(index: integer): Int64;
function GetPackSize(index: integer): Int64;
function GetCrc(index: integer): cardinal;
function GetComment(index: integer): RawUtf8;
function GetModDate(index: integer): TDateTime;
function GetCreateDate(index: integer): TDateTime;
function GetZipName(index: integer): RawUtf8;
function GetName(index: integer): RawUtf8;
function GetMethod(index: integer): RawUtf8;
function GetIsFolder(index: integer): boolean;
function GetAttributes(index: integer): cardinal;
function GetInstance: TObject;
/// the file name specified to OpenFile() or SaveToFile()
function FileName: TFileName;
/// the high-level identifier of this archive format
function Format: T7zFormatHandler;
/// the usual main extension of this archive format (e.g. 'zip' for fhZip)
function FormatExt: string;
/// all known extensions of this archive format (as '*.ext1;*.ext2')
function FormatExts: string;
/// the 7-zip internal CSLID of this archive format
function ClassId: TGuid;
/// let an event be called during file process
procedure SetProgressCallback(const callback: T7zProgressCallback);
/// let an event be called to let the user supply a password
procedure SetPasswordCallback(const callback: T7zPasswordCallback);
/// supply a password
procedure SetPassword(const password: RawUtf8);
/// the number of files in this archive
// - can be used as 0..Count-1 index in
// ZipName/FullName/Size/PackSize/IsFolder/Method/Comment/ModDate[ndx]
// properties and I7zReader.Extract or I7zWriter.SaveToFile methods
function Count: integer;
/// search for a given file name in this archive
// - case-insensitive search within ZipName[] items
// - returns -1 if not found, or the index in Item*[] properties
function NameToIndex(const zipname: RawUtf8): integer;
/// the kpidPath property value of an archived file
// - i.e. its usual file name, potentially with a relative path
// - e.g. 'toto.txt' or 'rep\toto.bmp'
property ZipName[index: integer]: RawUtf8
read GetZipName;
/// the kpidName property value of an archived file
// - is likely to be '' for most packers
property FullName[index: integer]: RawUtf8
read GetName;
/// the kpidSize property value of an archived file
property Size[index: integer]: Int64
read GetSize;
/// the kpidPackedSize property value
property PackSize[index: integer]: Int64
read GetPackSize;
/// the kpidIsFolder property value of an archived file
property IsFolder[index: integer]: boolean
read GetIsFolder;
/// the kpidMethod property value of an archived file
property Method[index: integer]: RawUtf8
read GetMethod;
/// the kpidComment property value of an archived file
property Comment[index: integer]: RawUtf8
read GetComment;
/// the kpidCRC property value of an archived file
property Crc[index: integer]: cardinal
read GetCrc;
/// the kpidLastWriteTime property value - i.e. the "usual" date
property ModDate[index: integer]: TDateTime
read GetModDate;
/// the kpidCreationTime property value
// - may be 0 for some packers (e.g. zip)
property CreateDate[index: integer]: TDateTime
read GetCreateDate;
/// the kpidAttributes property value
// - may be 0 for some packers (e.g. zip)
property Attributes[index: integer]: cardinal
read GetAttributes;
end;
/// reading acccess to an archive content using the 7z.dll
// - use inherited I7zArchive methods to access the archive information
I7zReader = interface(I7zArchive)
['{9C0C0C8C-883A-49ED-B608-A6D66D36D53A}']
// -- main high-level methods
/// open a given archive file
procedure OpenFile(const name: TFileName);
/// open an archive content from a stream
procedure OpenStream(stream: IInStream);
/// close the archive
procedure Close;
/// retrieve the raw value of one property
function GetProp(item: cardinal; prop: TPropID): T7zVariant;
/// uncompress a file by index from this archive into a stream
// - if no Stream is specified, will test for the output
procedure Extract(item: cardinal; Stream: TStream); overload;
/// uncompress a file by index from this archive into a local folder
procedure Extract(item: cardinal; const path: TFileName;
nosubfolder: boolean); overload;
/// uncompress a file by name from this archive into a stream
// - if no Stream (i.e. nil) is specified, will test for the output
function Extract(const zipname: RawUtf8; Stream: TStream): boolean; overload;
/// uncompress a file by name from this archive into a RawByteString
function Extract(const zipname: RawUtf8): RawByteString; overload;
/// uncompress a file by name from this archive into a local folder
function Extract(const zipname: RawUtf8; const path: TFileName;
nosubfolder: boolean = false): boolean; overload;
/// uncompress some files by name from this archive into a local folder
// - returns nil on success, or the list of invalid zipnames
function Extract(const zipnames: array of RawUtf8; const path: TFileName;
nosubfolder: boolean = false): TRawUtf8DynArray; overload;
/// uncompress several files from this archive using a callback per file
// - if no Callback is specified (as default), will test for the output
procedure Extract(const items: array of integer;
const callback: T7zGetStreamCallBack = nil); overload;
/// uncompress all files from this archive using a callback per file
// - if no Callback is specified (as default), will test for the output
procedure ExtractAll(const callback: T7zGetStreamCallBack = nil); overload;
/// uncompress all files from this archive into a given local folder
procedure ExtractAll(const path: TFileName; nosubfolder: boolean = false); overload;
/// access to the 7-zip internal API reading raw object
function InArchive: IInArchive;
end;
TZipCompressionMethod = (
mzCopy,
mzDeflate,
mzDeflate64,
mzBZip2,
mzLzma,
mzPpmd);
TZipEncryptionMethod = (
emAes128,
emAes192,
emAes256,
emZipCrypto);
T7zCompressionMethod = (
m7Copy,
m7Lzma,
mzLzma2,
m7BZip2,
m7Ppmd,
m7Deflate,
m7Deflate64);
/// writing acccess to an archive content using the 7z.dll
// - main pattern is to call AddFile/AddFiles/AddBuffer then SaveToFile() to
// generate a new archive
I7zWriter = interface(I7zArchive)
['{9C0C0C8C-883A-49ED-B608-A6D66D36D53B}']
// -- main high-level methods
/// add (or replace if ZipName already exists) a file within the archive
function AddFile(const Filename: TFileName; const ZipName: RawUtf8): boolean;
/// add (or replace) some files from a folder within the archive
// - returns the file names which were not readable so were not added
function AddFiles(const Dir, Path, Wildcard: TFileName; recurse: boolean): TFileNameDynArray;
/// add (or replace) a file from a memory buffer within the archive
procedure AddBuffer(const ZipName: RawUtf8; const Data: RawByteString);
/// add (or replace) a void folder within the archive
procedure AddDirectory(const ZipName: RawUtf8);
/// add (or replace) a file from a stream within the archive
procedure AddStream(Stream: TStream; Ownership: TStreamOwnership;
Attributes: cardinal; CreationTime, LastWriteTime: TUnixTime;
const Path: RawUtf8; IsFolder, IsAnti: boolean);
/// remove a file within the archive, from its index
function Delete(index: integer): boolean; overload;
/// remove a file within the archive, from its zipname
function Delete(const ZipName: RawUtf8): boolean; overload;
/// create a new archive file from the previously set AddFile/Delete
procedure SaveToFile(const DestName: TFileName);
/// create a new archive stream from the previously set AddFile/Delete
procedure SaveToStream(stream: TStream);
/// remove all previously set AddFile/Delete
procedure Clear;
/// modify a property of a file within the archive
procedure SetProperty(const zipname: RawUtf8; const value: T7zVariant);
/// modify the compression level to be used during process
procedure SetCompressionLevel(level: cardinal);
/// modify the threading level to be used during process
procedure SetMultiThreading(threadCount: cardinal);
/// modify the compression method to be used during process
procedure SetCompressionMethod(method: TZipCompressionMethod);
/// modify the encryption method to be used during process
procedure SetEncryptionMethod(method: TZipEncryptionMethod);
procedure SetDictionnarySize(size: cardinal);
procedure SetMemorySize(size: cardinal);
procedure SetDeflateNumPasses(pass: cardinal);
procedure SetNumFastBytes(fb: cardinal);
procedure SetNumMatchFinderCycles(mc: cardinal);
procedure SetCompressionMethod7z(method: T7zCompressionMethod);
procedure SetBindInfo7z(const bind: WideString);
procedure SetSolidSettings7z(solid: boolean);
procedure RemoveSfxBlock7z(remove: boolean);
procedure AutoFilter7z(auto: boolean);
procedure CompressHeaders7z(compress: boolean);
procedure CompressHeadersFull7z(compress: boolean);
procedure EncryptHeaders7z(encrypt: boolean);
procedure VolumeMode7z(mode: boolean);
/// access to the 7-zip internal API writing raw object
function OutArchive: IOutArchive;
end;
I7zCodec = interface
['{9C0C0C8C-883A-49ED-B608-A6D66D36D53C}']
end;
type
/// this is the main loader and factory for 7z.dll
I7zLib = interface
/// the library .dll file name as loaded by Create()
function FileName: TFileName;
/// factory of the main I7zReader high-level archive decompressor
function NewReader(fmt: T7zFormatHandler): I7zReader; overload;
/// factory of the main I7zReader high-level archive file decompressor
// - will guess the file format from its existing content by default
function NewReader(const name: TFileName;
fmt: T7zFormatHandler = fhUndefined; const pw: RawUtf8 = ''): I7zReader; overload;
/// factory of the main I7zWriter high-level archive compressor
function NewWriter(fmt: T7zFormatHandler): I7zWriter; overload;
/// factory of the main I7zWriter high-level archive compressor
// - will guess the file format from its existing content by default
function NewWriter(const name: TFileName;
fmt: T7zFormatHandler = fhUndefined): I7zWriter; overload;
end;
/// implement the main loader and factory for 7z.dll
// - you can instantiate this class and store it into a I7zLib instance,
// then use NewReader/NewWriter factories when needed: just ensure
// the I7zlib instance is released last
// - as alternative, you can use global New7zReader/New7zWriter factories
T7zLib = class(TInterfacedObject, I7zLib)
protected
fHandle: THandle;
fFileName: TFileName;
fFormat: T7zFormatHandler;
// the raw function from 7z.dll
fCreateObject: function(const clsid, iid: TGuid; var outObject): HRESULT; stdcall;
function TryLoad(const libname: TFileName): boolean;
/// main encapsulated API factory of the library
procedure CreateObject(const clsid, iid: TGuid; var obj);
public
/// return the GUID of a given archive format
// - in the form '{23170F69-40C1-278A-1000-000110xx0000}'
class function FormatGuid(fmt: T7zFormatHandler): TGuid;
/// try to detect the proper archive format of a file
// - will read the file header first, unless OnlyFileName is set
class function FormatDetect(const FileName: TFileName;
OnlyFileName: boolean = false): T7zFormatHandler;
/// return the known file extensions of a given archive format as '*.ext1;*.ext2'
class function FormatFileExtensions(fmt: T7zFormatHandler): string;
/// return the main file extension of a given archive format as 'ext'
class function FormatFileExtension(fmt: T7zFormatHandler): string;
/// load the 7z.dll
constructor Create(lib: TFileName = ''); reintroduce;
/// unload the 7z.dll
destructor Destroy; override;
/// I7zLib methods
function FileName: TFileName;
function NewReader(fmt: T7zFormatHandler): I7zReader; overload;
function NewReader(const name: TFileName; fmt: T7zFormatHandler = fhUndefined;
const pw: RawUtf8 = ''): I7zReader; overload;
function NewWriter(fmt: T7zFormatHandler): I7zWriter; overload;
function NewWriter(const name: TFileName;
fmt: T7zFormatHandler = fhUndefined): I7zWriter; overload;
end;
/// global factory of the main I7zReader high-level archive file decompressor
// - will guess the file format from its existing content
// - will own its own TZlib instance to access the 7z.dll library
function New7zReader(const name: TFileName; fmt: T7zFormatHandler = fhUndefined;
const lib: TFileName = ''; const pw: RawUtf8 = ''): I7zReader;
/// global factory of the main I7zWriter high-level archive compressor
// - will own its own TZlib instance to access the 7z.dll library
function New7zWriter(fmt: T7zFormatHandler; const lib: TFileName = ''): I7zWriter; overload;
/// global factory of the main I7zWriter to update an existing archive file
// - will guess the file format from its existing content
// - will own its own TZlib instance to access the 7z.dll library
function New7zWriter(const name: TFileName; fmt: T7zFormatHandler = fhUndefined;
const lib: TFileName = ''): I7zWriter; overload;
implementation
{ ****************** Low-Level 7-Zip API Definitions }
function TIME_PREC_TO_ARC_FLAGS_MASK(x: cardinal): cardinal;
begin
result := cardinal(1) shl (kTime_Prec_Mask_bit_index + x);
end;
function TIME_PREC_TO_ARC_FLAGS_TIME_DEFAULT(x: cardinal): cardinal;
begin
result := x shl kTime_Prec_Default_bit_index;
end;
{ ****************** I7zReader/I7zWriter High-Level Wrappers }
{ E7Zip }
class procedure E7Zip.RaiseAfterCheck(Caller: TObject;
const Context: shortstring; Res: HResult);
begin
raise CreateFmt('%s.%s error %x (%s)',
[ClassNameShort(Caller)^, Context, Res, string(WinErrorText(Res, nil))])
end;
class procedure E7Zip.Check(Caller: TObject; const Context: shortstring;
Res: HResult);
begin
if Res and $80000000 <> 0 then
RaiseAfterCheck(Caller, Context, Res);
end;
class procedure E7Zip.CheckOk(Caller: TObject; const Context: shortstring;
Res: HResult);
begin
if Res <> S_OK then
RaiseAfterCheck(Caller, Context, Res);
end;
type
/// the properties identifiers of a 7-zip
T7zMethodPropID = (
kID,
kName,
kDecoder,
kEncoder,
kInStreams,
kOutStreams,
kDescription,
kDecoderIsAssigned,
kEncoderIsAssigned
);
T7zArchive = class;
T7zStream = class(TInterfacedObject,
ISequentialInStream, IInStream, IStreamGetSize,
ISequentialOutStream, IOutStream, IOutStreamFlush)
private
fStream: TStream;
fIndex: integer;
fOwnedStream: boolean;
protected
// ISequentialInStream method
function Read(data: pointer; size: cardinal;
processedSize: PCardinal): HRESULT; stdcall;
// IInStream and IOutStream method
function Seek(offset: Int64; seekOrigin: cardinal;
newPosition: PInt64): HRESULT; stdcall;
// IStreamGetSize method
function GetSize(size: PInt64): HRESULT; stdcall;
// ISequentialOutStream method
function Write(data: pointer; size: cardinal;
processedSize: PCardinal): HRESULT; stdcall;
// IOutStream method
function SetSize(newSize: Int64): HRESULT; stdcall;
// IOutStreamFlush method
function Flush: HRESULT; stdcall;
public
constructor Create(Stream: TStream; OwnedStream: boolean;
ArchiveIndex: integer = -1);
constructor CreateFromFile(const Name: TFileName; Mode: cardinal;
ArchiveIndex: integer = -1);
destructor Destroy; override;
end;
T7zParent = class(TInterfacedObject)
private
fOwner: T7zLib;
public
constructor Create(lib: T7zLib); reintroduce; virtual;
end;
T7zCodec = class(T7zParent,
I7zCodec, ICompressProgressInfo)
private
// the raw functions from 7z.dll
fGetMethodProperty: function(index: cardinal; propID: T7zMethodPropID;
var value: T7zVariant): HRESULT; stdcall;
fGetNumberOfMethods: function(numMethods: PCardinal): HRESULT; stdcall;
protected
function GetNumberOfMethods: cardinal;
function GetMethodProperty(index: cardinal; propID: T7zMethodPropID): T7zVariant;
function GetName(index: integer): string;
// ICompressProgressInfo method
function SetRatioInfo(inSize, outSize: PInt64): HRESULT; stdcall;
public
function GetDecoder(index: integer): ICompressCoder;
function GetEncoder(index: integer): ICompressCoder;
constructor Create(lib: T7zLib); override;
property MethodProperty[index: cardinal; propID: T7zMethodPropID]: T7zVariant
read GetMethodProperty;
property NumberOfMethods: cardinal
read GetNumberOfMethods;
property Name[index: integer]: string
read GetName;
end;
T7zArchive = class(T7zParent,
I7zArchive, IProgress, ICryptoGetTextPassword, ICryptoGetTextPassword2)
protected
fFileName: TFileName;
// the raw function from 7z.dll
fGetHandlerProperty: function(propID: T7zHandlerPropID;
var value: T7zVariant): HRESULT; stdcall;
protected
fClassId: TGuid;
fFormat: T7zFormatHandler;
fLibOwned: I7zLib;
fProgressCallback: T7zProgressCallback;
fProgressCurrent: Int64;
fProgressTotal: Int64;
fPasswordCallback: T7zPasswordCallback;
fPasswordIsDefined: boolean;
fPasswordUtf16: SynUnicode;
function GetLibStringProperty(index: T7zHandlerPropID): string;
function GetLibGUIDProperty(index: T7zHandlerPropID): TGuid;
function GetInstance: TObject;
// I7zArchive methods
function FileName: TFileName;
function Format: T7zFormatHandler;
function FormatExt: string;
function FormatExts: string;
function ClassId: TGuid;
procedure SetProgressCallback(const callback: T7zProgressCallback);
procedure SetPasswordCallback(const callback: T7zPasswordCallback);
procedure SetPassword(const password: RawUtf8);
function Count: integer; virtual; abstract;
function NameToIndex(const zipname: RawUtf8): integer; virtual; abstract;
function GetZipName(index: integer): RawUtf8; virtual; abstract;
function GetName(index: integer): RawUtf8; virtual; abstract;
function GetMethod(index: integer): RawUtf8; virtual; abstract;
function GetSize(index: integer): Int64; virtual; abstract;
function GetIsFolder(index: integer): boolean; virtual; abstract;
function GetPackSize(index: integer): Int64; virtual; abstract;