📄 kpzipobj.pas
字号:
{ ********************************************************************************** }
{ }
{ COPYRIGHT 1997 Kevin Boylan }
{ Source File: UnZipObj.Pas }
{ Description: VCLUnZip/VCLZip component - native Delphi unzip component. }
{ Date: May 1997 }
{ Author: Kevin Boylan, boylank@bigfoot.com }
{ }
{ }
{ ********************************************************************************** }
unit kpZipObj;
{$P-} { turn off open parameters }
{$R-} { 3/10/98 2.03 }
{$Q-} { 3/10/98 2.03 }
{$B-} { turn off complete boolean eval } { 12/24/98 2.17 }
{$I KPDEFS.INC}
interface
uses
{$IFDEF WIN32}
Windows,
{$ELSE}
WinTypes, WinProcs,
{$ENDIF}
{$IFNDEF INT64STREAMS}
kpHstrms,
{$ENDIF}
SysUtils, Classes, kpCntn;
type
cpltype = array[0..30] of WORD;
cpdtype = array[0..29] of WORD;
{$IFDEF ISBCB}
U_LONG = DWORD;
LongInt = Integer;
Comp = Double;
{$ENDIF}
{$IFDEF ISDELPHI4}
U_LONG = Cardinal;
{$ENDIF}
{$IFDEF ISDELPHI3}
U_LONG = LongInt;
{$ENDIF}
{$IFDEF ISDELPHI2}
U_LONG = LongInt;
{$ENDIF}
{$IFDEF ISDELPHI1}
U_LONG = LongInt;
{$ENDIF}
{$I kpZConst.Pas}
{$I kpZTypes.Pas}
var
CENTSIG: LongInt; { = $02014b50 }
LOCSIG: LongInt; { = $04034b50 }
ENDSIG: LongInt; { = $06054b50 }
ZIP64EOC: LongInt; { = $06064b50 }
ZIP64EOCL: LongInt; { = $07064b50 }
LOC4: Byte; { = $50 Last byte of LOCSIG }
LOC3: Byte; { = $4b 3rd byte of LOCSIG }
LOC2: Byte; { = $03 2nd byte of LOCSIG }
LOC1: Byte; { = $04 1st byte of LOCSIG }
END4: Byte; { = $50 Last byte of ENDSIG }
type
{********************* HEADER INFO **************************************}
THeaderType = (htLocal, htCentral);
TOEMConvert = (oemAlways,oemNever,oemFlexible);
SignatureType = packed record
case Integer of
0: (Sig: LongInt); { $04034b50 }
1: (ID1, ID2: WORD); { $4b50, $0403 }
end;
local_file_header = packed record
Signature: SignatureType;
version_needed_to_extract: WORD;
general_purpose_bit_flag: WORD;
compression_method: WORD;
last_mod_file_date_time: U_LONG;
crc32: U_LONG;
compressed_size: FILE_INT;
uncompressed_size: FILE_INT;
filename_length: WORD;
extra_field_length: WORD;
end;
localPtr = ^local_file_header;
central_file_header = packed record
Signature: SignatureType;
version_made_by: WORD;
version_needed_to_extract: WORD;
general_purpose_bit_flag: WORD;
compression_method: WORD;
last_mod_file_date_time: U_LONG;
crc32: U_LONG;
compressed_size: FILE_INT;
uncompressed_size: FILE_INT;
filename_length: WORD;
extra_field_length: WORD;
file_comment_length: WORD;
disk_number_start: WORD;
internal_file_attributes: WORD;
external_file_attributes: U_LONG;
relative_offset: FILE_INT;
end;
centralPtr = ^central_file_header;
zip64_Extra_Field = packed record
Tag: WORD; { $0001 }
Size: WORD;
Uncompressed_Size: BIGINT;
Compressed_Size: BIGINT;
Relative_Offset: BIGINT;
DiskStart: LongWord;
end;
zip64_Extra_FieldPtr = ^zip64_Extra_Field;
data_descriptor_20 = packed record
crc32: ULONG;
compressed_size: FILE_INT;
uncompressed_size: FILE_INT;
end;
data_descriptor_zip64 = packed record
crc32: ULONG;
compressed_size: BIGINT;
uncompressed_size: BIGINT;
end;
NTFS_extra_field = packed record
Tag: WORD; { $000a }
Size: WORD;
Reserved: LongWord;
Tag1: WORD; { $0001 }
Size1: WORD;
Mtime: BIGINT;
Atime: BIGINT;
Ctime: BIGINT;
end;
NTFS_extra_fieldPtr = ^NTFS_extra_field;
TZipHeaderInfo = class(TPersistent) {****************TZipHeaderInfo******************}
{ This class contains all the information reflected in both the central and local
headers for a particular compressed file within a zip file }
private
{$IFDEF KPDEMO}
DR: Boolean;
{$ENDIF}
Fversion_made_by: WORD;
Fversion_needed_to_extract: WORD;
Fgeneral_purpose_bit_flag: WORD;
Fcompression_method: WORD;
Flast_mod_file_date_time: U_LONG;
Fcrc32: U_LONG;
Fcompressed_size: FILE_INT;
Funcompressed_size: FILE_INT;
Ffilename_length: WORD;
FCextra_field_length: WORD;
FLextra_field_length: WORD;
Ffile_comment_length: WORD;
Fdisk_number_start: WORD;
Finternal_file_attributes: WORD;
Fexternal_file_attributes: U_LONG;
Frelative_offset: FILE_INT;
Fcentral_offset: BIGINT;
Ffilename: string;
Fdirectory: string;
FZip64_Extended: zip64_Extra_FieldPtr;
Ffilecomment: PChar;
FMatchFlag: Boolean;
FFileIsOK: Byte;
FSelected: Boolean;
FOEMConvert: TOEMConvert; { 2/17/02 2.22+ }
FOriginalExtraOffset: BIGINT;
FOriginalCExtra_field_length: WORD;
FOriginalZip64_Extra_length: WORD;
protected
function GetHasComment: Boolean;
function GetIsEncrypted: Boolean;
function GetHasDescriptor: Boolean;
function Getfilecomment(S: TkpStream): PChar;
function GetLocalSize: Integer;
function GetCentralSize: Integer;
procedure Setfilename(FName: string);
procedure Setdirectory(Directory: string);
procedure SetFileComment(FComment: PChar);
function GetCompressedSize: BIGINT;
procedure SetCompressedSize(size: BIGINT);
function GetUnCompressedSize: BIGINT;
procedure SetUnCompressedSize(size: BIGINT);
function GetNewZip64Extended: zip64_Extra_FieldPtr;
function FZip64_Extended_Instance: zip64_Extra_FieldPtr;
procedure GetExtraFields(S: TkpStream);
function GetRelativeOffset: BIGINT;
procedure SetRelativeOffset(offset: BIGINT);
function GetDisk_number_start: LongWord;
procedure SetDisk_number_start(disk: LongWord);
function Zip64ExtraSize(ht: THeaderType): Word;
procedure WriteZip64Extra(S: TkpStream; ht: THeaderType);
procedure ToOEM(var fname: string); { 2/17/02 2.22+ }
procedure FromOEM(var fname: string); { 2/17/02 2.22+ }
public
constructor Create;
constructor InitWithCentral(crec: centralPtr; FName: string);
constructor InitWithLocal(lrec: localPtr; FName: string);
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
procedure AssignTo(Dest: TPersistent); override;
procedure SetFromCentral(crec: centralPtr; FName: string);
procedure SetFromLocal(lrec: localPtr; FName: string);
procedure Clear;
procedure SaveCentralToStream(S: TkpStream; TempFile: TkpStream; UsingTemp: Boolean);
procedure SaveLocalToStream(S: TkpStream);
function WriteDataDescriptor(S: TkpStream): Boolean;
function ReadCentralFromStream(var S: TkpStream; NewDiskEvent: TNewDiskEvent):
Boolean;
function ReadLocalFromStream(S: TkpStream): Boolean;
procedure SetDateTime(DateTime: TDateTime);
procedure SetNewFileComment(NewComment: string);
property version_made_by: WORD read Fversion_made_by write Fversion_made_by;
property version_needed_to_extract: WORD read Fversion_needed_to_extract
write Fversion_needed_to_extract;
property general_purpose_bit_flag: WORD read Fgeneral_purpose_bit_flag
write Fgeneral_purpose_bit_flag;
property compression_method: WORD read Fcompression_method write Fcompression_method;
property last_mod_file_date_time: U_LONG read Flast_mod_file_date_time
write Flast_mod_file_date_time;
property crc32: U_LONG read Fcrc32 write Fcrc32;
property compressed_size: BIGINT read GetCompressedSize write SetCompressedSize;
property uncompressed_size: BIGINT read GetUnCompressedSize write
SetUnCompressedSize;
property filename_length: WORD read Ffilename_length write Ffilename_length;
property Cextra_field_length: WORD read FCextra_field_length write
FCextra_field_length;
property Lextra_field_length: WORD read FLextra_field_length write
FLextra_field_length;
property file_comment_length: WORD read Ffile_comment_length write
Ffile_comment_length;
// property disk_number_start: WORD read Fdisk_number_start write Fdisk_number_start;
property disk_number_start: LongWord read GetDisk_number_start write
SetDisk_number_start;
property internal_file_attributes: WORD read Finternal_file_attributes
write Finternal_file_attributes;
property external_file_attributes: U_LONG read Fexternal_file_attributes
write Fexternal_file_attributes;
// property relative_offset: FILE_INT read Frelative_offset write Frelative_offset;
property relative_offset: BIGINT read GetRelativeOffset write SetRelativeOffset;
property central_offset: BIGINT read FCentral_Offset write FCentral_Offset;
property filename: string read Ffilename write Setfilename;
property directory: string read Fdirectory write Setdirectory;
property MatchFlag: Boolean read FMatchFlag write FMatchFlag;
property HasComment: Boolean read GetHasComment;
property Encrypted: Boolean read GetIsEncrypted;
property HasDescriptor: Boolean read GetHasDescriptor;
property filecomment: PChar read FFilecomment write SetFileComment;
property LocalSize: Integer read GetLocalSize;
property CentralSize: Integer read GetCentralSize;
property FileIsOK: Byte read FFileIsOK write FFileIsOK;
property Selected: Boolean read FSelected write FSelected;
property OEMConvert: TOEMConvert read FOEMConvert write FOEMConvert; { 2/17/02 2.22+ }
end;
{**************************** END OF CENTRAL **********************************}
zip64_end_of_central = packed record
ID: LongInt; { $06064b50}
size: BIGINT;
version_made_by: WORD;
version_needed: WORD;
this_disk: LongWord;
start_central_disk: LongWord;
num_entries_this_disk: BIGINT;
num_entries: BIGINT;
size_central: BIGINT;
offset_central: BIGINT;
end;
zip64_end_of_centralPtr = ^zip64_end_of_central;
zip64_end_of_central_locator = packed record
ID: LongInt; { $07064b50}
num_disk_start_zip64_end_central: FILE_INT;
offset_zip64_end_central: BIGINT;
num_disks: FILE_INT;
end;
zip64_end_of_central_locatorPtr = ^zip64_end_of_central_locator;
end_of_central = packed record
ID: LongInt; { $06054b50 }
this_disk: WORD;
start_central_disk: WORD;
num_entries_this_disk: WORD;
num_entries: WORD;
size_central: LongWord;
offset_central: FILE_INT;
zip_comment_length: WORD;
end;
end_of_centralPtr = ^end_of_central;
TEndCentral = class(TPersistent) {********************TEndCentral******************}
{ This class contains all information contained in the end of central record
for a zip file, plus some other pertinent information }
private
Fecrec: end_of_central;
FZipComment: PChar;
FZipCommentPos: BIGINT;
FModified: Boolean;
FZip64EOC: zip64_end_of_centralPtr;
FZip64EOCL: zip64_end_of_central_locatorPtr;
protected
function GetZipHasComment: Boolean;
function GetZipComment(S: TkpStream): PChar;
function GetEndCentralSize: LongInt;
function GetThis_Disk: LongWord;
procedure SetThis_Disk(disk: LongWord);
function GetStart_Central_Disk: LongWord;
procedure SetStart_Central_Disk(disk: LongWord);
function GetNum_Entries_This_Disk: LongWord;
procedure SetNum_Entries_This_Disk(entries: LongWord);
function GetNum_Entries: BIGINT;
procedure SetNum_Entries(entries: BIGINT);
function GetSize_Central: BIGINT;
procedure SetSize_Central(size: BIGINT);
function GetOffset_Central: BIGINT;
procedure SetOffset_Central(offset: BIGINT);
function Getnum_disk_start_zip64_end_central: LongWord;
procedure Setnum_disk_start_zip64_end_central(numDisk: LongWord);
function Getoffset_zip64_end_central: BIGINT;
procedure Setoffset_zip64_end_central(offset: BIGINT);
function Getnum_disks: LongWord;
procedure Setnum_disks(numDisks: LongWord);
function GetNewFZip64EOC: zip64_end_of_centralPtr;
function GetNewFZip64EOCL: zip64_end_of_central_locatorPtr;
function FZip64EOC_Instance: zip64_end_of_centralPtr;
function FZip64EOCL_Instance: zip64_end_of_central_locatorPtr;
property ecrec: end_of_central read Fecrec write Fecrec;
public
constructor Create;
destructor Destroy; override;
procedure SetNewZipComment(NewComment: string);
procedure Assign(Source: TPersistent); override;
procedure AssignTo(Dest: TPersistent); override;
procedure SetFromEndCentral(crec: end_of_centralPtr);
procedure Clear;
procedure SaveToStream(S: TkpStream);
function ReadFromStream(S: TkpStream): Boolean;
property ID: LongInt read Fecrec.ID write Fecrec.ID;
property this_disk: LongWord read GetThis_Disk write SetThis_Disk;
property start_central_disk: LongWord read GetStart_Central_Disk
write SetStart_Central_Disk;
property num_entries_this_disk: LongWord read GetNum_Entries_This_Disk
write SetNum_Entries_This_Disk;
property num_entries: BIGINT read GetNum_Entries write SetNum_Entries;
property size_central: BIGINT read GetSize_Central write SetSize_Central;
property offset_central: BIGINT read GetOffset_Central write SetOffset_Central;
property zip_comment_length: WORD read Fecrec.zip_comment_length
write Fecrec.zip_comment_length;
property ZipHasComment: Boolean read GetZipHasComment;
property ZipCommentPos: BIGINT read FZipCommentPos write FZipCommentPos;
property ZipComment: PChar read FZipComment write FZipComment;
property Modified: Boolean read FModified write FModified default False;
property EndCentralSize: LongInt read GetEndCentralSize;
property num_disk_start_zip64_end_central: LongWord read
Getnum_disk_start_zip64_end_central write
Setnum_disk_start_zip64_end_central;
property offset_zip64_end_central: BIGINT read Getoffset_zip64_end_central write
Setoffset_zip64_end_central;
property num_disks: LongWord read Getnum_disks write Setnum_disks;
end;
{******************************* ZIP SORTING ***************************************}
TZipSortMode = (ByName, ByFileName, ByDirectoryName, ByDate, ByCompressedSize,
ByUnCompressedSize, ByRate, ByNone);
TSortedZip = class(TSortedObjectList)
{ This class holds a sorted collection of TZipHeaderInfo objects }
private
FSortMode: TZipSortMode;
FFilesDate: TDateTime;
FIsZip64: Boolean;
public
constructor Create(WithDuplicates: TDuplicates);
Function AddObject(Item: tObject): Integer; override;
function Compare(Key1, Key2: Pointer): integer; override;
property SortMode: TZipSortMode read FSortMode write FSortMode default ByNone;
property filesdate: TDateTime read FFilesDate write FFilesDate;
property isZip64: Boolean read FIsZip64;
end;
{$IFDEF KPDEMO}
function DRun: Boolean;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -