📄 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}
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 }
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 **************************************}
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: LongInt;
uncompressed_size: LongInt;
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: LongInt;
uncompressed_size: LongInt;
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: LongInt;
end;
centralPtr = ^central_file_header;
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: LongInt;
Funcompressed_size: LongInt;
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: LongInt;
Fcentral_offset: LongInt;
Ffilename: string;
Fdirectory: string;
Ffilecomment: PChar;
FMatchFlag: Boolean;
FFileIsOK: Byte;
FSelected: Boolean;
FOEMConvert: Boolean; { 2/17/02 2.22+ }
protected
function GetHasComment: Boolean;
function GetIsEncrypted: Boolean;
function GetHasDescriptor: Boolean;
function Getfilecomment(S: TStream): PChar;
function GetLocalSize: Integer;
function GetCentralSize: Integer;
procedure Setfilename(FName: string);
procedure Setdirectory(Directory: string);
procedure SetFileComment(FComment: PChar);
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: TStream);
procedure SaveLocalToStream(S: TStream);
function ReadCentralFromStream(var S: TStream; NewDiskEvent: TNewDiskEvent): Boolean;
function ReadLocalFromStream(S: TStream): 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: LongInt read Fcompressed_size write Fcompressed_size;
property uncompressed_size: LongInt read Funcompressed_size write Funcompressed_size;
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 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: LongInt read Frelative_offset write Frelative_offset;
property central_offset: LongInt 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: Boolean read FOEMConvert write FOEMConvert; { 2/17/02 2.22+ }
end;
{**************************** END OF CENTRAL **********************************}
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: LongInt;
offset_central: LongInt;
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: LongInt;
FModified: Boolean;
protected
function GetZipHasComment: Boolean;
function GetZipComment(S: TStream): PChar;
function GetEndCentralSize: LongInt;
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: TStream);
function ReadFromStream(S: TStream): Boolean;
property ID: LongInt read Fecrec.ID write Fecrec.ID;
property this_disk: WORD read Fecrec.this_disk write Fecrec.this_disk;
property start_central_disk: WORD read Fecrec.start_central_disk
write Fecrec.start_central_disk;
property num_entries_this_disk: WORD read Fecrec.num_entries_this_disk
write Fecrec.num_entries_this_disk;
property num_entries: WORD read Fecrec.num_entries write Fecrec.num_entries;
property size_central: LongInt read Fecrec.size_central write Fecrec.size_central;
property offset_central: LongInt read Fecrec.offset_central write
Fecrec.offset_central;
property zip_comment_length: WORD read Fecrec.zip_comment_length
write Fecrec.zip_comment_length;
property ZipHasComment: Boolean read GetZipHasComment;
property ZipCommentPos: LongInt 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;
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;
public
constructor Create(WithDuplicates: TDuplicates);
function Compare(Key1, Key2: Pointer): integer; override;
property SortMode: TZipSortMode read FSortMode write FSortMode default ByNone;
property filesdate: TDateTime read FFilesDate write FFilesDate;
end;
{$IFDEF KPDEMO}
function DRun: Boolean;
{$ENDIF}
procedure setZipSignatures(csig, lsig, esig: LongInt);
implementation {//////////////////////////////////////////////////////////////////////}
uses KpLib;
{***************** TZipHeaderInfo Methods *********************}
constructor TZipHeaderInfo.Create;
begin
inherited Create;
Clear;
end;
destructor TZipHeaderInfo.Destroy;
begin
if (FFileComment <> nil) then
begin
StrDispose(FFileComment);
FFileComment := nil;
end;
end;
procedure TZipHeaderInfo.AssignTo(Dest: TPersistent);
var
finfo: TZipHeaderInfo;
begin
if Dest is TZipHeaderInfo then
begin
finfo := TZipHeaderInfo(Dest);
finfo.version_made_by := version_made_by;
finfo.version_needed_to_extract := version_needed_to_extract;
finfo.general_purpose_bit_flag := general_purpose_bit_flag;
finfo.compression_method := compression_method;
finfo.last_mod_file_date_time := last_mod_file_date_time;
finfo.crc32 := crc32;
finfo.compressed_size := compressed_size;
finfo.uncompressed_size := uncompressed_size;
finfo.filename_length := filename_length;
finfo.Cextra_field_length := Cextra_field_length;
finfo.Lextra_field_length := Lextra_field_length;
finfo.file_comment_length := file_comment_length;
finfo.disk_number_start := disk_number_start;
finfo.internal_file_attributes := internal_file_attributes;
finfo.external_file_attributes := external_file_attributes;
finfo.relative_offset := relative_offset;
finfo.central_offset := central_offset;
finfo.filename := filename;
finfo.directory := directory;
if (file_comment_length > 0) and (filecomment <> nil) then
begin
if finfo.filecomment <> nil then
StrDispose(finfo.filecomment);
finfo.filecomment := StrAlloc(file_comment_length + 1);
StrCopy(finfo.filecomment, filecomment);
end;
finfo.MatchFlag := MatchFlag;
finfo.FileIsOK := FFileIsOK;
finfo.FSelected := FSelected;
end
else
inherited AssignTo(Dest);
end;
procedure TZipHeaderInfo.Assign(Source: TPersistent);
var
finfo: TZipHeaderInfo;
begin
if Source is TZipHeaderInfo then
begin
finfo := TZipHeaderInfo(Source);
Fversion_made_by := finfo.version_made_by;
Fversion_needed_to_extract := finfo.version_needed_to_extract;
Fgeneral_purpose_bit_flag := finfo.general_purpose_bit_flag;
Fcompression_method := finfo.compression_method;
Flast_mod_file_date_time := finfo.last_mod_file_date_time;
Fcrc32 := finfo.crc32;
Fcompressed_size := finfo.compressed_size;
Funcompressed_size := finfo.uncompressed_size;
Ffilename_length := finfo.filename_length;
FCextra_field_length := finfo.Cextra_field_length;
FLextra_field_length := finfo.Lextra_field_length;
Ffile_comment_length := finfo.file_comment_length;
Fdisk_number_start := finfo.disk_number_start;
Finternal_file_attributes := finfo.internal_file_attributes;
Fexternal_file_attributes := finfo.external_file_attributes;
Frelative_offset := finfo.relative_offset;
Fcentral_offset := finfo.central_offset;
filename := finfo.filename;
directory := finfo.directory;
if (finfo.file_comment_length > 0) and (finfo.filecomment <> nil) then
begin
if Ffilecomment <> nil then
StrDispose(Ffilecomment);
Ffilecomment := StrAlloc(file_comment_length + 1);
StrCopy(Ffilecomment, finfo.filecomment);
end;
MatchFlag := finfo.MatchFlag;
FFileIsOK := finfo.FFileIsOK;
FSelected := finfo.FSelected;
end
else
inherited Assign(Source);
end;
constructor TZipHeaderInfo.InitWithCentral(crec: centralPtr; FName: string);
begin
inherited Create;
SetFromCentral(crec, FName);
end;
constructor TZipHeaderInfo.InitWithLocal(lrec: localPtr; FName: string);
begin
inherited Create;
SetFromLocal(lrec, FName);
end;
procedure TZipHeaderInfo.SetFromCentral(crec: centralPtr; FName: string);
begin
Fversion_made_by := crec^.version_made_by;
Fversion_needed_to_extract := crec^.version_needed_to_extract;
Fgeneral_purpose_bit_flag := crec^.general_purpose_bit_flag;
Fcompression_method := crec^.compression_method;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -