-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSevenZipVCL.pas
1355 lines (1185 loc) · 52.8 KB
/
SevenZipVCL.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
(* MPUI-hcb, an MPlayer frontend for Windows
Copyright (C) 2006-2013 Huang Chen Bin <[email protected]>
based on TsevenZip by Ivo Andonov and TSevenZipVCL by Rainer Geigenberger ( -> http://www.rg-software.de - [email protected] )
Thanks to:
- Marko Kamin
- Craig Peterson
- Roberto
- Erik Smith
- Sergey Prokhorov
- Flurin Honegger
- Zach Saw
- Guillaume Di Giusto
This Unit is under Mozilla Public Licence
(
- You can use this Unit for free in free, share and commercial application.
- Mark clearly in your Readme or Help file that you use this unit/VCL with a link the
SevenZipVCL Homepage ( http://www.rg-software.de )
- Any changes of the source must be publised ( Just send it to me :- ) [email protected] )
)
*)
unit SevenZipVCL;
interface
uses
Windows, SysUtils, TntSysUtils, Tntdialogs, Classes,
TntWindows, ActiveX ,core, locale, plist;
const
//7z internal consts
//Extract
//NAskMode
kExtract = 0;
kTest = 1;
kSkip = 2;
//NOperationResult
kOK = 0;
kUnSupportedMethod = 1;
kDataError = 2;
kCRCError = 3;
FNAME_MAX32 = 512;
// SevenZIP onMessage Errorcode
FNoError = 0;
FFileNotFound = 1;
FDataError = 2;
FCRCError = 3;
FUnsupportedMethod = 4;
FIndexOutOfRange = 5; //FHO 21.01.2007
FUsercancel = 6;
FNoSFXarchive = 7;
FSFXModuleError = 8;
FSXFileCreationError = 9; //FHO 21.01.2007
FNoFilesToAdd =10; //FHO 21.01.2007
FNoFileCreated =11;
c7zipResMsg:array[FNoError..FNoFileCreated] of string= //FHO 21.01.2007
{ 0}('Success', //FHO 21.01.2007
{ 1} 'File not found', //FHO 21.01.2007
{ 2} 'Data Error', //FHO 21.01.2007
{ 3} 'CRC Error', //FHO 21.01.2007
{ 4} 'Unsupported Method', //FHO 21.01.2007
{ 5} 'Index out of Range', //FHO 21.01.2007
{ 6} 'User canceled operation', //FHO 21.01.2007
{ 7} 'File is not an 7z SFX archive', //FHO 21.01.2007
{ 8} 'SFXModule error ( Not found )', //FHO 21.01.2007
{ 9} 'Could not create SFX', //FHO 21.01.2007
{10} 'No files to add', //FHO 21.01.2007
{11} 'Could not create file' //FHO 21.01.2007
); //FHO 21.01.2007
const
kpidNoProperty = 0;
kpidHandlerItemIndex = 2;
kpidPath = 3;
kpidName = 4;
kpidExtension = 5;
kpidIsFolder = 6;
kpidSize = 7;
kpidPackedSize = 8;
kpidAttributes = 9;
kpidCreationTime = 10;
kpidLastAccessTime = 11;
kpidLastWriteTime = 12;
kpidSolid = 13;
kpidCommented = 14;
kpidEncrypted = 15;
kpidSplitBefore = 16;
kpidSplitAfter = 17;
kpidDictionarySize = 18;
kpidCRC = 19;
kpidType = 20;
kpidIsAnti = 21;
kpidMethod = 22;
kpidHostOS = 23;
kpidFileSystem = 24;
kpidUser = 25;
kpidGroup = 26;
kpidBlock = 27;
kpidComment = 28;
kpidPosition = 29;
kpidTotalSize = $1100;
kpidFreeSpace = $1101;
kpidClusterSize = $1102;
kpidVolumeName = $1103;
kpidLocalName = $1200;
kpidProvider = $1201;
kpidUserDefined = $10000;
//jjw 18.10.2006
type
TCreateObjectFunc = function ( const clsid: PGUID; const iid: PGUID; out _out ): Integer; stdcall;
//----------------------------------------------------------------------------------------------------
//--------------Widestring Classes--------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
type
TCompressType = ( LZMA,PPMD );
TCompressStrength = ( SAVE,FAST,NORMAL,MAXIMUM,ULTRA );
TLZMAStrength = 0..27;
TPPMDMem = 1..31;
TPPMDSize = 2..32;
AddOptsEnum = ( AddRecurseDirs, AddSolid, AddStoreOnlyFilename, AddIncludeDriveLetter, AddEncryptFilename );
AddOpts = Set Of AddOptsEnum;
ExtractOptsEnum = ( ExtractNoPath,ExtractOverwrite );
ExtractOpts = Set Of ExtractOptsEnum;
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//--------------Start SevenZip Interface-------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
type
TInterfacedObject = class( TObject, IUnknown )
protected
FRefCount: Integer;
function QueryInterface( const IID: TGUID; out Obj ): HResult; stdcall;
function _AddRef: Integer; stdcall;
function _Release: Integer; stdcall;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
class function NewInstance: TObject; override;
property RefCount: Integer read FRefCount;
end;
const
//000
szIID_IProgress = '{23170F69-40C1-278A-0000-000000050000}';
//30
szIID_ISequentialInStream = '{23170F69-40C1-278A-0000-000300010000}';
szIID_ISequentialOutStream = '{23170F69-40C1-278A-0000-000300020000}';
szIID_IInStream = '{23170F69-40C1-278A-0000-000300030000}';
szIID_IOutStream = '{23170F69-40C1-278A-0000-000300040000}';
szIID_IStreamGetSize = '{23170F69-40C1-278A-0000-000300060000}';
szIID_IOutStreamFlush = '{23170F69-40C1-278A-0000-000300070000}';
//400
szIID_ICompressProgressInfo = '{23170F69-40C1-278A-0000-000400040000}';
szIID_ICompressCoder = '{23170F69-40C1-278A-0000-000400050000}';
szIID_ICompressCoder2 = '{23170F69-40C1-278A-0000-000400180000}';
szIID_ICompressSetCoderProperties = '{23170F69-40C1-278A-0000-000400200000}';
szIID_ICompressSetDecoderProperties = '{23170F69-40C1-278A-0000-000400210000}';
szIID_ICompressSetDecoderProperties2 = '{23170F69-40C1-278A-0000-000400220000}';
szIID_ICompressWriteCoderProperties = '{23170F69-40C1-278A-0000-000400230000}';
szIID_ICompressGetInStreamProcessedSize = '{23170F69-40C1-278A-0000-000400240000}';
szIID_ICompressGetSubStreamSize = '{23170F69-40C1-278A-0000-000400300000}';
szIID_ICompressSetInStream = '{23170F69-40C1-278A-0000-000400310000}';
szIID_ICompressSetOutStream = '{23170F69-40C1-278A-0000-000400320000}';
szIID_ICompressSetInStreamSize = '{23170F69-40C1-278A-0000-000400330000}';
szIID_ICompressSetOutStreamSize = '{23170F69-40C1-278A-0000-000400340000}';
szIID_ICompressFilter = '{23170F69-40C1-278A-0000-000400400000}';
szIID_ICryptoProperties = '{23170F69-40C1-278A-0000-000400800000}';
szIID_ICryptoSetPassword = '{23170F69-40C1-278A-0000-000400900000}';
szIID_ICryptoSetCRC = '{23170F69-40C1-278A-0000-000400A00000}';
//500
szIID_ICryptoGetTextPassword = '{23170F69-40C1-278A-0000-000500100000}';
szIID_ICryptoGetTextPassword2 = '{23170F69-40C1-278A-0000-000500110000}';
//600
szIID_ISetProperties = '{23170F69-40C1-278A-0000-000600030000}';
szIID_IArchiveOpenCallback = '{23170F69-40C1-278A-0000-000600100000}';
szIID_IArchiveExtractCallback = '{23170F69-40C1-278A-0000-000600200000}';
szIID_IArchiveOpenVolumeCallback = '{23170F69-40C1-278A-0000-000600300000}';
szIID_IInArchiveGetStream = '{23170F69-40C1-278A-0000-000600400000}';
szIID_IArchiveOpenSetSubArchiveName = '{23170F69-40C1-278A-0000-000600500000}';
szIID_IInArchive = '{23170F69-40C1-278A-0000-000600600000}';
szIID_IArchiveUpdateCallback = '{23170F69-40C1-278A-0000-000600800000}';
szIID_IArchiveUpdateCallback2 = '{23170F69-40C1-278A-0000-000600820000}';
szIID_IOutArchive = '{23170F69-40C1-278A-0000-000600A00000}';
szIID_CCrypto_Hash_SHA256 = '{23170F69-40C1-278B-0703-000000000000}';
szIID_CCrypto7zAESEncoder = '{23170F69-40C1-278B-06F1-070100000100}';
szIID_CCrypto7zAESDecoder = '{23170F69-40C1-278B-06F1-070100000000}';
CLSID_CFormat:array[0..ZipTypeCount]of TGUID =(
{ CLSID_CFormat7z: TGUID = } '{23170F69-40C1-278A-1000-000110070000}',
{ CLSID_CFormatRar: TGUID = } '{23170F69-40C1-278A-1000-000110030000}',
{ CLSID_CFormatZip: TGUID = } '{23170F69-40C1-278A-1000-000110010000}',
{ CLSID_CFormat001: TGUID = } '{23170F69-40C1-278A-1000-000110070000}',
{ CLSID_CFormatArj: TGUID = } '{23170F69-40C1-278A-1000-000110040000}',
{ CLSID_CFormatBZip2: TGUID = } '{23170F69-40C1-278A-1000-000110020000}',
{ CLSID_CFormatZ: TGUID = } '{23170F69-40C1-278A-1000-000110050000}',
{ CLSID_CFormatLzh: TGUID = } '{23170F69-40C1-278A-1000-000110060000}',
{ CLSID_CFormatCab: TGUID = } '{23170F69-40C1-278A-1000-000110080000}',
// CLSID_CFormatNsis: TGUID = '{23170F69-40C1-278A-1000-000110090000}';
{ CLSID_CFormatLzma: TGUID = } '{23170F69-40C1-278A-1000-0001100a0000}',
(* CLSID_CFormatPe: TGUID = '{23170F69-40C1-278A-1000-000110dd0000}';
CLSID_CFormatElf: TGUID = '{23170F69-40C1-278A-1000-000110de0000}';
CLSID_CFormatMach_O: TGUID = '{23170F69-40C1-278A-1000-000110df0000}';
CLSID_CFormatUdf: TGUID = '{23170F69-40C1-278A-1000-000110e00000}'; *)
{ CLSID_CFormatXar: TGUID = } '{23170F69-40C1-278A-1000-000110e10000}',
// CLSID_CFormatMub: TGUID = '{23170F69-40C1-278A-1000-000110e20000}';
{ CLSID_CFormatHfs: TGUID = } '{23170F69-40C1-278A-1000-000110e30000}',
{ CLSID_CFormatDmg: TGUID = } '{23170F69-40C1-278A-1000-000110e40000}',
// CLSID_CFormatCompound: TGUID = '{23170F69-40C1-278A-1000-000110e50000}';
{ CLSID_CFormatWim: TGUID = } '{23170F69-40C1-278A-1000-000110e60000}',
//{ CLSID_CFormatIso: TGUID = } '{23170F69-40C1-278A-1000-000110e70000}',
// CLSID_CFormatBkf: TGUID = '{23170F69-40C1-278A-1000-000110e80000}';
// CLSID_CFormatChm: TGUID = '{23170F69-40C1-278A-1000-000110e90000}';
{ CLSID_CFormatSplit: TGUID = } '{23170F69-40C1-278A-1000-000110ea0000}',
{ CLSID_CFormatRpm: TGUID = } '{23170F69-40C1-278A-1000-000110eb0000}',
{ CLSID_CFormatDeb: TGUID = } '{23170F69-40C1-278A-1000-000110ec0000}',
{ CLSID_CFormatCpio: TGUID = } '{23170F69-40C1-278A-1000-000110ed0000}',
{ CLSID_CFormatTar: TGUID = } '{23170F69-40C1-278A-1000-000110ee0000}',
{ CLSID_CFormatGZip: TGUID = } '{23170F69-40C1-278A-1000-000110ef0000}'
);
// CLSID_CFormat7z: TGUID = szCLSID_CFormat7z;
IID_IInArchive: TGUID = szIID_IInArchive;
IID_IOutArchive: TGUID = szIID_IOutArchive;
IID_ISetProperties: TGUID = szIID_ISetProperties;
IID_ICompressCoder: TGUID = szIID_ICompressCoder;
IID_ICryptoGetTextPassword: TGUID = szIID_ICryptoGetTextPassword;
IID_ICryptoGetTextPassword2: TGUID = szIID_ICryptoGetTextPassword2;
IID_ICryptoSetPassword: TGUID = szIID_ICryptoSetPassword;
IID_IOutStream: TGUID = szIID_IOutStream;
IID_ISequentialInStream: TGUID = szIID_ISequentialInStream;
IID_IInStream: TGUID = szIID_IInStream;
IID_IStreamGetSize: TGUID = szIID_IStreamGetSize;
IID_IArchiveOpenCallback: TGUID = szIID_IArchiveOpenCallback;
IID_ICompressGetSubStreamSize: TGUID = szIID_ICompressGetSubStreamSize;
IID_IArchiveOpenSetSubArchiveName: TGUID = szIID_IArchiveOpenSetSubArchiveName;
IID_IArchiveExtractCallback: TGUID = szIID_IArchiveExtractCallback;
IID_IArchiveOpenVolumeCallback: TGUID = szIID_IArchiveOpenVolumeCallback;
IID_IArchiveUpdateCallback: TGUID = szIID_IArchiveUpdateCallback;
IID_IArchiveUpdateCallback2: TGUID = szIID_IArchiveUpdateCallback2;
IID_IProgress: TGUID = szIID_IProgress;
IID_ISequentialOutStream: TGUID = szIID_ISequentialOutStream;
type
HARC = THandle;
PInt64 = ^Int64;
type
ISequentialOutStream = interface( IUnknown )
[ szIID_ISequentialOutStream ]
function Write( const data; size: DWORD; processedSize: PDWORD ): Integer; stdcall;
end;
ISequentialInStream = interface( IUnknown )
[ szIID_ISequentialInStream ]
function Read( var data; size: DWORD; processedSize: PDWORD ): Integer; stdcall;
end;
ICryptoGetTextPassword = interface( IUnknown )
[ szIID_ICryptoGetTextPassword ]
function CryptoGetTextPassword( var Password: PWideChar ): Integer; stdcall;
end;
IInStream = interface( ISequentialInStream )
[ szIID_IInStream ]
function Seek( offset: Int64; seekOrigin: DWORD;newPosition: PInt64 ): Integer; stdcall;
end;
IStreamGetSize = interface( IUnknown )
[ szIID_IStreamGetSize ]
function GetSize( var size: Int64 ): Integer; stdcall;
end;
IArchiveOpenCallback = interface( IUnknown )
[ szIID_IArchiveOpenCallback ]
function SetTotal( const files: Int64; const bytes: Int64 ): Integer; stdcall;
function SetCompleted( const files: Int64; const bytes: Int64 ): Integer; stdcall;
end;
IProgress = interface( IUnknown )
[ szIID_IProgress ]
function SetTotal( total: Int64 ): Integer; stdcall;
function SetCompleted( const completeValue: PInt64 ): Integer; stdcall;
end;
IArchiveExtractCallback = interface( IProgress )
[ szIID_IArchiveExtractCallback ]
function GetStream( index: DWORD; out outStream: ISequentialOutStream; askExtractMode: DWORD ): Integer; stdcall;
// GetStream OUT: S_OK - OK, S_FALSE - skeep this file
function PrepareOperation( askExtractMode: Integer ): Integer; stdcall;
function SetOperationResult( resultEOperationResult: Integer ): Integer; stdcall;
end;
IInArchive = interface( IUnknown )
[ szIID_IInArchive ]
function Open( stream: IInStream; const maxCheckStartPosition: PInt64; openArchiveCallback: IArchiveOpenCallback ): Integer; stdcall;
function Close( ): Integer; stdcall;
function GetNumberOfItems( out numItems: DWORD ): Integer; stdcall;
function GetProperty( index: DWORD; propID: PROPID; var value: PROPVARIANT ): Integer; stdcall;
function Extract( const indices: PDWORD; numItems: DWORD; testMode: Integer; extractCallback: IArchiveExtractCallback ): Integer; stdcall;
function GetArchiveProperty( propID: PROPID; value: PPROPVARIANT ): Integer; stdcall;
function GetNumberOfProperties( var numProperties: DWORD ): Integer; stdcall;
function GetPropertyInfo( index: DWORD; var name: TBSTR; var propID: PROPID; var varType: {PVARTYPE}Integer ): Integer; stdcall;
function GetNumberOfArchiveProperties( var numProperties ): Integer; stdcall;
function GetArchivePropertyInfo( index: DWORD; name: PBSTR; propID: PPROPID; varType: {PVARTYPE}PInteger ): Integer; stdcall;
end;
IOutStream = interface( ISequentialOutStream )
[ szIID_IOutStream ]
function Seek( offset: Int64; seekOrigin: DWORD; newPosition: PInt64 ): Integer; stdcall;
function SetSize( newSize: Int64 ): Integer; stdcall;
end;
// -----------------------------------------------------------------------------
TSevenZip = class; // for reference only, implementated later below
TOpenVolume = procedure( var arcFileName: WideString; Removable: Boolean; out Cancel: Boolean ) of object;
TFiles = record
Name: WideString;
Handle: cardinal; //Integer; //FHO 20.01.2007
Size: Int64;//DWORD; // RG 26.01.2007
OnRemovableDrive: Boolean;
end; //FHO 17.01.2007
TArrayOfFiles = array of TFiles; //FHO 17.01.2007
TMyStreamWriter = class( TInterfacedObject, ISequentialOutStream, IOutStream )
private
arcName: WideString;
arcPosition, arcSize: int64; // DWORD; // RG 26.01.2007
FPLastError:PInteger; //FHO 22.01.2007
MyLastError: Integer; //FHO 22.01.2007
Files: TFiles;
function CreateNewFile: Boolean;
protected
property TheFiles: TFiles read Files;
public
destructor Destroy; override;
constructor Create( PLastError:PInteger;sz: Widestring);
function Write( const Data; Size: DWORD; ProcessedSize: PDWORD ): Integer; stdcall;
function Seek( Offset: Int64; SeekOrigin: DWORD; NewPosition: PInt64 ): Integer; stdcall;
function SetSize( newSize: Int64 ): Integer; stdcall;
end;
TMyStreamReader = class( TInterfacedObject, IInStream, IStreamGetSize, ISequentialInStream )
FSevenZip: TSevenZip;
arcName: WideString;
arcPosition, arcSize: Int64; //DWORD; // RG 26.01.2007
Files: TArrayOfFiles;
FOnOpenVolume: TOpenVolume;
FArchive: Boolean;
MyLastError: Integer; //FHO 22.01.2007
FMultivolume: Boolean;
function BrowseForFile(var Name:WideString): Boolean;
function OpenVolume( Index: Integer ): Boolean;
function OpenNextVolume: Boolean;
function OpenLastVolume: Boolean;
public
constructor Create( Owner: TSevenZip; sz: Widestring; asArchive: Boolean );
destructor Destroy; override;
function Seek( Offset: Int64; SeekOrigin: DWORD; NewPosition: PInt64 ): Integer; stdcall;
function Read( var Data; Size: DWORD; ProcessedSize: PDWORD ): Integer; stdcall;
function GetSize( var Size: Int64 ): Integer; stdcall;
end;
// -----------------------------------------------------------------------------
TMyArchiveExtractCallback = class( TInterfacedObject, IArchiveExtractCallback, ICryptoGetTextPassword )
FSevenzip: TSevenzip;
FExtractDirectory: Widestring;
FProgressFile: Widestring;
FProgressFilePos: int64;
FProgressFileSize: int64;
FLastPos: int64;
FFilestoextract: int64;
FLastFileToExt: Boolean;
FAllFilesExt: Boolean;
FPassword: WideString;
constructor Create( Owner: TSevenZip );
function GetStream( index: DWORD; out outStream: ISequentialOutStream; askExtractMode: DWORD ): Integer; stdcall;
// GetStream OUT: S_OK - OK, S_FALSE - skeep this file
function PrepareOperation( askExtractMode: Integer ): Integer; stdcall;
function SetOperationResult( resultEOperationResult: Integer ): Integer; stdcall;
function SetTotal( total: Int64 ): Integer; stdcall;
function SetCompleted( const completeValue: PInt64 ): Integer; stdcall;
// Shadow 29.11.2006
function CryptoGetTextPassword( var Password: PWideChar ): Integer; stdcall;
end;
TMyArchiveOpenCallback = class( TInterfacedObject, IArchiveOpenCallback, ICryptoGetTextPassword )
FSevenzip: TSevenzip;
FPassword: WideString;
constructor Create( Owner: TSevenZip );
function SetTotal( const files: Int64; const bytes: Int64 ): Integer; stdcall;
function SetCompleted( const files: Int64; const bytes: Int64 ): Integer; stdcall;
function CryptoGetTextPassword( var Password: PWideChar ): Integer; stdcall;
end;
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//--------------END SevenZip Interface--------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//--------------Start SevenZip VCL -------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------
TSevenZip = class( TComponent ) // Twincontrol TComponent
private
FErrCode: Integer;
FLastError:Integer; //FHO 22.01.2007
FHandle: HWND;
FExtrBaseDir: Widestring;
FExtrOutName: Widestring;
FSevenZipFileName: Widestring;
FComment: Widestring;
FRootDir: Widestring;
Ffiles: TWStringList;
FAddOptions: Addopts;
FExtractOptions: Extractopts;
FNumberOfFiles: Integer;
FCompresstype: TCompresstype;
FCompstrength: TCompressStrength;
FLZMAStrength: TLZMAStrength;
FPPMDSize: TPPMDSize;
FPPMDMem: TPPMDMem;
FMainCancel: Boolean; //FHO 25.01.2007
FCreateObject: TCreateObjectFunc;
FOnOpenVolume: TOpenVolume;
FPassword: WideString;
function InternalGetIndexByFilename( FileToExtract:Widestring ): Integer; //ZSA 21.02.2007
procedure SetLastError(const Value: Integer); //FHO 17.01.2007
protected
inA: IInArchive;
public
constructor Create( AOwner: TComponent; ArcType:WideString ); overload;
destructor Destroy; override;
{ Public Properties ( run-time only ) }
property Handle: HWND read fHandle write fHandle;
property ErrCode: Integer read fErrCode write fErrCode;
property LastError:Integer read FLastError write SetLastError;// FLastError;//FHO 22.01.2007
property SevenZipComment: Widestring read Fcomment write FComment;
property Files: TWStringList read Ffiles write ffiles;
{ Public Methods }
function Extract( TestArchive:Boolean=False ): Integer;
function List(q:boolean): Integer;
published
{ Public properties that also show on Object Inspector }
property AddRootDir: Widestring read FRootDir write FRootDir;
property AddOptions: AddOpts read FAddOptions write FAddOptions;
property ExtractOptions: ExtractOpts read FExtractOptions write FExtractOptions;
property ExtrBaseDir: Widestring read FExtrBaseDir write FExtrBaseDir;
property ExtrOutName: Widestring read FExtrOutName write FExtrOutName;
property LZMACompressType: TCompresstype read FCompresstype write FCompresstype;
property LZMACompressStrength: TCompressStrength read FCompstrength write FCompstrength;
property LZMAStrength: TLZMAStrength read FLZMAStrength write FLZMAstrength;
property LPPMDmem: TPPMDmem read FPPMDmem write FPPMDmem;
property LPPMDsize: TPPMDsize read FPPMDsize write FPPMDsize;
property SZFileName: Widestring read FSevenZipFileName write FSevenZipFilename;
property NumberOfFiles: Integer read FNumberOfFiles write FNumberOfFiles;
// Shadow 29.11.2006
property Password: WideString read FPassword write FPassword;
end;
// jjw 18.10.2006 FCreateobject - function CreateObject( const clsid: PGUID; const iid: PGUID; out _out ): Integer; stdcall; external '7za.dll';
//{$IFNDEF DynaLoadDLL}
//function CreateObject( const clsid: PGUID; const iid: PGUID; out _out ): Integer; stdcall; external '7za.dll'
//{$ENDIF}
procedure SortDWord( var A: array of DWord; iLo, iHi: DWord );
function DriveIsRemovable( Drive: WideString ): Boolean;
//Unicode procedures
function GetFileSizeandDateTime_Int64( fn: Widestring; var fs:int64; var ft:Tfiletime; var fa:Integer ): int64;
function FileExists_( fn: Widestring ): Boolean;
implementation
uses
Forms, SevenZip;
//--------------------------------------------------------------------------------------------------
//-------------------Start UniCode procedures-------------------------------------------------------
//--------------------------------------------------------------------------------------------------
function FileExists_( fn: Widestring ): Boolean;
var
fs:int64;
ft:Tfiletime;
fa:Integer;
begin
if Win32PlatformIsUnicode then
Result := ( GetFileSizeandDateTime_Int64( fn,fs,ft,fa ) > -1 )
else
Result := fileexists(string(fn));
end;
function PrevDir( Path: WideString ): WideString;
var
l: Integer;
begin
l := Length( Path );
if ( l > 0 ) and ( Path[ l ] = '\' ) then Dec( l );
while Path[ l ] <> '\' do Dec( l );
Result := Copy( Path, 1, l );
end;
function GetFileSizeandDateTime_Int64( fn: Widestring; var fs:int64; var ft:Tfiletime; var fa:Integer ): int64;
var
FindDataW: _Win32_Find_Dataw;
FindDataA: _Win32_Find_DataA;
SearchHandle: THandle;
begin
//Result := 0;
if Win32PlatformIsUnicode then
SearchHandle := FindFirstFilew( PWideChar( fn ), FindDataW )
else
SearchHandle := FindFirstFile( PAnsiChar( Ansistring( fn ) ), FindDataA );
if SearchHandle = INVALID_HANDLE_VALUE then
begin
Result := -1;
fs := -1;
fa := -1;
ft.dwLowDateTime := 0;
ft.dwHighDateTime := 0;
exit;
end;
if Win32PlatformIsUnicode then
begin
LARGE_Integer( Result ).LowPart := FindDataW.nFileSizeLow;
LARGE_Integer( Result ).HighPart := FindDataW.nFileSizeHigh;
LARGE_Integer( fs ).LowPart := FindDataW.nFileSizeLow;
LARGE_Integer( fs ).HighPart := FindDataW.nFileSizeHigh;
ft.dwLowDateTime := FinddataW.ftLastWriteTime.dwLowDateTime;
ft.dwHighDateTime := FinddataW.ftLastWriteTime.dwHighDateTime;
fa := FinddataW.dwFileAttributes;
end
else
begin
LARGE_Integer( Result ).LowPart := FindDataA.nFileSizeLow;
LARGE_Integer( Result ).HighPart := FindDataA.nFileSizeHigh;
LARGE_Integer( fs ).LowPart := FindDataA.nFileSizeLow;
LARGE_Integer( fs ).HighPart := FindDataA.nFileSizeHigh;
ft.dwLowDateTime := FinddataA.ftLastWriteTime.dwLowDateTime;
ft.dwHighDateTime := FinddataA.ftLastWriteTime.dwHighDateTime;
fa := FinddataA.dwFileAttributes;
end;
Windows.FindClose( SearchHandle );
end;
function ForceDirectoriesW( Path: WideString; Attr: Word ): Boolean;
begin
Result := TRUE;
if Path = '' then exit;
Path := WideExcludeTrailingBackslash( Path );
if WideDirectoryExists( Path ) then Exit;
if ( Length( Path ) < 3 ) or WideDirectoryExists(Path)
or ( ExtractFilePath( Path ) = Path ) then Exit; // avoid 'xyz:\' problem.
Result := ForceDirectoriesW( PrevDir( Path ), 0 ) and CreateDirectoryW( PWideChar( Path ), nil );
if Result and ( Attr > 0 ) then SetFileAttributesW( PWideChar( Path ), Attr );
end;
//--------------------------------------------------------------------------------------------------
//-------------------End UniCode procedures---------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
// Start common functions
//------------------------------------------------------------------------------------------------
//some Delphi veriosn do not take the Int64 overload
function FileSeek(Handle: Integer; const Offset: Int64; Origin: Integer): Int64;
begin
Result := Offset;
Int64Rec(Result).Lo := SetFilePointer(THandle(Handle), Int64Rec(Result).Lo,@Int64Rec(Result).Hi, Origin);
end;
procedure SortDWord(var A:array of DWord; iLo,iHi:DWord);
var Lo,Hi,Mid,T:DWord;
begin
Lo:=iLo;
Hi:=iHi;
Mid:=A[(Lo+Hi) div 2];
repeat
while A[Lo]<Mid do Inc(Lo);
while A[Hi]>Mid do Dec(Hi);
if Lo<=Hi then begin
T:=A[Lo];
A[Lo]:=A[Hi];
A[Hi]:=T;
Inc(Lo);
if Hi>0 then Dec(Hi); //Using DWord and not Integers
end;
until Lo>Hi;
if Hi>iLo then SortDWord(A,iLo,Hi);
if Lo<iHi then SortDWord(A,Lo,iHi);
end;
function DriveIsRemovable( Drive: WideString ): Boolean;
var
DT: Cardinal;
begin
DT := GetDriveTypeW( PWideChar( Drive ) );
Result := ( DT <> DRIVE_FIXED );
end;
//------------------------------------------------------------------------------------------------
// End common functions
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
//-------------------Start SevenZip Interface -----------------------------------------------
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
function TInterfacedObject.QueryInterface( const IID: TGUID; out Obj ): HResult;
const E_NOINTERFACE = HResult( $80004002 );
begin
if GetInterface( IID, Obj ) then Result:=0
else Result := E_NOINTERFACE;
end;
function TInterfacedObject._AddRef: Integer;
begin
Result := InterlockedIncrement( FRefCount );
end;
function TInterfacedObject._Release: Integer;
begin
Result := InterlockedDecrement( FRefCount );
if Result = 0 then Destroy;
end;
procedure TInterfacedObject.AfterConstruction;
begin
// Release the constructor's implicit refcount
InterlockedDecrement( FRefCount );
end;
procedure TInterfacedObject.BeforeDestruction;
begin
//if RefCount <> 0 then Error( reInvalidPtr );
end;
// Set an implicit refcount so that refcounting
// during construction won't destroy the object.
class function TInterfacedObject.NewInstance: TObject;
begin
Result := inherited NewInstance;
TInterfacedObject( Result ).FRefCount := 1;
end;
constructor TMyArchiveExtractCallback.Create( Owner: TSevenZip );
begin
inherited Create;
FSevenzip := Owner;
// Shadow 29.11.2006
if Assigned( FSevenzip ) then
FPassword := FSevenzip.Password
else FPassword := '';
end;
function TMyArchiveExtractCallback.GetStream(index:DWORD;
out outStream:ISequentialOutStream; askExtractMode:DWORD):Integer; stdcall;
var path:Propvariant; sz:Widestring; // fHnd: Integer;
MyLastError:Integer; //FHO 22.01.2007
begin
path.vt:=VT_EMPTY;
if self.FSevenzip.FMainCancel or (not procArc) then
begin
outStream := nil;
result := S_FALSE;
exit;
end;
Case askExtractMode of
kExtract: begin
FSevenzip.inA.GetProperty(index,kpidPath,path);
if ExtractNoPath in FSevenzip.FExtractOptions then begin
if FSevenzip.FExtrOutName='' then
sz:=FExtractDirectory + WideExtractFileName(path.bstrVal)
else sz:=FExtractDirectory + FSevenzip.FExtrOutName;
end
else begin
if FSevenzip.FExtrOutName='' then
sz:=FExtractDirectory + path.bstrVal
else
sz:=FExtractDirectory + WideExtractFileDir(path.bstrVal) + FSevenzip.FExtrOutName;
end;
dec(FFilestoextract);
if FFilestoextract=0 then FLastFileToExt:=true;
outStream := nil;
if Win32PlatformIsUnicode then
ForceDirectoriesW(WideExtractFilePath(sz),FILE_ATTRIBUTE_NORMAL)
else
ForceDirectories(extractfilepath(String(sz)));
try
outStream:=TMyStreamWriter.Create(@MyLastError,sz);
except
outStream := nil;
Result := S_FALSE;
Exit;
end;
end;
ktest: FSevenzip.inA.GetProperty( index, kpidPath, path );
kskip: begin
end;
end;
Result := S_OK;
end;
// GetStream OUT: S_OK - OK, S_FALSE - skeep this file
function TMyArchiveExtractCallback.PrepareOperation( askExtractMode: Integer ): Integer; stdcall;
begin
Result := S_OK;
end;
function TMyArchiveExtractCallback.SetOperationResult( resultEOperationResult: Integer ): Integer; stdcall;
begin
Result := S_OK;
case resultEOperationResult of
kOK : FSevenzip.ErrCode:=FNoError;
kUnSupportedMethod: begin //FHO 21.01.2007
FSevenzip.ErrCode:=FUnsupportedMethod;
end;
kDataError : begin //FHO 21.01.2007
FSevenzip.ErrCode:=FDataError;
end;
kCRCError : begin //FHO 21.01.2007
FSevenzip.ErrCode:=FCRCError;
end;
end;
if FLastFileToExt then FAllFilesExt := true; //no more files to extract, we can stop
end;
function TMyArchiveExtractCallback.SetTotal( total: Int64 ): Integer; stdcall;
begin
//all filesizes also skipped ones
//if FFilestoextract = 0 then // we extract all files, so we set FMaxProgress here
Result := S_OK;
//else Result:=S_FAlSE;
end;
function TMyArchiveExtractCallback.SetCompleted( const completeValue: PInt64 ): Integer; stdcall;
begin
if ( FProgressFilePos = 0 ) then
FProgressFilePos := FProgressFilePos + ( completeValue^ - FLastPos );
FLastPos := completeValue^;
//full and file progress position
Result := S_OK;
//have all files extracted. Could stop
if self.FAllFilesExt then Result := S_FALSE;
//User cancel operation
if Fsevenzip.FMainCancel or (not procArc) then begin
Result := S_FALSE;
FSevenzip.ErrCode:=FUsercancel; //FHO 21.01.2007
end;
end;
function TMyArchiveExtractCallback.CryptoGetTextPassword( var Password: PWideChar ): Integer;
begin
if Length( FPassword ) > 0 then begin
Password:=SysAllocString(PWChar(FPassword));
Result := S_OK;
end else Result := S_FALSE;
end;
{============ TMyOpenarchiveCallbackReader =================================================}
function TMyArchiveOpenCallback.CryptoGetTextPassword( var Password: PWideChar ): Integer;
begin
if FPassword='' then WideInputQuery(LOCstr_SetPW_Caption,FSevenzip.SZFileName,FPassword);
if Length( FPassword ) > 0 then begin
Password:=SysAllocString(PWChar(FPassword));
FSevenZip.Password:=Password;
Result := S_OK;
end else Result := S_FALSE;
TmpPW:=FPassword;
end;
constructor TMyArchiveOpenCallback.Create( Owner: TSevenZip );
begin
inherited Create;
FSevenzip := Owner;
// Shadow 29.11.2006
if Assigned( FSevenzip ) then
FPassword := FSevenzip.Password
else FPassword := '';
end;
function TMyArchiveOpenCallback.SetTotal( const files: Int64; const bytes: Int64 ): Integer; stdcall;
begin
Result := S_OK; //LifePower 07.01.2007
end;
function TMyArchiveOpenCallback.SetCompleted( const files: Int64; const bytes: Int64 ): Integer; stdcall;
begin
Result := S_OK;
end;
{============ TMyStreamReader =================================================}
function TMyStreamReader.Seek( Offset: Int64; SeekOrigin: DWORD; NewPosition: PInt64 ): Integer; stdcall;
begin
Result := S_OK;
case SeekOrigin of
soFromBeginning: arcPosition := Offset;
soFromCurrent: arcPosition := arcPosition + Offset;
soFromEnd: begin
if arcSize > 0 then
arcPosition := arcSize + Offset
else Result := S_FALSE;
end;
end;
if newPosition <> nil then newPosition^ := arcPosition;
end;
function TMyStreamReader.Read( var Data; Size: DWORD; ProcessedSize: PDWORD ): Integer; stdcall;
var fIdx:Integer; fPos:Int64; pSize,Read:DWORD; Vsize:Int64; Buff:PChar;
begin
if FArchive then begin
if ( Length( Files ) <= 1 ) and ( arcPosition + Size > Files[ 0 ].Size ) then begin
arcSize := arcPosition + Size;
if not OpenLastVolume then begin
Result := S_FALSE;
Exit;
end else FMultivolume := TRUE;
end;
end;
if ( not FArchive ) or ( not FMultivolume ) then begin
FileSeek( Files[ 0 ].Handle, arcPosition, soFromBeginning );
if not ReadFile( Files[ 0 ].Handle, Data, Size, pSize, nil ) then begin
MyLastError:=GetLastError; //FHO 22.01.2007
pSize := 0;
end;
Inc( arcPosition, pSize );
if ProcessedSize <> nil then ProcessedSize^ := pSize;
Result := S_OK;
Exit;
end;
fIdx := -1;
vSize := 0;
repeat
Inc( fIdx );
if ( not OpenVolume( fIdx + 1 ) ) and ( Files[ fIdx ].Handle = INVALID_HANDLE_VALUE ) then begin //FHO 20.01.2007
Result := S_FALSE;
Exit;
end;
vSize := vSize + Files[ fIdx ].Size;
until arcPosition < vSize;
Buff := @Data;
fPos := arcPosition - ( vSize - Files[ fIdx ].Size );
Read := 0;
while Read < Size do begin
if Read > 0 then begin
with Files[ fIdx - 1 ] do begin
FileClose( Handle );
Handle := INVALID_HANDLE_VALUE; //FHO 20.01.2007
Size := 0;
end;
if ( not OpenVolume( fIdx + 1 ) ) and ( Files[ fIdx ].Handle = INVALID_HANDLE_VALUE ) then begin //FHO 20.01.2007
Result := S_FALSE;
Exit;
end;
end;
FileSeek( Files[ fIdx ].Handle, fPos, soFromBeginning );
pSize := Size - Read;
if Files[ fIdx ].Size < fPos + pSize then pSize := Files[ fIdx ].Size - fPos;
if not ReadFile( Files[ fIdx ].Handle, Buff[ Read ], pSize, pSize, nil ) then begin
MyLastError:=GetLastError; //FHO 22.01.2007
Read := 0;
Break;
end;
Inc( Read, pSize );
Inc( fIdx );
fPos := 0;
end;
Inc( arcPosition, Read );
if Assigned( ProcessedSize ) then ProcessedSize^ := Read;
if MyLastError=0 then
Result := S_OK
else
Result := S_False;
end;
function TMyStreamReader.GetSize(var size:Int64):Integer; stdcall;
begin
if arcSize>0 then begin
Size:=arcSize; Result:=S_OK;
end
else Result:=S_FALSE;
end;
function TMyStreamReader.BrowseForFile(var Name:WideString): Boolean;
begin
if WidePromptForFileName(Name,'Any file(*.*)|*.*','',LOCstr_VolAsk_Caption,WideIncludeTrailingPathDelimiter(WideExtractFilePath(Name))) then
Result:=GetLastError=0
else Result:=FALSE;
end;
function TMyStreamReader.OpenVolume(Index:Integer):Boolean;
var i:Integer; s:WideString; fCancel:Boolean;
begin
Result:=FALSE;
if Index<=0 then Exit
else if Index<=Length(Files) then begin
if Files[Index-1].Handle<>INVALID_HANDLE_VALUE then begin //FHO 20.01.2007
Result:=TRUE;
Exit;
end;
end
else begin
i:=Length(Files);
while i<Index do begin
SetLength(Files,i+1);
Files[i].Handle:=INVALID_HANDLE_VALUE; //FHO 20.01.2007
Files[i].Size:=0;
Inc(i);
end;
end;
Dec(Index);
if Length(Files[Index].Name)<=0 then begin
s:=IntToStr(Index+1);
while Length(s)<3 do s:='0'+s;
// Shadow 28.11.2006
Files[Index].Name:=GetFileName(arcName)+'.'+s;
end;
while Files[Index].Handle=INValid_Handle_Value do begin //FHO 20.01.2007
Files[Index].Handle:=Tnt_CreateFileW(PWChar(Files[Index].Name),GENERIC_READ,FILE_SHARE_READ,nil,OPEN_EXISTING,0,0);
if Files[Index].Handle=INVALID_HANDLE_VALUE then begin //FHO 20.01.2007
if Assigned(FOnOpenVolume) then begin
FOnOpenVolume(Files[Index].Name,Files[Index].OnRemovableDrive,fCancel);
if not fCancel then Continue;
end
else begin
if BrowseForFile(Files[Index].Name) then Continue;
end;
Files[ Index ].Name := '';
Result := FALSE;
Exit;