| 1 |
(* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Version: MPL 1.1 |
| 3 |
* |
| 4 |
* The contents of this file are subject to the Mozilla Public License Version |
| 5 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 6 |
* the License. You may obtain a copy of the License at |
| 7 |
* http://www.mozilla.org/MPL/ |
| 8 |
* |
| 9 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 |
* for the specific language governing rights and limitations under the |
| 12 |
* License. |
| 13 |
* |
| 14 |
* The Original Code is TurboPower Abbrevia |
| 15 |
* |
| 16 |
* The Initial Developer of the Original Code is |
| 17 |
* TurboPower Software |
| 18 |
* |
| 19 |
* Portions created by the Initial Developer are Copyright (C) 1997-2002 |
| 20 |
* the Initial Developer. All Rights Reserved. |
| 21 |
* |
| 22 |
* Contributor(s): |
| 23 |
* Craig Peterson <[email protected]> |
| 24 |
* |
| 25 |
* ***** END LICENSE BLOCK ***** *) |
| 26 |
|
| 27 |
{*********************************************************} |
| 28 |
{* ABBREVIA: AbZipTyp.pas *} |
| 29 |
{*********************************************************} |
| 30 |
{* ABBREVIA: PKZip types *} |
| 31 |
{* Based on information from Appnote.txt, shipped with *} |
| 32 |
{* PKWare's PKZip for Windows 2.5 *} |
| 33 |
{*********************************************************} |
| 34 |
|
| 35 |
unit AbZipTyp; |
| 36 |
|
| 37 |
{$I AbDefine.inc} |
| 38 |
|
| 39 |
interface |
| 40 |
|
| 41 |
uses |
| 42 |
Classes, AbArcTyp, AbUtils, AbSpanSt; |
| 43 |
|
| 44 |
const |
| 45 |
{ note #$50 = 'P', #$4B = 'K'} |
| 46 |
Ab_ZipVersion = 63; |
| 47 |
Ab_ZipLocalFileHeaderSignature : Longint = $04034B50; |
| 48 |
Ab_ZipDataDescriptorSignature : Longint = $08074B50; |
| 49 |
Ab_ZipCentralDirectoryFileHeaderSignature : Longint = $02014B50; |
| 50 |
Ab_Zip64EndCentralDirectorySignature : Longint = $06064B50; |
| 51 |
Ab_Zip64EndCentralDirectoryLocatorSignature:Longint = $07064B50; |
| 52 |
Ab_ZipEndCentralDirectorySignature : Longint = $06054B50; |
| 53 |
Ab_ZipSpannedSetSignature : Longint = $08074B50; |
| 54 |
Ab_ZipPossiblySpannedSignature : Longint = $30304B50; |
| 55 |
Ab_GeneralZipSignature : Word = $4B50; |
| 56 |
|
| 57 |
Ab_ArchiveExtraDataRecord : Longint = $08064B50; |
| 58 |
Ab_DigitalSignature : Longint = $05054B50; |
| 59 |
|
| 60 |
Ab_WindowsExeSignature : Word = $5A4D; |
| 61 |
Ab_LinuxExeSignature : Longint = $464C457F; |
| 62 |
|
| 63 |
AbDefZipSpanningThreshold = 0; |
| 64 |
AbDefPasswordRetries = 3; |
| 65 |
AbFileIsEncryptedFlag = $0001; |
| 66 |
AbHasDataDescriptorFlag = $0008; |
| 67 |
AbLanguageEncodingFlag = $0800; |
| 68 |
|
| 69 |
Ab_Zip64SubfieldID : Word = $0001; |
| 70 |
Ab_NTFSSubfieldID : Word = $000A; |
| 71 |
Ab_InfoZipTimestampSubfieldID : Word = $5455; |
| 72 |
Ab_InfoZipUnicodePathSubfieldID : Word = $7075; |
| 73 |
Ab_XceedUnicodePathSubfieldID : Word = $554E; |
| 74 |
Ab_XceedUnicodePathSignature : LongWord= $5843554E; |
| 75 |
|
| 76 |
type |
| 77 |
TNtfsTimeField = packed record |
| 78 |
Reserved : UInt32; |
| 79 |
Tag : UInt16; |
| 80 |
Size : UInt16; |
| 81 |
Mtime : UInt64; |
| 82 |
Atime : UInt64; |
| 83 |
Ctime : UInt64; |
| 84 |
end; |
| 85 |
PNtfsTimeField = ^TNtfsTimeField; |
| 86 |
|
| 87 |
TInfoZipTimeField = packed record |
| 88 |
Tag : Byte; |
| 89 |
Mtime : UInt32; |
| 90 |
end; |
| 91 |
PInfoZipTimeField = ^TInfoZipTimeField; |
| 92 |
|
| 93 |
type |
| 94 |
PAbByteArray4K = ^TAbByteArray4K; |
| 95 |
TAbByteArray4K = array[1..4096] of Byte; |
| 96 |
PAbByteArray8K = ^TAbByteArray8K; |
| 97 |
TAbByteArray8K = array[0..8192] of Byte; |
| 98 |
PAbIntArray8K = ^TAbIntArray8K; |
| 99 |
TAbIntArray8K = array[0..8192] of SmallInt; |
| 100 |
|
| 101 |
PAbWordArray = ^TAbWordArray; |
| 102 |
TAbWordArray = array[0..65535 div SizeOf(Word)-1] of Word; |
| 103 |
PAbByteArray = ^TAbByteArray; |
| 104 |
TAbByteArray = array[0..65535-1] of Byte; |
| 105 |
PAbSmallIntArray = ^TAbSmallIntArray; |
| 106 |
TAbSmallIntArray = array[0..65535 div SizeOf(SmallInt)-1] of SmallInt; |
| 107 |
|
| 108 |
PAbIntegerArray = ^TAbIntegerArray; |
| 109 |
TAbIntegerArray = array[0..65535 div sizeof(integer)-1] of integer; |
| 110 |
|
| 111 |
TAbZip64EndOfCentralDirectoryRecord = packed record |
| 112 |
Signature : Longint; |
| 113 |
RecordSize : Int64; |
| 114 |
VersionMadeBy : Word; |
| 115 |
VersionNeededToExtract : Word; |
| 116 |
DiskNumber : LongWord; |
| 117 |
StartDiskNumber : LongWord; |
| 118 |
EntriesOnDisk : Int64; |
| 119 |
TotalEntries : Int64; |
| 120 |
DirectorySize : Int64; |
| 121 |
DirectoryOffset : Int64; |
| 122 |
end; |
| 123 |
|
| 124 |
TAbZip64EndOfCentralDirectoryLocator = packed record |
| 125 |
Signature : Longint; |
| 126 |
StartDiskNumber : Longint; |
| 127 |
RelativeOffset : Int64; |
| 128 |
TotalDisks : Longint; |
| 129 |
end; |
| 130 |
|
| 131 |
TAbZipEndOfCentralDirectoryRecord = packed record |
| 132 |
Signature : Longint; |
| 133 |
DiskNumber : Word; |
| 134 |
StartDiskNumber : Word; |
| 135 |
EntriesOnDisk : Word; |
| 136 |
TotalEntries : Word; |
| 137 |
DirectorySize : LongWord; |
| 138 |
DirectoryOffset : LongWord; |
| 139 |
CommentLength : Word; |
| 140 |
end; |
| 141 |
|
| 142 |
TAbFollower = {used to expand reduced files} |
| 143 |
packed record |
| 144 |
Size : Byte; {size of follower set} |
| 145 |
FSet : array[0..31] of Byte; {follower set} |
| 146 |
end; |
| 147 |
PAbFollowerSets = ^TAbFollowerSets; |
| 148 |
TAbFollowerSets = array[0..255] of TAbFollower; |
| 149 |
|
| 150 |
|
| 151 |
PAbSfEntry = ^TAbSfEntry; |
| 152 |
TAbSfEntry = {entry in a Shannon-Fano tree} |
| 153 |
packed record |
| 154 |
case Byte of |
| 155 |
0 : (Code : Word; Value, BitLength : Byte); |
| 156 |
1 : (L : Longint); |
| 157 |
end; |
| 158 |
PAbSfTree = ^TAbSfTree; |
| 159 |
TAbSfTree = |
| 160 |
packed record {a Shannon-Fano tree} |
| 161 |
Entries : SmallInt; |
| 162 |
MaxLength : SmallInt; |
| 163 |
Entry : array[0..256] of TAbSfEntry; |
| 164 |
end; |
| 165 |
|
| 166 |
PInfoZipUnicodePathRec = ^TInfoZipUnicodePathRec; |
| 167 |
TInfoZipUnicodePathRec = packed record |
| 168 |
Version: Byte; |
| 169 |
NameCRC32: LongInt; |
| 170 |
UnicodeName: array[0..0] of AnsiChar; |
| 171 |
end; |
| 172 |
|
| 173 |
PXceedUnicodePathRec = ^TXceedUnicodePathRec; |
| 174 |
TXceedUnicodePathRec = packed record |
| 175 |
Signature: LongWord; |
| 176 |
Length: Integer; |
| 177 |
UnicodeName: array[0..0] of WideChar; |
| 178 |
end; |
| 179 |
|
| 180 |
PZip64LocalHeaderRec = ^TZip64LocalHeaderRec; |
| 181 |
TZip64LocalHeaderRec = packed record |
| 182 |
UncompressedSize: Int64; |
| 183 |
CompressedSize: Int64; |
| 184 |
end; |
| 185 |
|
| 186 |
type |
| 187 |
TAbZipCompressionMethod = |
| 188 |
(cmStored, cmShrunk, cmReduced1, cmReduced2, cmReduced3, |
| 189 |
cmReduced4, cmImploded, cmTokenized, cmDeflated, |
| 190 |
cmEnhancedDeflated, cmDCLImploded, cmBzip2 = 12, cmLZMA = 14, |
| 191 |
cmIBMTerse = 18, cmLZ77, cmZstd = 93, cmXz = 95, cmJPEG = 96, |
| 192 |
cmWavPack = 97, cmPPMd); |
| 193 |
|
| 194 |
TAbZipSupportedMethod = |
| 195 |
(smStored, smDeflated, smBestMethod); |
| 196 |
|
| 197 |
{ExternalFileAttributes compatibility; aliases are Info-ZIP/PKZIP overlaps} |
| 198 |
TAbZipHostOS = |
| 199 |
(hosDOS, hosAmiga, hosVAX, hosUnix, hosVMCMS, hosAtari, |
| 200 |
hosOS2, hosMacintosh, hosZSystem, hosCPM, hosNTFS, hosTOPS20 = hosNTFS, |
| 201 |
hosMVS, hosWinNT = hosMVS, hosVSE, hosQDOS = hosVSE, hosRISC, |
| 202 |
hosVFAT, hosAltMVS, hosBeOS, hosTandem, hosOS400, hosTHEOS = hosOS400, |
| 203 |
hosDarwin, hosAtheOS = 30); |
| 204 |
|
| 205 |
{for method 6 - imploding} |
| 206 |
TAbZipDictionarySize = |
| 207 |
(dsInvalid, ds4K, ds8K); |
| 208 |
|
| 209 |
{for method 8 - deflating} |
| 210 |
TAbZipDeflationOption = |
| 211 |
(doInvalid, doNormal, doMaximum, doFast, doSuperFast ); |
| 212 |
|
| 213 |
type |
| 214 |
TAbNeedPasswordEvent = procedure(Sender : TObject; |
| 215 |
var NewPassword : AnsiString) of object; |
| 216 |
|
| 217 |
const |
| 218 |
AbDefCompressionMethodToUse = smBestMethod; |
| 219 |
AbDefDeflationOption = doNormal; |
| 220 |
|
| 221 |
|
| 222 |
type |
| 223 |
TAbZipDataDescriptor = class( TObject ) |
| 224 |
protected {private} |
| 225 |
FCRC32 : Longint; |
| 226 |
FCompressedSize : Int64; |
| 227 |
FUncompressedSize : Int64; |
| 228 |
public {methods} |
| 229 |
procedure LoadFromStream( Stream : TStream ); |
| 230 |
procedure SaveToStream( Stream : TStream ); |
| 231 |
public {properties} |
| 232 |
property CRC32 : Longint |
| 233 |
read FCRC32 write FCRC32; |
| 234 |
property CompressedSize : Int64 |
| 235 |
read FCompressedSize write FCompressedSize; |
| 236 |
property UncompressedSize : Int64 |
| 237 |
read FUncompressedSize write FUncompressedSize; |
| 238 |
end; |
| 239 |
|
| 240 |
type |
| 241 |
{ TAbZipFileHeader interface =============================================== } |
| 242 |
{ancestor class for ZipLocalFileHeader and DirectoryFileHeader} |
| 243 |
TAbZipFileHeader = class( TObject ) |
| 244 |
protected {private} |
| 245 |
FValidSignature : Longint; |
| 246 |
FSignature : Longint; |
| 247 |
FVersionNeededToExtract : Word; |
| 248 |
FGeneralPurposeBitFlag : Word; |
| 249 |
FCompressionMethod : Word; |
| 250 |
FLastModFileTime : Word; |
| 251 |
FLastModFileDate : Word; |
| 252 |
FCRC32 : Longint; |
| 253 |
FCompressedSize : LongWord; |
| 254 |
FUncompressedSize : LongWord; |
| 255 |
FFileName : AnsiString; |
| 256 |
FExtraField : TAbExtraField; |
| 257 |
protected {methods} |
| 258 |
function GetCompressionMethod : TAbZipCompressionMethod; |
| 259 |
function GetCompressionRatio : Double; |
| 260 |
function GetDataDescriptor : Boolean; |
| 261 |
function GetDeflationOption : TAbZipDeflationOption; |
| 262 |
function GetDictionarySize : TAbZipDictionarySize; |
| 263 |
function GetEncrypted : Boolean; |
| 264 |
function GetIsUTF8 : Boolean; |
| 265 |
function GetShannonFanoTreeCount : Byte; |
| 266 |
function GetValid : Boolean; |
| 267 |
procedure SetCompressionMethod( Value : TAbZipCompressionMethod ); |
| 268 |
procedure SetIsUTF8( Value : Boolean ); |
| 269 |
public {methods} |
| 270 |
constructor Create; |
| 271 |
destructor Destroy; override; |
| 272 |
public {properties} |
| 273 |
property Signature : Longint |
| 274 |
read FSignature write FSignature; |
| 275 |
property VersionNeededToExtract : Word |
| 276 |
read FVersionNeededToExtract write FVersionNeededToExtract; |
| 277 |
property GeneralPurposeBitFlag : Word |
| 278 |
read FGeneralPurposeBitFlag write FGeneralPurposeBitFlag; |
| 279 |
property CompressionMethod : TAbZipCompressionMethod |
| 280 |
read GetCompressionMethod write SetCompressionMethod; |
| 281 |
property LastModFileTime : Word |
| 282 |
read FLastModFileTime write FLastModFileTime; |
| 283 |
property LastModFileDate : Word |
| 284 |
read FLastModFileDate write FLastModFileDate; |
| 285 |
property CRC32 : Longint |
| 286 |
read FCRC32 write FCRC32; |
| 287 |
property CompressedSize : LongWord |
| 288 |
read FCompressedSize write FCompressedSize; |
| 289 |
property UncompressedSize : LongWord |
| 290 |
read FUncompressedSize write FUncompressedSize; |
| 291 |
property FileName : AnsiString |
| 292 |
read FFileName write FFileName; |
| 293 |
property ExtraField : TAbExtraField |
| 294 |
read FExtraField; |
| 295 |
|
| 296 |
property CompressionRatio : Double |
| 297 |
read GetCompressionRatio; |
| 298 |
property DeflationOption : TAbZipDeflationOption |
| 299 |
read GetDeflationOption; |
| 300 |
property DictionarySize : TAbZipDictionarySize |
| 301 |
read GetDictionarySize; |
| 302 |
property HasDataDescriptor : Boolean |
| 303 |
read GetDataDescriptor; |
| 304 |
property IsValid : Boolean |
| 305 |
read GetValid; |
| 306 |
property IsEncrypted : Boolean |
| 307 |
read GetEncrypted; |
| 308 |
property IsUTF8 : Boolean |
| 309 |
read GetIsUTF8 write SetIsUTF8; |
| 310 |
property ShannonFanoTreeCount : Byte |
| 311 |
read GetShannonFanoTreeCount; |
| 312 |
end; |
| 313 |
|
| 314 |
{ TAbZipLocalFileHeader interface ========================================== } |
| 315 |
TAbZipLocalFileHeader = class( TAbZipFileHeader ) |
| 316 |
public {methods} |
| 317 |
constructor Create; |
| 318 |
destructor Destroy; override; |
| 319 |
procedure LoadFromStream( Stream : TStream ); |
| 320 |
procedure SaveToStream( Stream : TStream ); |
| 321 |
end; |
| 322 |
|
| 323 |
{ TAbZipDirectoryFileHeader interface ====================================== } |
| 324 |
TAbZipDirectoryFileHeader = class( TAbZipFileHeader ) |
| 325 |
protected {private} |
| 326 |
FVersionMadeBy : Word; |
| 327 |
FDiskNumberStart : Word; |
| 328 |
FInternalFileAttributes : Word; |
| 329 |
FExternalFileAttributes : LongWord; |
| 330 |
FRelativeOffset : LongWord; |
| 331 |
FFileComment : AnsiString; |
| 332 |
public {methods} |
| 333 |
constructor Create; |
| 334 |
destructor Destroy; override; |
| 335 |
procedure LoadFromStream( Stream : TStream ); |
| 336 |
procedure SaveToStream( Stream : TStream ); |
| 337 |
public {properties} |
| 338 |
property VersionMadeBy : Word |
| 339 |
read FVersionMadeBy write FVersionMadeBy; |
| 340 |
property DiskNumberStart : Word |
| 341 |
read FDiskNumberStart write FDiskNumberStart; |
| 342 |
property InternalFileAttributes : Word |
| 343 |
read FInternalFileAttributes write FInternalFileAttributes; |
| 344 |
property ExternalFileAttributes : LongWord |
| 345 |
read FExternalFileAttributes write FExternalFileAttributes; |
| 346 |
property RelativeOffset : LongWord |
| 347 |
read FRelativeOffset write FRelativeOffset; |
| 348 |
property FileComment : AnsiString |
| 349 |
read FFileComment write FFileComment; |
| 350 |
end; |
| 351 |
|
| 352 |
{ TAbZipDirectoryFileFooter interface ====================================== } |
| 353 |
TAbZipDirectoryFileFooter = class( TObject ) |
| 354 |
protected {private} |
| 355 |
FDiskNumber : LongWord; |
| 356 |
FStartDiskNumber : LongWord; |
| 357 |
FEntriesOnDisk : Int64; |
| 358 |
FTotalEntries : Int64; |
| 359 |
FDirectorySize : Int64; |
| 360 |
FDirectoryOffset : Int64; |
| 361 |
FZipfileComment : AnsiString; |
| 362 |
function GetIsZip64: Boolean; |
| 363 |
public {methods} |
| 364 |
procedure LoadFromStream( Stream : TStream ); |
| 365 |
procedure LoadZip64FromStream( Stream : TStream ); |
| 366 |
procedure SaveToStream( Stream : TStream; aZip64TailOffset : Int64 = -1 ); |
| 367 |
public {properties} |
| 368 |
property DiskNumber : LongWord |
| 369 |
read FDiskNumber write FDiskNumber; |
| 370 |
property EntriesOnDisk : Int64 |
| 371 |
read FEntriesOnDisk write FEntriesOnDisk; |
| 372 |
property TotalEntries : Int64 |
| 373 |
read FTotalEntries write FTotalEntries; |
| 374 |
property DirectorySize : Int64 |
| 375 |
read FDirectorySize write FDirectorySize; |
| 376 |
property DirectoryOffset : Int64 |
| 377 |
read FDirectoryOffset write FDirectoryOffset; |
| 378 |
property StartDiskNumber : LongWord |
| 379 |
read FStartDiskNumber write FStartDiskNumber; |
| 380 |
property ZipfileComment : AnsiString |
| 381 |
read FZipfileComment write FZipfileComment; |
| 382 |
property IsZip64: Boolean |
| 383 |
read GetIsZip64; |
| 384 |
end; |
| 385 |
|
| 386 |
{ TAbZipItem interface ===================================================== } |
| 387 |
TAbZipItem = class( TAbArchiveItem ) |
| 388 |
protected {private} |
| 389 |
FItemInfo : TAbZipDirectoryFileHeader; |
| 390 |
FDiskNumberStart : LongWord; |
| 391 |
FLFHExtraField : TAbExtraField; |
| 392 |
FRelativeOffset : Int64; |
| 393 |
FDateTime : TDateTime; |
| 394 |
|
| 395 |
protected {methods} |
| 396 |
function GetCompressionMethod : TAbZipCompressionMethod; |
| 397 |
function GetCompressionRatio : Double; |
| 398 |
function GetDeflationOption : TAbZipDeflationOption; |
| 399 |
function GetDictionarySize : TAbZipDictionarySize; |
| 400 |
function GetExtraField : TAbExtraField; |
| 401 |
function GetFileComment : AnsiString; |
| 402 |
function GetGeneralPurposeBitFlag : Word; |
| 403 |
function GetHostOS: TAbZipHostOS; |
| 404 |
function GetInternalFileAttributes : Word; |
| 405 |
function GetRawFileName : AnsiString; |
| 406 |
function GetShannonFanoTreeCount : Byte; |
| 407 |
function GetVersionMadeBy : Word; |
| 408 |
function GetVersionNeededToExtract : Word; |
| 409 |
procedure SaveCDHToStream( Stream : TStream ); |
| 410 |
procedure SaveDDToStream( Stream : TStream ); |
| 411 |
procedure SaveLFHToStream( Stream : TStream ); |
| 412 |
procedure SetCompressionMethod( Value : TAbZipCompressionMethod ); |
| 413 |
procedure SetDiskNumberStart( Value : LongWord ); |
| 414 |
procedure SetFileComment(const Value : AnsiString ); |
| 415 |
procedure SetGeneralPurposeBitFlag( Value : Word ); |
| 416 |
procedure SetHostOS( Value : TAbZipHostOS ); |
| 417 |
procedure SetInternalFileAttributes( Value : Word ); |
| 418 |
procedure SetRelativeOffset( Value : Int64 ); |
| 419 |
procedure SetVersionMadeBy( Value : Word ); |
| 420 |
procedure SetVersionNeededToExtract( Value : Word ); |
| 421 |
procedure UpdateVersionNeededToExtract; |
| 422 |
procedure UpdateZip64ExtraHeader; |
| 423 |
|
| 424 |
protected {redefined property methods} |
| 425 |
function GetCRC32 : Longint; override; |
| 426 |
function GetExternalFileAttributes : LongWord; override; |
| 427 |
function GetIsDirectory: Boolean; override; |
| 428 |
function GetIsEncrypted : Boolean; override; |
| 429 |
function GetLastModFileDate : Word; override; |
| 430 |
function GetLastModFileTime : Word; override; |
| 431 |
function GetNativeFileAttributes : LongInt; override; |
| 432 |
function GetNativeLastModFileTime: Longint; override; |
| 433 |
function GetLastModTimeAsDateTime: TDateTime; override; |
| 434 |
procedure SetCompressedSize( const Value : Int64 ); override; |
| 435 |
procedure SetCRC32( const Value : Longint ); override; |
| 436 |
procedure SetExternalFileAttributes( Value : LongWord ); override; |
| 437 |
procedure SetFileName(const Value : string ); override; |
| 438 |
procedure SetLastModFileDate(const Value : Word ); override; |
| 439 |
procedure SetLastModFileTime(const Value : Word ); override; |
| 440 |
procedure SetUncompressedSize( const Value : Int64 ); override; |
| 441 |
procedure SetLastModTimeAsDateTime(const Value: TDateTime); override; |
| 442 |
|
| 443 |
public {methods} |
| 444 |
constructor Create; |
| 445 |
destructor Destroy; override; |
| 446 |
procedure LoadFromStream( Stream : TStream ); |
| 447 |
|
| 448 |
public {properties} |
| 449 |
property CompressionMethod : TAbZipCompressionMethod |
| 450 |
read GetCompressionMethod |
| 451 |
write SetCompressionMethod; |
| 452 |
property CompressionRatio : Double |
| 453 |
read GetCompressionRatio; |
| 454 |
property DeflationOption : TAbZipDeflationOption |
| 455 |
read GetDeflationOption; |
| 456 |
property DictionarySize : TAbZipDictionarySize |
| 457 |
read GetDictionarySize; |
| 458 |
property DiskNumberStart : LongWord |
| 459 |
read FDiskNumberStart |
| 460 |
write SetDiskNumberStart; |
| 461 |
property ExtraField : TAbExtraField |
| 462 |
read GetExtraField; |
| 463 |
property FileComment : AnsiString |
| 464 |
read GetFileComment |
| 465 |
write SetFileComment; |
| 466 |
property HostOS: TAbZipHostOS |
| 467 |
read GetHostOS |
| 468 |
write SetHostOS; |
| 469 |
property InternalFileAttributes : Word |
| 470 |
read GetInternalFileAttributes |
| 471 |
write SetInternalFileAttributes; |
| 472 |
property GeneralPurposeBitFlag : Word |
| 473 |
read GetGeneralPurposeBitFlag |
| 474 |
write SetGeneralPurposeBitFlag; |
| 475 |
property LFHExtraField : TAbExtraField |
| 476 |
read FLFHExtraField; |
| 477 |
property RawFileName : AnsiString |
| 478 |
read GetRawFileName; |
| 479 |
property RelativeOffset : Int64 |
| 480 |
read FRelativeOffset |
| 481 |
write SetRelativeOffset; |
| 482 |
property ShannonFanoTreeCount : Byte |
| 483 |
read GetShannonFanoTreeCount; |
| 484 |
property VersionMadeBy : Word |
| 485 |
read GetVersionMadeBy |
| 486 |
write SetVersionMadeBy; |
| 487 |
property VersionNeededToExtract : Word |
| 488 |
read GetVersionNeededToExtract |
| 489 |
write SetVersionNeededToExtract; |
| 490 |
end; |
| 491 |
|
| 492 |
{ TAbZipArchive interface ================================================== } |
| 493 |
TAbZipArchive = class( TAbArchive ) |
| 494 |
protected {private} |
| 495 |
FCompressionMethodToUse : TAbZipSupportedMethod; |
| 496 |
FDeflationOption : TAbZipDeflationOption; |
| 497 |
FInfo : TAbZipDirectoryFileFooter; |
| 498 |
FIsExecutable : Boolean; |
| 499 |
FPassword : AnsiString; |
| 500 |
FPasswordRetries : Byte; |
| 501 |
FStubSize : LongWord; |
| 502 |
|
| 503 |
FExtractHelper : TAbArchiveItemExtractEvent; |
| 504 |
FExtractToStreamHelper : TAbArchiveItemExtractToStreamEvent; |
| 505 |
FTestHelper : TAbArchiveItemTestEvent; |
| 506 |
FInsertHelper : TAbArchiveItemInsertEvent; |
| 507 |
FInsertFromStreamHelper : TAbArchiveItemInsertFromStreamEvent; |
| 508 |
FOnNeedPassword : TAbNeedPasswordEvent; |
| 509 |
FOnRequestLastDisk : TAbRequestDiskEvent; |
| 510 |
FOnRequestNthDisk : TAbRequestNthDiskEvent; |
| 511 |
FOnRequestBlankDisk : TAbRequestDiskEvent; |
| 512 |
|
| 513 |
protected {methods} |
| 514 |
procedure DoExtractHelper(Index : Integer; const NewName : string); |
| 515 |
procedure DoExtractToStreamHelper(Index : Integer; aStream : TStream); |
| 516 |
procedure DoTestHelper(Index : Integer); |
| 517 |
procedure DoInsertHelper(Index : Integer; OutStream : TStream); |
| 518 |
procedure DoInsertFromStreamHelper(Index : Integer; OutStream : TStream); |
| 519 |
function GetItem( Index : Integer ) : TAbZipItem; |
| 520 |
function GetZipfileComment : AnsiString; |
| 521 |
procedure PutItem( Index : Integer; Value : TAbZipItem ); |
| 522 |
procedure DoRequestDisk(const AMessage: string; var Abort : Boolean); |
| 523 |
procedure DoRequestLastDisk( var Abort : Boolean ); |
| 524 |
virtual; |
| 525 |
procedure DoRequestNthDisk(Sender: TObject; DiskNumber : Byte; var Abort : Boolean ); |
| 526 |
virtual; |
| 527 |
procedure DoRequestBlankDisk(Sender: TObject; var Abort : Boolean ); |
| 528 |
virtual; |
| 529 |
procedure ExtractItemAt(Index : Integer; const UseName : string); |
| 530 |
override; |
| 531 |
procedure ExtractItemToStreamAt(Index : Integer; aStream : TStream); |
| 532 |
override; |
| 533 |
procedure TestItemAt(Index : Integer); |
| 534 |
override; |
| 535 |
function FixName(const Value : string ) : string; |
| 536 |
override; |
| 537 |
function GetSupportsEmptyFolders: Boolean; |
| 538 |
override; |
| 539 |
procedure LoadArchive; |
| 540 |
override; |
| 541 |
procedure SaveArchive; |
| 542 |
override; |
| 543 |
procedure SetZipfileComment(const Value : AnsiString ); |
| 544 |
|
| 545 |
protected {properties} |
| 546 |
property IsExecutable : Boolean |
| 547 |
read FIsExecutable write FIsExecutable; |
| 548 |
|
| 549 |
public {protected} |
| 550 |
procedure DoRequestImage(Sender: TObject; ImageNumber: Integer; |
| 551 |
var ImageName: string; var Abort: Boolean); |
| 552 |
|
| 553 |
public {methods} |
| 554 |
constructor CreateFromStream( aStream : TStream; const ArchiveName : string ); |
| 555 |
override; |
| 556 |
destructor Destroy; |
| 557 |
override; |
| 558 |
function CreateItem(const SourceFileName : string; |
| 559 |
const ArchiveDirectory : string): TAbArchiveItem; override; |
| 560 |
|
| 561 |
public {properties} |
| 562 |
property CompressionMethodToUse : TAbZipSupportedMethod |
| 563 |
read FCompressionMethodToUse |
| 564 |
write FCompressionMethodToUse; |
| 565 |
property DeflationOption : TAbZipDeflationOption |
| 566 |
read FDeflationOption |
| 567 |
write FDeflationOption; |
| 568 |
property ExtractHelper : TAbArchiveItemExtractEvent |
| 569 |
read FExtractHelper |
| 570 |
write FExtractHelper; |
| 571 |
property ExtractToStreamHelper : TAbArchiveItemExtractToStreamEvent |
| 572 |
read FExtractToStreamHelper |
| 573 |
write FExtractToStreamHelper; |
| 574 |
property TestHelper : TAbArchiveItemTestEvent |
| 575 |
read FTestHelper |
| 576 |
write FTestHelper; |
| 577 |
property InsertHelper : TAbArchiveItemInsertEvent |
| 578 |
read FInsertHelper |
| 579 |
write FInsertHelper; |
| 580 |
property InsertFromStreamHelper : TAbArchiveItemInsertFromStreamEvent |
| 581 |
read FInsertFromStreamHelper |
| 582 |
write FInsertFromStreamHelper; |
| 583 |
property Password : AnsiString |
| 584 |
read FPassword |
| 585 |
write FPassword; |
| 586 |
property PasswordRetries : Byte |
| 587 |
read FPasswordRetries |
| 588 |
write FPasswordRetries |
| 589 |
default AbDefPasswordRetries; |
| 590 |
property StubSize : LongWord |
| 591 |
read FStubSize; |
| 592 |
property ZipfileComment : AnsiString |
| 593 |
read GetZipfileComment |
| 594 |
write SetZipfileComment; |
| 595 |
|
| 596 |
property Items[Index : Integer] : TAbZipItem |
| 597 |
read GetItem |
| 598 |
write PutItem; default; |
| 599 |
|
| 600 |
property SuspiciousLinks : TStringList |
| 601 |
read FSuspiciousLinks; |
| 602 |
|
| 603 |
public {events} |
| 604 |
property OnNeedPassword : TAbNeedPasswordEvent |
| 605 |
read FOnNeedPassword write FOnNeedPassword; |
| 606 |
property OnRequestLastDisk : TAbRequestDiskEvent |
| 607 |
read FOnRequestLastDisk write FOnRequestLastDisk; |
| 608 |
property OnRequestNthDisk : TAbRequestNthDiskEvent |
| 609 |
read FOnRequestNthDisk write FOnRequestNthDisk; |
| 610 |
property OnRequestBlankDisk : TAbRequestDiskEvent |
| 611 |
read FOnRequestBlankDisk write FOnRequestBlankDisk; |
| 612 |
end; |
| 613 |
|
| 614 |
{============================================================================} |
| 615 |
procedure MakeSelfExtracting( StubStream, ZipStream, |
| 616 |
SelfExtractingStream : TStream ); |
| 617 |
{-takes an executable stub, and a .zip format stream, and creates |
| 618 |
a SelfExtracting stream. The stub should create a TAbZipArchive |
| 619 |
passing itself as the file, using a read-only open mode. It should |
| 620 |
then perform operations as needed - like ExtractFiles( '*.*' ). |
| 621 |
This routine updates the RelativeOffset of each item in the archive} |
| 622 |
|
| 623 |
function FindCentralDirectoryTail(aStream : TStream) : Int64; |
| 624 |
|
| 625 |
function VerifyZip(Strm : TStream) : TAbArchiveType; |
| 626 |
|
| 627 |
function VerifySelfExtracting(Strm : TStream) : TAbArchiveType; |
| 628 |
|
| 629 |
function ZipCompressionMethodToString(aMethod: TAbZipCompressionMethod): string; |
| 630 |
|
| 631 |
implementation |
| 632 |
|
| 633 |
uses |
| 634 |
{$IFDEF MSWINDOWS} |
| 635 |
Windows, |
| 636 |
{$ENDIF} |
| 637 |
{$IFDEF LibcAPI} |
| 638 |
Libc, |
| 639 |
{$ENDIF} |
| 640 |
{$IFDEF UnixDialogs} |
| 641 |
{$IFDEF KYLIX} |
| 642 |
QControls, |
| 643 |
QDialogs, |
| 644 |
{$ENDIF} |
| 645 |
{$IFDEF LCL} |
| 646 |
Controls, |
| 647 |
Dialogs, |
| 648 |
{$ENDIF} |
| 649 |
{$ENDIF} |
| 650 |
Math, |
| 651 |
AbResString, |
| 652 |
AbExcept, |
| 653 |
AbVMStrm, |
| 654 |
SysUtils, |
| 655 |
LazUTF8, |
| 656 |
DCOSUtils, |
| 657 |
DCStrUtils, |
| 658 |
DCBasicTypes, |
| 659 |
DCClassesUtf8, |
| 660 |
DCDateTimeUtils, |
| 661 |
DCConvertEncoding; |
| 662 |
|
| 663 |
function VerifyZip(Strm : TStream) : TAbArchiveType; |
| 664 |
{ determine if stream appears to be in PkZip format } |
| 665 |
var |
| 666 |
Empty : TAbZipEndOfCentralDirectoryRecord; |
| 667 |
Footer : TAbZipEndOfCentralDirectoryRecord; |
| 668 |
Sig : LongInt; |
| 669 |
TailPosition : int64; |
| 670 |
StartPos : int64; |
| 671 |
begin |
| 672 |
StartPos := Strm.Position; |
| 673 |
Result := atUnknown; |
| 674 |
try |
| 675 |
Strm.Position := 0; |
| 676 |
if Strm.Read(Sig, SizeOf(Sig)) = SizeOf(Sig) then |
| 677 |
begin |
| 678 |
if (Sig = Ab_ZipSpannedSetSignature) then |
| 679 |
Result := atSpannedZip |
| 680 |
else begin |
| 681 |
{ attempt to find Central Directory Tail } |
| 682 |
TailPosition := FindCentralDirectoryTail( Strm ); |
| 683 |
if TailPosition <> -1 then begin |
| 684 |
{ check Central Directory Signature } |
| 685 |
if (Strm.Read(Footer, SizeOf(Footer)) = SizeOf(Footer)) and |
| 686 |
(Footer.Signature = Ab_ZipEndCentralDirectorySignature) then |
| 687 |
begin |
| 688 |
Empty:= Default(TAbZipEndOfCentralDirectoryRecord); |
| 689 |
Empty.Signature:= Ab_ZipEndCentralDirectorySignature; |
| 690 |
{ check Central Directory Offset } |
| 691 |
if (Footer.DirectoryOffset = High(LongWord)) or |
| 692 |
((Strm.Seek(Footer.DirectoryOffset, soBeginning) = Footer.DirectoryOffset) and |
| 693 |
(Strm.Read(Sig, SizeOf(Sig)) = SizeOf(Sig)) and |
| 694 |
(Sig = Ab_ZipCentralDirectoryFileHeaderSignature)) then |
| 695 |
begin |
| 696 |
if Footer.DiskNumber = 0 then |
| 697 |
Result := atZip |
| 698 |
else |
| 699 |
Result := atSpannedZip; |
| 700 |
end |
| 701 |
{ empty archive } |
| 702 |
else if (Strm.Size = SizeOf(Footer)) and |
| 703 |
(CompareMem(@Footer, @Empty, SizeOf(Footer))) then |
| 704 |
begin |
| 705 |
Result := atZip |
| 706 |
end; |
| 707 |
end; |
| 708 |
end; |
| 709 |
end; |
| 710 |
end; |
| 711 |
except |
| 712 |
on EReadError do |
| 713 |
Result := atUnknown; |
| 714 |
end; |
| 715 |
Strm.Position := StartPos; |
| 716 |
end; |
| 717 |
|
| 718 |
function VerifySelfExtracting(Strm : TStream) : TAbArchiveType; |
| 719 |
{ determine if stream appears to be an executable with appended PkZip data } |
| 720 |
var |
| 721 |
FileSignature : Longint; |
| 722 |
StartPos : Int64; |
| 723 |
IsWinExe, IsLinuxExe : Boolean; |
| 724 |
begin |
| 725 |
StartPos := Strm.Position; |
| 726 |
{ verify presence of executable stub } |
| 727 |
{check file type of stub stream} |
| 728 |
Strm.Position := 0; |
| 729 |
Strm.Read( FileSignature, sizeof( FileSignature ) ); |
| 730 |
|
| 731 |
Result := atSelfExtZip; |
| 732 |
|
| 733 |
{ detect executable type } |
| 734 |
IsLinuxExe := FileSignature = Ab_LinuxExeSignature; |
| 735 |
IsWinExe := LongRec(FileSignature).Lo = Ab_WindowsExeSignature; |
| 736 |
if not (IsWinExe or IsLinuxExe) then |
| 737 |
Result := atUnknown; |
| 738 |
|
| 739 |
{ Check for central directory tail } |
| 740 |
if VerifyZip(Strm) <> atZip then |
| 741 |
Result := atUnknown; |
| 742 |
|
| 743 |
Strm.Position := StartPos; |
| 744 |
end; |
| 745 |
{============================================================================} |
| 746 |
function ZipCompressionMethodToString(aMethod: TAbZipCompressionMethod): string; |
| 747 |
begin |
| 748 |
case aMethod of |
| 749 |
cmStored: |
| 750 |
Result := AbZipStored; |
| 751 |
cmShrunk: |
| 752 |
Result := AbZipShrunk; |
| 753 |
cmReduced1..cmReduced4: |
| 754 |
Result := AbZipReduced; |
| 755 |
cmImploded: |
| 756 |
Result := AbZipImploded; |
| 757 |
cmTokenized: |
| 758 |
Result := AbZipTokenized; |
| 759 |
cmDeflated: |
| 760 |
Result := AbZipDeflated; |
| 761 |
cmEnhancedDeflated: |
| 762 |
Result := AbZipDeflate64; |
| 763 |
cmDCLImploded: |
| 764 |
Result := AbZipDCLImploded; |
| 765 |
cmBzip2: |
| 766 |
Result := AbZipBzip2; |
| 767 |
cmLZMA: |
| 768 |
Result := AbZipLZMA; |
| 769 |
cmIBMTerse: |
| 770 |
Result := AbZipIBMTerse; |
| 771 |
cmLZ77: |
| 772 |
Result := AbZipLZ77; |
| 773 |
cmJPEG: |
| 774 |
Result := AbZipJPEG; |
| 775 |
cmWavPack: |
| 776 |
Result := AbZipWavPack; |
| 777 |
cmPPMd: |
| 778 |
Result := AbZipPPMd; |
| 779 |
else |
| 780 |
Result := Format(AbZipUnknown, [Ord(aMethod)]); |
| 781 |
end; |
| 782 |
end; |
| 783 |
{============================================================================} |
| 784 |
function FindCentralDirectoryTail(aStream : TStream) : Int64; |
| 785 |
{ search end of aStream looking for ZIP Central Directory structure |
| 786 |
returns position in stream if found (otherwise returns -1), |
| 787 |
leaves stream positioned at start of structure or at original |
| 788 |
position if not found } |
| 789 |
const |
| 790 |
MaxBufSize = 256 * 1024; |
| 791 |
var |
| 792 |
StartPos : Int64; |
| 793 |
TailRec : TAbZipEndOfCentralDirectoryRecord; |
| 794 |
Buffer : PAnsiChar; |
| 795 |
Offset : Int64; |
| 796 |
TestPos : PAnsiChar; |
| 797 |
BytesRead : Int64; |
| 798 |
BufSize : Int64; |
| 799 |
CommentLen: integer; |
| 800 |
begin |
| 801 |
{save the starting position} |
| 802 |
StartPos := aStream.Seek(0, soCurrent); |
| 803 |
|
| 804 |
{start off with the majority case: no zip file comment, so the |
| 805 |
central directory tail is the last thing in the stream and it's a |
| 806 |
fixed size and doesn't indicate a zip file comment} |
| 807 |
Result := aStream.Seek(-sizeof(TailRec), soEnd); |
| 808 |
if (Result >= 0) then begin |
| 809 |
aStream.ReadBuffer(TailRec, sizeof(TailRec)); |
| 810 |
if (TailRec.Signature = Ab_ZipEndCentralDirectorySignature) and |
| 811 |
(TailRec.CommentLength = 0) then begin |
| 812 |
aStream.Seek(Result, soBeginning); |
| 813 |
Exit; |
| 814 |
end; |
| 815 |
end; |
| 816 |
|
| 817 |
{the zip stream seems to have a comment, or it has null padding |
| 818 |
bytes from some flaky program, or it's not even a zip formatted |
| 819 |
stream; we need to search for the tail signature} |
| 820 |
|
| 821 |
{get a buffer} |
| 822 |
BufSize := Min(MaxBufSize, aStream.Size); |
| 823 |
GetMem(Buffer, BufSize); |
| 824 |
try |
| 825 |
|
| 826 |
{start out searching backwards} |
| 827 |
Offset := -BufSize; |
| 828 |
|
| 829 |
{seek to the search position} |
| 830 |
Result := aStream.Seek(Offset, soEnd); |
| 831 |
if (Result < 0) then begin |
| 832 |
Result := aStream.Seek(0, soBeginning); |
| 833 |
end; |
| 834 |
|
| 835 |
{read a buffer full} |
| 836 |
BytesRead := aStream.Read(Buffer^, BufSize); |
| 837 |
|
| 838 |
if BytesRead < sizeOf(TailRec) then begin |
| 839 |
Result := -1; |
| 840 |
Exit; |
| 841 |
end; |
| 842 |
|
| 843 |
{search backwards through the buffer looking for the signature} |
| 844 |
TestPos := Buffer + BytesRead - sizeof(TailRec); |
| 845 |
while (TestPos <> Buffer) and |
| 846 |
(PLongint(TestPos)^ <> Ab_ZipEndCentralDirectorySignature) do |
| 847 |
dec(TestPos); |
| 848 |
|
| 849 |
{if we found the signature...} |
| 850 |
if (PLongint(TestPos)^ = Ab_ZipEndCentralDirectorySignature) then begin |
| 851 |
|
| 852 |
{get the tail record at this position} |
| 853 |
Move(TestPos^, TailRec, sizeof(TailRec)); |
| 854 |
|
| 855 |
{if it's as valid a tail as we can check here...} |
| 856 |
CommentLen := -Offset - (TestPos - Buffer + sizeof(TailRec)); |
| 857 |
if (TailRec.CommentLength <= CommentLen) then begin |
| 858 |
|
| 859 |
{calculate its position and exit} |
| 860 |
Result := Result + (TestPos - Buffer); |
| 861 |
aStream.Seek(Result, soBeginning); |
| 862 |
Exit; |
| 863 |
end; |
| 864 |
end; |
| 865 |
|
| 866 |
{if we reach this point, the CD tail is not present} |
| 867 |
Result := -1; |
| 868 |
aStream.Seek(StartPos, soBeginning); |
| 869 |
finally |
| 870 |
FreeMem(Buffer); |
| 871 |
end; |
| 872 |
end; |
| 873 |
{============================================================================} |
| 874 |
procedure MakeSelfExtracting( StubStream, ZipStream, |
| 875 |
SelfExtractingStream : TStream ); |
| 876 |
{-takes an executable stub, and a .zip format stream, and creates |
| 877 |
a SelfExtracting stream. The stub should create a TAbZipArchive |
| 878 |
passing itself as the file, using a read-only open mode. It should |
| 879 |
then perform operations as needed - like ExtractFiles( '*.*' ). |
| 880 |
This routine updates the RelativeOffset of each item in the archive} |
| 881 |
var |
| 882 |
DirectoryStart : Int64; |
| 883 |
FileSignature : Longint; |
| 884 |
StubSize : LongWord; |
| 885 |
TailPosition : Int64; |
| 886 |
ZDFF : TAbZipDirectoryFileFooter; |
| 887 |
ZipItem : TAbZipItem; |
| 888 |
IsWinExe, IsLinuxExe : Boolean; |
| 889 |
begin |
| 890 |
{check file type of stub stream} |
| 891 |
StubStream.Position := 0; |
| 892 |
StubStream.Read(FileSignature, SizeOf(FileSignature)); |
| 893 |
|
| 894 |
{detect executable type } |
| 895 |
IsLinuxExe := FileSignature = Ab_LinuxExeSignature; |
| 896 |
IsWinExe := LongRec(FileSignature).Lo = Ab_WindowsExeSignature; |
| 897 |
|
| 898 |
if not (IsWinExe or IsLinuxExe) then |
| 899 |
raise EAbZipInvalidStub.Create; |
| 900 |
|
| 901 |
StubStream.Position := 0; |
| 902 |
StubSize := StubStream.Size; |
| 903 |
|
| 904 |
ZipStream.Position := 0; |
| 905 |
ZipStream.Read( FileSignature, sizeof( FileSignature ) ); |
| 906 |
if LongRec(FileSignature).Lo <> Ab_GeneralZipSignature then |
| 907 |
raise EAbZipInvalid.Create; |
| 908 |
ZipStream.Position := 0; |
| 909 |
|
| 910 |
{copy the stub into the selfex stream} |
| 911 |
SelfExtractingStream.Position := 0; |
| 912 |
SelfExtractingStream.CopyFrom( StubStream, 0 ); |
| 913 |
|
| 914 |
TailPosition := FindCentralDirectoryTail( ZipStream ); |
| 915 |
if TailPosition = -1 then |
| 916 |
raise EAbZipInvalid.Create; |
| 917 |
{load the ZipDirectoryFileFooter} |
| 918 |
ZDFF := TAbZipDirectoryFileFooter.Create; |
| 919 |
try |
| 920 |
ZDFF.LoadFromStream( ZipStream ); |
| 921 |
DirectoryStart := ZDFF.DirectoryOffset; |
| 922 |
finally |
| 923 |
ZDFF.Free; |
| 924 |
end; |
| 925 |
{copy everything up to the CDH into the SelfExtractingStream} |
| 926 |
ZipStream.Position := 0; |
| 927 |
SelfExtractingStream.CopyFrom( ZipStream, DirectoryStart ); |
| 928 |
ZipStream.Position := DirectoryStart; |
| 929 |
repeat |
| 930 |
ZipItem := TAbZipItem.Create; |
| 931 |
try |
| 932 |
ZipItem.LoadFromStream( ZipStream ); |
| 933 |
ZipItem.RelativeOffset := ZipItem.RelativeOffset + StubSize; |
| 934 |
{save the modified entry into the Self Extracting Stream} |
| 935 |
ZipItem.SaveCDHToStream( SelfExtractingStream ); |
| 936 |
finally |
| 937 |
ZipItem.Free; |
| 938 |
end; |
| 939 |
until ZipStream.Position = TailPosition; |
| 940 |
|
| 941 |
{save the CDH Footer.} |
| 942 |
ZDFF := TAbZipDirectoryFileFooter.Create; |
| 943 |
try |
| 944 |
ZDFF.LoadFromStream( ZipStream ); |
| 945 |
ZDFF.DirectoryOffset := ZDFF.DirectoryOffset + StubSize; |
| 946 |
ZDFF.SaveToStream( SelfExtractingStream ); |
| 947 |
finally |
| 948 |
ZDFF.Free; |
| 949 |
end; |
| 950 |
end; |
| 951 |
{============================================================================} |
| 952 |
{ TAbZipDataDescriptor implementation ====================================== } |
| 953 |
procedure TAbZipDataDescriptor.LoadFromStream(Stream: TStream); |
| 954 |
var |
| 955 |
Signature: LongInt = 0; |
| 956 |
begin |
| 957 |
Stream.Read(Signature, SizeOf(Ab_ZipDataDescriptorSignature)); |
| 958 |
if (Signature <> Ab_ZipDataDescriptorSignature) then Exit; |
| 959 |
Stream.Read(FCRC32, SizeOf(FCRC32)); |
| 960 |
Stream.Read(FCompressedSize, SizeOf(LongWord)); |
| 961 |
Stream.Read(FUncompressedSize, SizeOf(LongWord)); |
| 962 |
end; |
| 963 |
{ -------------------------------------------------------------------------- } |
| 964 |
procedure TAbZipDataDescriptor.SaveToStream( Stream : TStream ); |
| 965 |
begin |
| 966 |
Stream.Write( Ab_ZipDataDescriptorSignature, sizeof( Ab_ZipDataDescriptorSignature ) ); |
| 967 |
Stream.Write( FCRC32, sizeof( FCRC32 ) ); |
| 968 |
if (FCompressedSize >= $FFFFFFFF) or (FUncompressedSize >= $FFFFFFFF) then begin |
| 969 |
Stream.Write( FCompressedSize, sizeof( FCompressedSize ) ); |
| 970 |
Stream.Write( FUncompressedSize, sizeof( FUncompressedSize ) ); |
| 971 |
end |
| 972 |
else begin |
| 973 |
Stream.Write( FCompressedSize, sizeof( LongWord ) ); |
| 974 |
Stream.Write( FUncompressedSize, sizeof( LongWord ) ); |
| 975 |
end; |
| 976 |
end; |
| 977 |
{ -------------------------------------------------------------------------- } |
| 978 |
|
| 979 |
{ TAbZipFileHeader implementation ========================================== } |
| 980 |
constructor TAbZipFileHeader.Create; |
| 981 |
begin |
| 982 |
inherited Create; |
| 983 |
FExtraField := TAbExtraField.Create; |
| 984 |
FValidSignature := $0; |
| 985 |
end; |
| 986 |
{ -------------------------------------------------------------------------- } |
| 987 |
destructor TAbZipFileHeader.Destroy; |
| 988 |
begin |
| 989 |
FreeAndNil(FExtraField); |
| 990 |
inherited Destroy; |
| 991 |
end; |
| 992 |
{ -------------------------------------------------------------------------- } |
| 993 |
function TAbZipFileHeader.GetCompressionMethod : TAbZipCompressionMethod; |
| 994 |
begin |
| 995 |
Result := TAbZipCompressionMethod( FCompressionMethod ); |
| 996 |
end; |
| 997 |
{ -------------------------------------------------------------------------- } |
| 998 |
function TAbZipFileHeader.GetDataDescriptor : Boolean; |
| 999 |
begin |
| 1000 |
Result := ( CompressionMethod = cmDeflated ) and |
| 1001 |
( ( FGeneralPurposeBitFlag and AbHasDataDescriptorFlag ) <> 0 ); |
| 1002 |
end; |
| 1003 |
{ -------------------------------------------------------------------------- } |
| 1004 |
function TAbZipFileHeader.GetCompressionRatio : Double; |
| 1005 |
var |
| 1006 |
CompSize : Int64; |
| 1007 |
begin |
| 1008 |
{adjust for encrypted headers - ensures we never get negative compression |
| 1009 |
ratios for stored, encrypted files - no guarantees about negative |
| 1010 |
compression ratios in other cases} |
| 1011 |
if isEncrypted then |
| 1012 |
CompSize := CompressedSize - 12 |
| 1013 |
else |
| 1014 |
CompSize := CompressedSize; |
| 1015 |
if UncompressedSize > 0 then |
| 1016 |
Result := 100.0 * ( 1 - ( ( 1.0 * CompSize ) / UncompressedSize ) ) |
| 1017 |
else |
| 1018 |
Result := 0.0; |
| 1019 |
end; |
| 1020 |
{ -------------------------------------------------------------------------- } |
| 1021 |
function TAbZipFileHeader.GetDeflationOption : TAbZipDeflationOption; |
| 1022 |
begin |
| 1023 |
if CompressionMethod = cmDeflated then |
| 1024 |
if ( ( FGeneralPurposeBitFlag and $02 ) <> 0 ) then |
| 1025 |
if ( ( FGeneralPurposeBitFlag and $04 ) <> 0 ) then |
| 1026 |
Result := doSuperFast |
| 1027 |
else |
| 1028 |
Result := doMaximum |
| 1029 |
else |
| 1030 |
if ( ( FGeneralPurposeBitFlag and $04 ) <> 0 ) then |
| 1031 |
Result := doFast |
| 1032 |
else |
| 1033 |
Result := doNormal |
| 1034 |
else |
| 1035 |
Result := doInvalid; |
| 1036 |
end; |
| 1037 |
{ -------------------------------------------------------------------------- } |
| 1038 |
function TAbZipFileHeader.GetDictionarySize : TAbZipDictionarySize; |
| 1039 |
begin |
| 1040 |
if CompressionMethod = cmImploded then |
| 1041 |
if ( ( FGeneralPurposeBitFlag and $02 ) <> 0 ) then |
| 1042 |
Result := ds8K |
| 1043 |
else |
| 1044 |
Result := ds4K |
| 1045 |
else |
| 1046 |
Result := dsInvalid; |
| 1047 |
end; |
| 1048 |
{ -------------------------------------------------------------------------- } |
| 1049 |
function TAbZipFileHeader.GetEncrypted : Boolean; |
| 1050 |
begin |
| 1051 |
{bit 0 of the GeneralPurposeBitFlag} |
| 1052 |
Result := ( ( FGeneralPurposeBitFlag and AbFileIsEncryptedFlag ) <> 0 ); |
| 1053 |
end; |
| 1054 |
{ -------------------------------------------------------------------------- } |
| 1055 |
function TAbZipFileHeader.GetIsUTF8 : Boolean; |
| 1056 |
begin |
| 1057 |
Result := ( ( GeneralPurposeBitFlag and AbLanguageEncodingFlag ) <> 0 ); |
| 1058 |
end; |
| 1059 |
{ -------------------------------------------------------------------------- } |
| 1060 |
function TAbZipFileHeader.GetShannonFanoTreeCount : Byte; |
| 1061 |
begin |
| 1062 |
if CompressionMethod = cmImploded then |
| 1063 |
if ( ( FGeneralPurposeBitFlag and $04 ) <> 0 ) then |
| 1064 |
Result := 3 |
| 1065 |
else |
| 1066 |
Result := 2 |
| 1067 |
else |
| 1068 |
Result := 0; |
| 1069 |
end; |
| 1070 |
{ -------------------------------------------------------------------------- } |
| 1071 |
function TAbZipFileHeader.GetValid : Boolean; |
| 1072 |
begin |
| 1073 |
Result := ( FValidSignature = FSignature ); |
| 1074 |
end; |
| 1075 |
{ -------------------------------------------------------------------------- } |
| 1076 |
procedure TAbZipFileHeader.SetCompressionMethod( Value : |
| 1077 |
TAbZipCompressionMethod ); |
| 1078 |
begin |
| 1079 |
FCompressionMethod := Ord( Value ); |
| 1080 |
end; |
| 1081 |
{ -------------------------------------------------------------------------- } |
| 1082 |
procedure TAbZipFileHeader.SetIsUTF8( Value : Boolean ); |
| 1083 |
begin |
| 1084 |
if Value then |
| 1085 |
GeneralPurposeBitFlag := GeneralPurposeBitFlag or AbLanguageEncodingFlag |
| 1086 |
else |
| 1087 |
GeneralPurposeBitFlag := GeneralPurposeBitFlag and not AbLanguageEncodingFlag; |
| 1088 |
end; |
| 1089 |
{ -------------------------------------------------------------------------- } |
| 1090 |
|
| 1091 |
{ TAbZipLocalFileHeader implementation ===================================== } |
| 1092 |
constructor TAbZipLocalFileHeader.Create; |
| 1093 |
begin |
| 1094 |
inherited Create; |
| 1095 |
FValidSignature := Ab_ZipLocalFileHeaderSignature; |
| 1096 |
end; |
| 1097 |
{ -------------------------------------------------------------------------- } |
| 1098 |
destructor TAbZipLocalFileHeader.Destroy; |
| 1099 |
begin |
| 1100 |
inherited Destroy; |
| 1101 |
end; |
| 1102 |
{ -------------------------------------------------------------------------- } |
| 1103 |
procedure TAbZipLocalFileHeader.LoadFromStream( Stream : TStream ); |
| 1104 |
var |
| 1105 |
ExtraFieldLength, FileNameLength : Word; |
| 1106 |
begin |
| 1107 |
with Stream do begin |
| 1108 |
Read( FSignature, sizeof( FSignature ) ); |
| 1109 |
Read( FVersionNeededToExtract, sizeof( FVersionNeededToExtract ) ); |
| 1110 |
Read( FGeneralPurposeBitFlag, sizeof( FGeneralPurposeBitFlag ) ); |
| 1111 |
Read( FCompressionMethod, sizeof( FCompressionMethod ) ); |
| 1112 |
Read( FLastModFileTime, sizeof( FLastModFileTime ) ); |
| 1113 |
Read( FLastModFileDate, sizeof( FLastModFileDate ) ); |
| 1114 |
Read( FCRC32, sizeof( FCRC32 ) ); |
| 1115 |
Read( FCompressedSize, sizeof( FCompressedSize ) ); |
| 1116 |
Read( FUncompressedSize, sizeof( FUncompressedSize ) ); |
| 1117 |
Read( FileNameLength, sizeof( FileNameLength ) ); |
| 1118 |
Read( ExtraFieldLength, sizeof( ExtraFieldLength ) ); |
| 1119 |
|
| 1120 |
SetLength( FFileName, FileNameLength ); |
| 1121 |
if FileNameLength > 0 then |
| 1122 |
Read( FFileName[1], FileNameLength ); |
| 1123 |
|
| 1124 |
FExtraField.LoadFromStream( Stream, ExtraFieldLength ); |
| 1125 |
end; |
| 1126 |
if not IsValid then |
| 1127 |
raise EAbZipInvalid.Create; |
| 1128 |
end; |
| 1129 |
{ -------------------------------------------------------------------------- } |
| 1130 |
procedure TAbZipLocalFileHeader.SaveToStream( Stream : TStream ); |
| 1131 |
var |
| 1132 |
ExtraFieldLength, FileNameLength: Word; |
| 1133 |
begin |
| 1134 |
with Stream do begin |
| 1135 |
{write the valid signature from the constant} |
| 1136 |
Write( FValidSignature, sizeof( FValidSignature ) ); |
| 1137 |
Write( FVersionNeededToExtract, sizeof( FVersionNeededToExtract ) ); |
| 1138 |
Write( FGeneralPurposeBitFlag, sizeof( FGeneralPurposeBitFlag ) ); |
| 1139 |
Write( FCompressionMethod, sizeof( FCompressionMethod ) ); |
| 1140 |
Write( FLastModFileTime, sizeof( FLastModFileTime ) ); |
| 1141 |
Write( FLastModFileDate, sizeof( FLastModFileDate ) ); |
| 1142 |
Write( FCRC32, sizeof( FCRC32 ) ); |
| 1143 |
Write( FCompressedSize, sizeof( FCompressedSize ) ); |
| 1144 |
Write( FUncompressedSize, sizeof( FUncompressedSize ) ); |
| 1145 |
FileNameLength := Word( Length( FFileName ) ); |
| 1146 |
Write( FileNameLength, sizeof( FileNameLength ) ); |
| 1147 |
ExtraFieldLength := Length(FExtraField.Buffer); |
| 1148 |
Write( ExtraFieldLength, sizeof( ExtraFieldLength ) ); |
| 1149 |
if FileNameLength > 0 then |
| 1150 |
Write( FFileName[1], FileNameLength ); |
| 1151 |
if ExtraFieldLength > 0 then |
| 1152 |
Write(FExtraField.Buffer[0], ExtraFieldLength); |
| 1153 |
end; |
| 1154 |
end; |
| 1155 |
{ -------------------------------------------------------------------------- } |
| 1156 |
|
| 1157 |
{ TAbZipDirectoryFileHeader implementation ================================= } |
| 1158 |
constructor TAbZipDirectoryFileHeader.Create; |
| 1159 |
begin |
| 1160 |
inherited Create; |
| 1161 |
FValidSignature := Ab_ZipCentralDirectoryFileHeaderSignature; |
| 1162 |
end; |
| 1163 |
{ -------------------------------------------------------------------------- } |
| 1164 |
destructor TAbZipDirectoryFileHeader.Destroy; |
| 1165 |
begin |
| 1166 |
inherited Destroy; |
| 1167 |
end; |
| 1168 |
{ -------------------------------------------------------------------------- } |
| 1169 |
procedure TAbZipDirectoryFileHeader.LoadFromStream( Stream : TStream ); |
| 1170 |
var |
| 1171 |
ExtraFieldLength, FileCommentLength, FileNameLength : Word; |
| 1172 |
begin |
| 1173 |
with Stream do begin |
| 1174 |
Read( FSignature, sizeof( FSignature ) ); |
| 1175 |
Read( FVersionMadeBy, sizeof( FVersionMadeBy ) ); |
| 1176 |
Read( FVersionNeededToExtract, sizeof( FVersionNeededToExtract ) ); |
| 1177 |
Read( FGeneralPurposeBitFlag, sizeof( FGeneralPurposeBitFlag ) ); |
| 1178 |
Read( FCompressionMethod, sizeof( FCompressionMethod ) ); |
| 1179 |
Read( FLastModFileTime, sizeof( FLastModFileTime ) ); |
| 1180 |
Read( FLastModFileDate, sizeof( FLastModFileDate ) ); |
| 1181 |
Read( FCRC32, sizeof( FCRC32 ) ); |
| 1182 |
Read( FCompressedSize, sizeof( FCompressedSize ) ); |
| 1183 |
Read( FUncompressedSize, sizeof( FUncompressedSize ) ); |
| 1184 |
Read( FileNameLength, sizeof( FileNameLength ) ); |
| 1185 |
Read( ExtraFieldLength, sizeof( ExtraFieldLength ) ); |
| 1186 |
Read( FileCommentLength, sizeof( FileCommentLength ) ); |
| 1187 |
Read( FDiskNumberStart, sizeof( FDiskNumberStart ) ); |
| 1188 |
Read( FInternalFileAttributes, sizeof( FInternalFileAttributes ) ); |
| 1189 |
Read( FExternalFileAttributes, sizeof( FExternalFileAttributes ) ); |
| 1190 |
Read( FRelativeOffset, sizeof( FRelativeOffset ) ); |
| 1191 |
|
| 1192 |
SetLength( FFileName, FileNameLength ); |
| 1193 |
if FileNameLength > 0 then |
| 1194 |
Read( FFileName[1], FileNameLength ); |
| 1195 |
|
| 1196 |
FExtraField.LoadFromStream( Stream, ExtraFieldLength ); |
| 1197 |
|
| 1198 |
SetLength( FFileComment, FileCommentLength ); |
| 1199 |
if FileCommentLength > 0 then |
| 1200 |
Read( FFileComment[1], FileCommentLength ); |
| 1201 |
end; |
| 1202 |
if not IsValid then |
| 1203 |
raise EAbZipInvalid.Create; |
| 1204 |
end; |
| 1205 |
{ -------------------------------------------------------------------------- } |
| 1206 |
procedure TAbZipDirectoryFileHeader.SaveToStream( Stream : TStream ); |
| 1207 |
var |
| 1208 |
ExtraFieldLength, FileCommentLength, FileNameLength : Word; |
| 1209 |
begin |
| 1210 |
with Stream do begin |
| 1211 |
{write the valid signature from the constant} |
| 1212 |
Write( FValidSignature, sizeof( FValidSignature ) ); |
| 1213 |
Write( FVersionMadeBy, sizeof( FVersionMadeBy ) ); |
| 1214 |
Write( FVersionNeededToExtract, sizeof( FVersionNeededToExtract ) ); |
| 1215 |
Write( FGeneralPurposeBitFlag, sizeof( FGeneralPurposeBitFlag ) ); |
| 1216 |
Write( FCompressionMethod, sizeof( FCompressionMethod ) ); |
| 1217 |
Write( FLastModFileTime, sizeof( FLastModFileTime ) ); |
| 1218 |
Write( FLastModFileDate, sizeof( FLastModFileDate ) ); |
| 1219 |
Write( FCRC32, sizeof( FCRC32 ) ); |
| 1220 |
Write( FCompressedSize, sizeof( FCompressedSize ) ); |
| 1221 |
Write( FUncompressedSize, sizeof( FUncompressedSize ) ); |
| 1222 |
FileNameLength := Word( Length( FFileName ) ); |
| 1223 |
Write( FileNameLength, sizeof( FileNameLength ) ); |
| 1224 |
ExtraFieldLength := Length(FExtraField.Buffer); |
| 1225 |
Write( ExtraFieldLength, sizeof( ExtraFieldLength ) ); |
| 1226 |
FileCommentLength := Word( Length( FFileComment ) ); |
| 1227 |
Write( FileCommentLength, sizeof( FileCommentLength ) ); |
| 1228 |
Write( FDiskNumberStart, sizeof( FDiskNumberStart ) ); |
| 1229 |
Write( FInternalFileAttributes, sizeof( FInternalFileAttributes ) ); |
| 1230 |
Write( FExternalFileAttributes, sizeof( FExternalFileAttributes ) ); |
| 1231 |
Write( FRelativeOffset, sizeof( FRelativeOffset ) ); |
| 1232 |
if FileNameLength > 0 then |
| 1233 |
Write( FFileName[1], FileNameLength ); |
| 1234 |
if ExtraFieldLength > 0 then |
| 1235 |
Write( FExtraField.Buffer[0], ExtraFieldLength ); |
| 1236 |
if FileCommentLength > 0 then |
| 1237 |
Write( FFileComment[1], FileCommentLength ); |
| 1238 |
end; |
| 1239 |
end; |
| 1240 |
{ -------------------------------------------------------------------------- } |
| 1241 |
|
| 1242 |
{ TAbZipDirectoryFileFooter implementation ================================= } |
| 1243 |
function TAbZipDirectoryFileFooter.GetIsZip64: Boolean; |
| 1244 |
begin |
| 1245 |
if DiskNumber >= $FFFF then Exit(True); |
| 1246 |
if StartDiskNumber >= $FFFF then Exit(True); |
| 1247 |
if EntriesOnDisk >= $FFFF then Exit(True); |
| 1248 |
if TotalEntries >= $FFFF then Exit(True); |
| 1249 |
if DirectorySize >= $FFFFFFFF then Exit(True); |
| 1250 |
if DirectoryOffset >= $FFFFFFFF then Exit(True); |
| 1251 |
Result := False; |
| 1252 |
end; |
| 1253 |
{ -------------------------------------------------------------------------- } |
| 1254 |
procedure TAbZipDirectoryFileFooter.LoadFromStream( Stream : TStream ); |
| 1255 |
var |
| 1256 |
Footer: TAbZipEndOfCentralDirectoryRecord; |
| 1257 |
begin |
| 1258 |
Stream.ReadBuffer( Footer, SizeOf(Footer) ); |
| 1259 |
if Footer.Signature <> Ab_ZipEndCentralDirectorySignature then |
| 1260 |
raise EAbZipInvalid.Create; |
| 1261 |
FDiskNumber := Footer.DiskNumber; |
| 1262 |
FStartDiskNumber := Footer.StartDiskNumber; |
| 1263 |
FEntriesOnDisk := Footer.EntriesOnDisk; |
| 1264 |
FTotalEntries := Footer.TotalEntries; |
| 1265 |
FDirectorySize := Footer.DirectorySize; |
| 1266 |
FDirectoryOffset := Footer.DirectoryOffset; |
| 1267 |
SetLength( FZipfileComment, Footer.CommentLength ); |
| 1268 |
if Footer.CommentLength > 0 then |
| 1269 |
Stream.ReadBuffer( FZipfileComment[1], Footer.CommentLength ); |
| 1270 |
end; |
| 1271 |
{ -------------------------------------------------------------------------- } |
| 1272 |
procedure TAbZipDirectoryFileFooter.LoadZip64FromStream( Stream : TStream ); |
| 1273 |
{load the ZIP64 end of central directory record. |
| 1274 |
LoadFromStream() must be called first to load the standard record} |
| 1275 |
var |
| 1276 |
Footer: TAbZip64EndOfCentralDirectoryRecord; |
| 1277 |
begin |
| 1278 |
Stream.ReadBuffer( Footer, SizeOf(Footer) ); |
| 1279 |
if Footer.Signature <> Ab_Zip64EndCentralDirectorySignature then |
| 1280 |
raise EAbZipInvalid.Create; |
| 1281 |
if FDiskNumber = $FFFF then |
| 1282 |
FDiskNumber := Footer.DiskNumber; |
| 1283 |
if FStartDiskNumber = $FFFF then |
| 1284 |
FStartDiskNumber := Footer.StartDiskNumber; |
| 1285 |
if FEntriesOnDisk = $FFFF then |
| 1286 |
FEntriesOnDisk := Footer.EntriesOnDisk; |
| 1287 |
if FTotalEntries = $FFFF then |
| 1288 |
FTotalEntries := Footer.TotalEntries; |
| 1289 |
if FDirectorySize = $FFFFFFFF then |
| 1290 |
FDirectorySize := Footer.DirectorySize; |
| 1291 |
if FDirectoryOffset = $FFFFFFFF then |
| 1292 |
FDirectoryOffset := Footer.DirectoryOffset; |
| 1293 |
{RecordSize, VersionMadeBy, and VersionNeededToExtract are currently ignored} |
| 1294 |
end; |
| 1295 |
{ -------------------------------------------------------------------------- } |
| 1296 |
procedure TAbZipDirectoryFileFooter.SaveToStream( Stream : TStream; |
| 1297 |
aZip64TailOffset: Int64 = -1); |
| 1298 |
{write end of central directory record, along with Zip64 records if necessary. |
| 1299 |
aZip64TailOffset is the value to use for the Zip64 locator's directory |
| 1300 |
offset, and is only necessary when writing to an intermediate stream} |
| 1301 |
var |
| 1302 |
Footer: TAbZipEndOfCentralDirectoryRecord; |
| 1303 |
Zip64Footer: TAbZip64EndOfCentralDirectoryRecord; |
| 1304 |
Zip64Locator: TAbZip64EndOfCentralDirectoryLocator; |
| 1305 |
begin |
| 1306 |
if IsZip64 then begin |
| 1307 |
{setup Zip64 end of central directory record} |
| 1308 |
Zip64Footer.Signature := Ab_Zip64EndCentralDirectorySignature; |
| 1309 |
Zip64Footer.RecordSize := SizeOf(Zip64Footer) - |
| 1310 |
SizeOf(Zip64Footer.Signature) - SizeOf(Zip64Footer.RecordSize); |
| 1311 |
Zip64Footer.VersionMadeBy := 45; |
| 1312 |
Zip64Footer.VersionNeededToExtract := 45; |
| 1313 |
Zip64Footer.DiskNumber := DiskNumber; |
| 1314 |
Zip64Footer.StartDiskNumber := StartDiskNumber; |
| 1315 |
Zip64Footer.EntriesOnDisk := EntriesOnDisk; |
| 1316 |
Zip64Footer.TotalEntries := TotalEntries; |
| 1317 |
Zip64Footer.DirectorySize := DirectorySize; |
| 1318 |
Zip64Footer.DirectoryOffset := DirectoryOffset; |
| 1319 |
{setup Zip64 end of central directory locator} |
| 1320 |
Zip64Locator.Signature := Ab_Zip64EndCentralDirectoryLocatorSignature; |
| 1321 |
Zip64Locator.StartDiskNumber := DiskNumber; |
| 1322 |
if aZip64TailOffset = -1 then |
| 1323 |
Zip64Locator.RelativeOffset := Stream.Position |
| 1324 |
else |
| 1325 |
Zip64Locator.RelativeOffset := aZip64TailOffset; |
| 1326 |
Zip64Locator.TotalDisks := DiskNumber + 1; |
| 1327 |
{write Zip64 records} |
| 1328 |
Stream.WriteBuffer(Zip64Footer, SizeOf(Zip64Footer)); |
| 1329 |
Stream.WriteBuffer(Zip64Locator, SizeOf(Zip64Locator)); |
| 1330 |
end; |
| 1331 |
Footer.Signature := Ab_ZipEndCentralDirectorySignature; |
| 1332 |
Footer.DiskNumber := Min(FDiskNumber, $FFFF); |
| 1333 |
Footer.StartDiskNumber := Min(FStartDiskNumber, $FFFF); |
| 1334 |
Footer.EntriesOnDisk := Min(FEntriesOnDisk, $FFFF); |
| 1335 |
Footer.TotalEntries := Min(FTotalEntries, $FFFF); |
| 1336 |
Footer.DirectorySize := Min(FDirectorySize, $FFFFFFFF); |
| 1337 |
Footer.DirectoryOffset := Min(FDirectoryOffset, $FFFFFFFF); |
| 1338 |
Footer.CommentLength := Length( FZipfileComment ); |
| 1339 |
Stream.WriteBuffer( Footer, SizeOf(Footer) ); |
| 1340 |
if FZipfileComment <> '' then |
| 1341 |
Stream.Write( FZipfileComment[1], Length(FZipfileComment) ); |
| 1342 |
end; |
| 1343 |
{ -------------------------------------------------------------------------- } |
| 1344 |
|
| 1345 |
{ TAbZipItem implementation ================================================ } |
| 1346 |
constructor TAbZipItem.Create; |
| 1347 |
begin |
| 1348 |
inherited Create; |
| 1349 |
FItemInfo := TAbZipDirectoryFileHeader.Create; |
| 1350 |
FLFHExtraField := TAbExtraField.Create; |
| 1351 |
end; |
| 1352 |
{ -------------------------------------------------------------------------- } |
| 1353 |
destructor TAbZipItem.Destroy; |
| 1354 |
begin |
| 1355 |
FLFHExtraField.Free; |
| 1356 |
FItemInfo.Free; |
| 1357 |
FItemInfo := nil; |
| 1358 |
inherited Destroy; |
| 1359 |
end; |
| 1360 |
{ -------------------------------------------------------------------------- } |
| 1361 |
function TAbZipItem.GetCompressionMethod : TAbZipCompressionMethod; |
| 1362 |
begin |
| 1363 |
Result := FItemInfo.CompressionMethod; |
| 1364 |
end; |
| 1365 |
{ -------------------------------------------------------------------------- } |
| 1366 |
function TAbZipItem.GetCompressionRatio : Double; |
| 1367 |
begin |
| 1368 |
Result := FItemInfo.CompressionRatio; |
| 1369 |
end; |
| 1370 |
{ -------------------------------------------------------------------------- } |
| 1371 |
function TAbZipItem.GetCRC32 : Longint; |
| 1372 |
begin |
| 1373 |
Result := FItemInfo.CRC32; |
| 1374 |
end; |
| 1375 |
{ -------------------------------------------------------------------------- } |
| 1376 |
function TAbZipItem.GetDeflationOption : TAbZipDeflationOption; |
| 1377 |
begin |
| 1378 |
Result := FItemInfo.DeflationOption; |
| 1379 |
end; |
| 1380 |
{ -------------------------------------------------------------------------- } |
| 1381 |
function TAbZipItem.GetDictionarySize : TAbZipDictionarySize; |
| 1382 |
begin |
| 1383 |
Result := FItemInfo.DictionarySize; |
| 1384 |
end; |
| 1385 |
{ -------------------------------------------------------------------------- } |
| 1386 |
function TAbZipItem.GetGeneralPurposeBitFlag : Word; |
| 1387 |
begin |
| 1388 |
Result := FItemInfo.GeneralPurposeBitFlag; |
| 1389 |
end; |
| 1390 |
{ -------------------------------------------------------------------------- } |
| 1391 |
function TAbZipItem.GetHostOS: TAbZipHostOS; |
| 1392 |
begin |
| 1393 |
Result := TAbZipHostOS(Hi(VersionMadeBy)); |
| 1394 |
end; |
| 1395 |
{ -------------------------------------------------------------------------- } |
| 1396 |
function TAbZipItem.GetExternalFileAttributes : LongWord; |
| 1397 |
begin |
| 1398 |
Result := FItemInfo.ExternalFileAttributes; |
| 1399 |
end; |
| 1400 |
{ -------------------------------------------------------------------------- } |
| 1401 |
function TAbZipItem.GetExtraField : TAbExtraField; |
| 1402 |
begin |
| 1403 |
Result := FItemInfo.ExtraField; |
| 1404 |
end; |
| 1405 |
{ -------------------------------------------------------------------------- } |
| 1406 |
function TAbZipItem.GetFileComment : AnsiString; |
| 1407 |
begin |
| 1408 |
Result := FItemInfo.FileComment; |
| 1409 |
end; |
| 1410 |
{ -------------------------------------------------------------------------- } |
| 1411 |
function TAbZipItem.GetInternalFileAttributes : Word; |
| 1412 |
begin |
| 1413 |
Result := FItemInfo.InternalFileAttributes; |
| 1414 |
end; |
| 1415 |
{ -------------------------------------------------------------------------- } |
| 1416 |
function TAbZipItem.GetIsDirectory: Boolean; |
| 1417 |
begin |
| 1418 |
Result := ((ExternalFileAttributes and faDirectory) <> 0) or |
| 1419 |
((FileName <> '') and CharInSet(Filename[Length(FFilename)], ['\','/'])); |
| 1420 |
end; |
| 1421 |
{ -------------------------------------------------------------------------- } |
| 1422 |
function TAbZipItem.GetIsEncrypted : Boolean; |
| 1423 |
begin |
| 1424 |
Result := FItemInfo.IsEncrypted; |
| 1425 |
end; |
| 1426 |
{ -------------------------------------------------------------------------- } |
| 1427 |
function TAbZipItem.GetLastModFileDate : Word; |
| 1428 |
begin |
| 1429 |
Result := FItemInfo.LastModFileDate; |
| 1430 |
end; |
| 1431 |
{ -------------------------------------------------------------------------- } |
| 1432 |
function TAbZipItem.GetLastModFileTime : Word; |
| 1433 |
begin |
| 1434 |
Result := FItemInfo.LastModFileTime; |
| 1435 |
end; |
| 1436 |
{ -------------------------------------------------------------------------- } |
| 1437 |
function TAbZipItem.GetNativeFileAttributes : LongInt; |
| 1438 |
begin |
| 1439 |
{$IFDEF MSWINDOWS} |
| 1440 |
if (HostOS = hosUnix) or (ExternalFileAttributes > $1FFFF) then |
| 1441 |
Result := AbUnix2DosFileAttributes(ExternalFileAttributes shr 16) |
| 1442 |
else |
| 1443 |
Result := Byte(ExternalFileAttributes); |
| 1444 |
{$ENDIF} |
| 1445 |
{$IFDEF UNIX} |
| 1446 |
if HostOS in [hosDOS, hosOS2, hosNTFS, hosWinNT, hosVFAT] then |
| 1447 |
Result := AbDOS2UnixFileAttributes(ExternalFileAttributes) |
| 1448 |
else begin |
| 1449 |
Result := ExternalFileAttributes shr 16; |
| 1450 |
if Result = 0 then begin |
| 1451 |
Result:= AB_FPERMISSION_GENERIC; |
| 1452 |
if GetIsDirectory then begin |
| 1453 |
Result := Result or AB_FMODE_DIR or AB_FPERMISSION_OWNEREXECUTE; |
| 1454 |
end; |
| 1455 |
end; |
| 1456 |
end; |
| 1457 |
{$ENDIF} |
| 1458 |
end; |
| 1459 |
{ -------------------------------------------------------------------------- } |
| 1460 |
function TAbZipItem.GetRawFileName : AnsiString; |
| 1461 |
begin |
| 1462 |
Result := FItemInfo.FileName; |
| 1463 |
end; |
| 1464 |
{ -------------------------------------------------------------------------- } |
| 1465 |
function TAbZipItem.GetNativeLastModFileTime: Longint; |
| 1466 |
{$IFDEF UNIX} |
| 1467 |
var |
| 1468 |
DateTime: TDateTime; |
| 1469 |
{$ENDIF} |
| 1470 |
begin |
| 1471 |
// Zip stores MS-DOS date/time. |
| 1472 |
{$IFDEF UNIX} |
| 1473 |
if (FDateTime <> 0) then |
| 1474 |
DateTime := FDateTime |
| 1475 |
else begin |
| 1476 |
DateTime := AbDosFileDateToDateTime(LastModFileDate, LastModFileTime); |
| 1477 |
end; |
| 1478 |
Result := DateTimeToUnixFileTime(DateTime); |
| 1479 |
{$ELSE} |
| 1480 |
if (FDateTime <> 0) then |
| 1481 |
Result := DateTimeToDosFileTime(FDateTime) |
| 1482 |
else begin |
| 1483 |
LongRec(Result).Hi := LastModFileDate; |
| 1484 |
LongRec(Result).Lo := LastModFileTime; |
| 1485 |
end; |
| 1486 |
{$ENDIF} |
| 1487 |
end; |
| 1488 |
{ -------------------------------------------------------------------------- } |
| 1489 |
function TAbZipItem.GetLastModTimeAsDateTime: TDateTime; |
| 1490 |
begin |
| 1491 |
if (FDateTime <> 0) then |
| 1492 |
Result := FDateTime |
| 1493 |
else |
| 1494 |
Result := AbDosFileDateToDateTime(FItemInfo.LastModFileDate, FItemInfo.LastModFileTime); |
| 1495 |
end; |
| 1496 |
{ -------------------------------------------------------------------------- } |
| 1497 |
function TAbZipItem.GetShannonFanoTreeCount : Byte; |
| 1498 |
begin |
| 1499 |
Result := FItemInfo.ShannonFanoTreeCount; |
| 1500 |
end; |
| 1501 |
{ -------------------------------------------------------------------------- } |
| 1502 |
function TAbZipItem.GetVersionMadeBy : Word; |
| 1503 |
begin |
| 1504 |
Result := FItemInfo.VersionMadeBy; |
| 1505 |
end; |
| 1506 |
{ -------------------------------------------------------------------------- } |
| 1507 |
function TAbZipItem.GetVersionNeededToExtract : Word; |
| 1508 |
begin |
| 1509 |
Result := FItemInfo.VersionNeededToExtract; |
| 1510 |
end; |
| 1511 |
{ -------------------------------------------------------------------------- } |
| 1512 |
procedure TAbZipItem.LoadFromStream( Stream : TStream ); |
| 1513 |
var |
| 1514 |
Tag, TagSize, |
| 1515 |
FieldSize: Word; |
| 1516 |
FieldStream: TStream; |
| 1517 |
InfoZipField: PInfoZipUnicodePathRec; |
| 1518 |
UnicodeName: UnicodeString; |
| 1519 |
UTF8Name: AnsiString; |
| 1520 |
XceedField: PXceedUnicodePathRec; |
| 1521 |
SystemCode: TAbZipHostOs; |
| 1522 |
begin |
| 1523 |
FItemInfo.LoadFromStream( Stream ); |
| 1524 |
|
| 1525 |
{ decode filename (ANSI/OEM/UTF-8) } |
| 1526 |
if FItemInfo.IsUTF8 and (FindInvalidUTF8Codepoint(PAnsiChar(FItemInfo.FileName), Length(FItemInfo.FileName)) < 0) then |
| 1527 |
FFileName := FItemInfo.FileName |
| 1528 |
else if FItemInfo.ExtraField.Get(Ab_InfoZipUnicodePathSubfieldID, Pointer(InfoZipField), FieldSize) and |
| 1529 |
(FieldSize > SizeOf(TInfoZipUnicodePathRec)) and |
| 1530 |
(InfoZipField.Version = 1) and |
| 1531 |
(InfoZipField.NameCRC32 = AbCRC32Of(FItemInfo.FileName)) then begin |
| 1532 |
SetString(UTF8Name, InfoZipField.UnicodeName, |
| 1533 |
FieldSize - SizeOf(TInfoZipUnicodePathRec) + 1); |
| 1534 |
FFileName := UTF8Name; |
| 1535 |
end |
| 1536 |
else if FItemInfo.ExtraField.Get(Ab_XceedUnicodePathSubfieldID, Pointer(XceedField), FieldSize) and |
| 1537 |
(FieldSize > SizeOf(TXceedUnicodePathRec)) and |
| 1538 |
(XceedField.Signature = Ab_XceedUnicodePathSignature) and |
| 1539 |
(XceedField.Length * SizeOf(WideChar) = FieldSize - SizeOf(TXceedUnicodePathRec) + SizeOf(WideChar)) then begin |
| 1540 |
SetString(UnicodeName, XceedField.UnicodeName, XceedField.Length); |
| 1541 |
FFileName := Utf16ToUtf8(UnicodeName); |
| 1542 |
end |
| 1543 |
else begin |
| 1544 |
SystemCode := HostOS; |
| 1545 |
if (SystemCode = hosDOS) then |
| 1546 |
FFileName := CeOemToUtf8(FItemInfo.FileName) |
| 1547 |
else if (SystemCode = hosNTFS) or (SystemCode = hosWinNT) then |
| 1548 |
FFileName := CeAnsiToUtf8(FItemInfo.FileName) |
| 1549 |
else |
| 1550 |
FFileName := CeSysToUtf8(FItemInfo.FileName); |
| 1551 |
end; |
| 1552 |
|
| 1553 |
{ read ZIP64 extended header } |
| 1554 |
FUncompressedSize := FItemInfo.UncompressedSize; |
| 1555 |
FCompressedSize := FItemInfo.CompressedSize; |
| 1556 |
FRelativeOffset := FItemInfo.RelativeOffset; |
| 1557 |
FDiskNumberStart := FItemInfo.DiskNumberStart; |
| 1558 |
if FItemInfo.ExtraField.GetStream(Ab_Zip64SubfieldID, FieldStream) then |
| 1559 |
try |
| 1560 |
if FItemInfo.UncompressedSize = $FFFFFFFF then |
| 1561 |
FieldStream.ReadBuffer(FUncompressedSize, SizeOf(Int64)); |
| 1562 |
if FItemInfo.CompressedSize = $FFFFFFFF then |
| 1563 |
FieldStream.ReadBuffer(FCompressedSize, SizeOf(Int64)); |
| 1564 |
if FItemInfo.RelativeOffset = $FFFFFFFF then |
| 1565 |
FieldStream.ReadBuffer(FRelativeOffset, SizeOf(Int64)); |
| 1566 |
if FItemInfo.DiskNumberStart = $FFFF then |
| 1567 |
FieldStream.ReadBuffer(FDiskNumberStart, SizeOf(LongWord)); |
| 1568 |
finally |
| 1569 |
FieldStream.Free; |
| 1570 |
end; |
| 1571 |
|
| 1572 |
LastModFileTime := FItemInfo.LastModFileTime; |
| 1573 |
LastModFileDate := FItemInfo.LastModFileDate; |
| 1574 |
// NTFS Extra Field |
| 1575 |
if FItemInfo.ExtraField.GetStream(Ab_NTFSSubfieldID, FieldStream) then |
| 1576 |
try |
| 1577 |
FieldSize:= FieldStream.Size; |
| 1578 |
if (FieldSize >= 32) then |
| 1579 |
begin |
| 1580 |
// Skip Reserved |
| 1581 |
Dec(FieldSize, 4); |
| 1582 |
FieldStream.Seek(4, soBeginning); |
| 1583 |
while (FieldSize > 4) do |
| 1584 |
begin |
| 1585 |
Dec(FieldSize, 4); |
| 1586 |
Tag:= FieldStream.ReadWord; |
| 1587 |
TagSize:= FieldStream.ReadWord; |
| 1588 |
TagSize:= Min(TagSize, FieldSize); |
| 1589 |
if (Tag = $0001) and (TagSize >= 24) then |
| 1590 |
begin |
| 1591 |
FDateTime:= WinFileTimeToDateTime(TWinFileTime(FieldStream.ReadQWord)); |
| 1592 |
Break; |
| 1593 |
end; |
| 1594 |
Dec(FieldSize, TagSize); |
| 1595 |
end; |
| 1596 |
end; |
| 1597 |
finally |
| 1598 |
FieldStream.Free; |
| 1599 |
end |
| 1600 |
// Extended Timestamp Extra Field |
| 1601 |
else if FItemInfo.ExtraField.GetStream(Ab_InfoZipTimestampSubfieldID, FieldStream) then |
| 1602 |
try |
| 1603 |
FieldSize:= FieldStream.Size; |
| 1604 |
if (FieldSize >= 5) then |
| 1605 |
begin |
| 1606 |
Tag:= FieldStream.ReadByte; |
| 1607 |
if (Tag and $01 <> 0) then |
| 1608 |
FDateTime:= UnixFileTimeToDateTime(TUnixFileTime(FieldStream.ReadDWord)); |
| 1609 |
end; |
| 1610 |
finally |
| 1611 |
FieldStream.Free; |
| 1612 |
end; |
| 1613 |
FDiskFileName := FileName; |
| 1614 |
AbUnfixName( FDiskFileName ); |
| 1615 |
Action := aaNone; |
| 1616 |
Tagged := False; |
| 1617 |
end; |
| 1618 |
{ -------------------------------------------------------------------------- } |
| 1619 |
procedure TAbZipItem.SaveLFHToStream( Stream : TStream ); |
| 1620 |
var |
| 1621 |
LFH : TAbZipLocalFileHeader; |
| 1622 |
Zip64Field: TZip64LocalHeaderRec; |
| 1623 |
begin |
| 1624 |
LFH := TAbZipLocalFileHeader.Create; |
| 1625 |
try |
| 1626 |
LFH.VersionNeededToExtract := VersionNeededToExtract; |
| 1627 |
LFH.GeneralPurposeBitFlag := GeneralPurposeBitFlag; |
| 1628 |
LFH.CompressionMethod := CompressionMethod; |
| 1629 |
LFH.LastModFileTime := LastModFileTime; |
| 1630 |
LFH.LastModFileDate := LastModFileDate; |
| 1631 |
LFH.CRC32 := CRC32; |
| 1632 |
LFH.FileName := RawFileName; |
| 1633 |
LFH.ExtraField.Assign(LFHExtraField); |
| 1634 |
LFH.ExtraField.CloneFrom(ExtraField, Ab_InfoZipUnicodePathSubfieldID); |
| 1635 |
LFH.ExtraField.CloneFrom(ExtraField, Ab_XceedUnicodePathSubfieldID); |
| 1636 |
{ Write ZIP64 local header when file size > 3 GB to speed up archive creation } |
| 1637 |
if (UncompressedSize > $C0000000) then |
| 1638 |
begin |
| 1639 |
{ setup sizes; unlike the central directory header, the ZIP64 local header |
| 1640 |
needs to store both compressed and uncompressed sizes if either needs it } |
| 1641 |
if (CompressedSize >= $FFFFFFFF) or (UncompressedSize >= $FFFFFFFF) then |
| 1642 |
begin |
| 1643 |
LFH.UncompressedSize := $FFFFFFFF; |
| 1644 |
LFH.CompressedSize := $FFFFFFFF; |
| 1645 |
end |
| 1646 |
else begin |
| 1647 |
LFH.UncompressedSize := UncompressedSize; |
| 1648 |
LFH.CompressedSize := CompressedSize; |
| 1649 |
end; |
| 1650 |
Zip64Field.UncompressedSize := UncompressedSize; |
| 1651 |
Zip64Field.CompressedSize := CompressedSize; |
| 1652 |
LFH.ExtraField.Put(Ab_Zip64SubfieldID, Zip64Field, SizeOf(Zip64Field)); |
| 1653 |
end |
| 1654 |
else begin |
| 1655 |
LFH.UncompressedSize := UncompressedSize; |
| 1656 |
LFH.CompressedSize := CompressedSize; |
| 1657 |
LFH.ExtraField.Delete(Ab_Zip64SubfieldID); |
| 1658 |
end; |
| 1659 |
LFH.SaveToStream( Stream ); |
| 1660 |
{ Synchronize central directory header with local file header } |
| 1661 |
FItemInfo.CompressedSize := LFH.CompressedSize; |
| 1662 |
FItemInfo.UncompressedSize := LFH.UncompressedSize; |
| 1663 |
finally |
| 1664 |
LFH.Free; |
| 1665 |
end; |
| 1666 |
end; |
| 1667 |
{ -------------------------------------------------------------------------- } |
| 1668 |
procedure TAbZipItem.SaveCDHToStream( Stream : TStream ); |
| 1669 |
{-Save a ZipCentralDirectorHeader entry to Stream} |
| 1670 |
begin |
| 1671 |
FItemInfo.SaveToStream( Stream ); |
| 1672 |
end; |
| 1673 |
{ -------------------------------------------------------------------------- } |
| 1674 |
procedure TAbZipItem.SaveDDToStream( Stream : TStream ); |
| 1675 |
var |
| 1676 |
DD : TAbZipDataDescriptor; |
| 1677 |
begin |
| 1678 |
DD := TAbZipDataDescriptor.Create; |
| 1679 |
try |
| 1680 |
DD.CRC32 := CRC32; |
| 1681 |
DD.CompressedSize := CompressedSize; |
| 1682 |
DD.UncompressedSize := UncompressedSize; |
| 1683 |
DD.SaveToStream( Stream ); |
| 1684 |
finally |
| 1685 |
DD.Free; |
| 1686 |
end; |
| 1687 |
end; |
| 1688 |
{ -------------------------------------------------------------------------- } |
| 1689 |
procedure TAbZipItem.SetCompressedSize( const Value : Int64 ); |
| 1690 |
begin |
| 1691 |
FCompressedSize := Value; |
| 1692 |
FItemInfo.CompressedSize := Min(Value, $FFFFFFFF); |
| 1693 |
UpdateZip64ExtraHeader; |
| 1694 |
end; |
| 1695 |
{ -------------------------------------------------------------------------- } |
| 1696 |
procedure TAbZipItem.SetCompressionMethod( Value : TAbZipCompressionMethod ); |
| 1697 |
begin |
| 1698 |
FItemInfo.CompressionMethod := Value; |
| 1699 |
UpdateVersionNeededToExtract; |
| 1700 |
end; |
| 1701 |
{ -------------------------------------------------------------------------- } |
| 1702 |
procedure TAbZipItem.SetCRC32( const Value : Longint ); |
| 1703 |
begin |
| 1704 |
FItemInfo.CRC32 := Value; |
| 1705 |
end; |
| 1706 |
{ -------------------------------------------------------------------------- } |
| 1707 |
procedure TAbZipItem.SetDiskNumberStart( Value : LongWord ); |
| 1708 |
begin |
| 1709 |
FDiskNumberStart := Value; |
| 1710 |
FItemInfo.DiskNumberStart := Min(Value, $FFFF); |
| 1711 |
UpdateZip64ExtraHeader; |
| 1712 |
end; |
| 1713 |
{ -------------------------------------------------------------------------- } |
| 1714 |
procedure TAbZipItem.SetExternalFileAttributes( Value : LongWord ); |
| 1715 |
begin |
| 1716 |
FItemInfo.ExternalFileAttributes := Value; |
| 1717 |
end; |
| 1718 |
{ -------------------------------------------------------------------------- } |
| 1719 |
procedure TAbZipItem.SetFileComment(const Value : AnsiString ); |
| 1720 |
begin |
| 1721 |
FItemInfo.FileComment := Value; |
| 1722 |
end; |
| 1723 |
{ -------------------------------------------------------------------------- } |
| 1724 |
{$IFDEF KYLIX}{$IFOPT O+}{$DEFINE OPTIMIZATIONS_ON}{$O-}{$ENDIF}{$ENDIF} |
| 1725 |
procedure TAbZipItem.SetFileName(const Value : string ); |
| 1726 |
var |
| 1727 |
{$IFDEF MSWINDOWS} |
| 1728 |
AnsiName : AnsiString; |
| 1729 |
UnicName : UnicodeString; |
| 1730 |
{$ENDIF} |
| 1731 |
UTF8Name : AnsiString; |
| 1732 |
FieldSize : Word; |
| 1733 |
I : Integer; |
| 1734 |
InfoZipField : PInfoZipUnicodePathRec; |
| 1735 |
UseExtraField: Boolean; |
| 1736 |
begin |
| 1737 |
inherited SetFileName(Value); |
| 1738 |
{$IFDEF MSWINDOWS} |
| 1739 |
FItemInfo.IsUTF8 := False; |
| 1740 |
HostOS := hosDOS; |
| 1741 |
UnicName := CeUtf8ToUtf16(Value); |
| 1742 |
if CeTryEncode(UnicName, CP_OEMCP, False, AnsiName) then |
| 1743 |
{no-op} |
| 1744 |
else if (GetACP <> GetOEMCP) and CeTryEncode(UnicName, CP_ACP, False, AnsiName) then |
| 1745 |
HostOS := hosWinNT |
| 1746 |
else |
| 1747 |
FItemInfo.IsUTF8 := True; |
| 1748 |
if FItemInfo.IsUTF8 then |
| 1749 |
FItemInfo.FileName := Value |
| 1750 |
else |
| 1751 |
FItemInfo.FileName := AnsiName; |
| 1752 |
{$ENDIF} |
| 1753 |
{$IFDEF UNIX} |
| 1754 |
HostOS := hosUnix; |
| 1755 |
FItemInfo.FileName := Value; |
| 1756 |
FItemInfo.IsUTF8 := SystemEncodingUtf8; |
| 1757 |
{$ENDIF} |
| 1758 |
|
| 1759 |
UseExtraField := False; |
| 1760 |
if not FItemInfo.IsUTF8 then |
| 1761 |
for i := 1 to Length(Value) do begin |
| 1762 |
if Ord(Value[i]) > 127 then begin |
| 1763 |
UseExtraField := True; |
| 1764 |
Break; |
| 1765 |
end; |
| 1766 |
end; |
| 1767 |
|
| 1768 |
if UseExtraField then begin |
| 1769 |
UTF8Name := Value; |
| 1770 |
FieldSize := SizeOf(TInfoZipUnicodePathRec) + Length(UTF8Name) - 1; |
| 1771 |
GetMem(InfoZipField, FieldSize); |
| 1772 |
try |
| 1773 |
InfoZipField.Version := 1; |
| 1774 |
InfoZipField.NameCRC32 := AbCRC32Of(FItemInfo.FileName); |
| 1775 |
Move(UTF8Name[1], InfoZipField.UnicodeName, Length(UTF8Name)); |
| 1776 |
FItemInfo.ExtraField.Put(Ab_InfoZipUnicodePathSubfieldID, InfoZipField^, FieldSize); |
| 1777 |
finally |
| 1778 |
FreeMem(InfoZipField); |
| 1779 |
end; |
| 1780 |
end |
| 1781 |
else |
| 1782 |
FItemInfo.ExtraField.Delete(Ab_InfoZipUnicodePathSubfieldID); |
| 1783 |
FItemInfo.ExtraField.Delete(Ab_XceedUnicodePathSubfieldID); |
| 1784 |
end; |
| 1785 |
{$IFDEF OPTIMIZATIONS_ON}{$O+}{$ENDIF} |
| 1786 |
{ -------------------------------------------------------------------------- } |
| 1787 |
procedure TAbZipItem.SetGeneralPurposeBitFlag( Value : Word ); |
| 1788 |
begin |
| 1789 |
FItemInfo.GeneralPurposeBitFlag := Value; |
| 1790 |
UpdateVersionNeededToExtract; |
| 1791 |
end; |
| 1792 |
{ -------------------------------------------------------------------------- } |
| 1793 |
procedure TAbZipItem.SetHostOS( Value : TAbZipHostOS ); |
| 1794 |
begin |
| 1795 |
FItemInfo.VersionMadeBy := Low(FItemInfo.VersionMadeBy) or |
| 1796 |
Word(Ord(Value)) shl 8; |
| 1797 |
end; |
| 1798 |
{ -------------------------------------------------------------------------- } |
| 1799 |
procedure TAbZipItem.SetInternalFileAttributes( Value : Word ); |
| 1800 |
begin |
| 1801 |
FItemInfo.InternalFileAttributes := Value; |
| 1802 |
end; |
| 1803 |
{ -------------------------------------------------------------------------- } |
| 1804 |
procedure TAbZipItem.SetLastModFileDate( const Value : Word ); |
| 1805 |
begin |
| 1806 |
FItemInfo.LastModFileDate := Value; |
| 1807 |
end; |
| 1808 |
{ -------------------------------------------------------------------------- } |
| 1809 |
procedure TAbZipItem.SetLastModFileTime( const Value : Word ); |
| 1810 |
begin |
| 1811 |
FItemInfo.LastModFileTime := Value; |
| 1812 |
end; |
| 1813 |
{ -------------------------------------------------------------------------- } |
| 1814 |
procedure TAbZipItem.SetRelativeOffset( Value : Int64 ); |
| 1815 |
begin |
| 1816 |
FRelativeOffset := Value; |
| 1817 |
FItemInfo.RelativeOffset := Min(Value, $FFFFFFFF); |
| 1818 |
UpdateZip64ExtraHeader; |
| 1819 |
end; |
| 1820 |
{ -------------------------------------------------------------------------- } |
| 1821 |
procedure TAbZipItem.SetUncompressedSize( const Value : Int64 ); |
| 1822 |
begin |
| 1823 |
FUncompressedSize := Value; |
| 1824 |
FItemInfo.UncompressedSize:= Min(Value, $FFFFFFFF); |
| 1825 |
UpdateZip64ExtraHeader; |
| 1826 |
end; |
| 1827 |
|
| 1828 |
procedure TAbZipItem.SetLastModTimeAsDateTime(const Value: TDateTime); |
| 1829 |
var |
| 1830 |
DataSize: Word; |
| 1831 |
ANtfsTime: PNtfsTimeField; |
| 1832 |
AInfoZipTime: PInfoZipTimeField; |
| 1833 |
begin |
| 1834 |
inherited SetLastModTimeAsDateTime(Value); |
| 1835 |
|
| 1836 |
// Update time extra fields |
| 1837 |
if FItemInfo.ExtraField.Get(Ab_NTFSSubfieldID, ANtfsTime, DataSize) then |
| 1838 |
begin |
| 1839 |
if (DataSize = SizeOf(TNtfsTimeField)) then |
| 1840 |
begin |
| 1841 |
if ANtfsTime^.Tag = $0001 then |
| 1842 |
begin |
| 1843 |
ANtfsTime^.Mtime := DateTimeToWinFileTime(Value); |
| 1844 |
end; |
| 1845 |
end; |
| 1846 |
end |
| 1847 |
else if FItemInfo.ExtraField.Get(Ab_InfoZipTimestampSubfieldID, AInfoZipTime, DataSize) then |
| 1848 |
begin |
| 1849 |
if (DataSize = SizeOf(TInfoZipTimeField)) then |
| 1850 |
begin |
| 1851 |
if (AInfoZipTime^.Tag and $01 <> 0) then |
| 1852 |
begin |
| 1853 |
AInfoZipTime^.Mtime := UInt32(DateTimeToUnixFileTime(Value)); |
| 1854 |
end; |
| 1855 |
end; |
| 1856 |
end; |
| 1857 |
end; |
| 1858 |
|
| 1859 |
{ -------------------------------------------------------------------------- } |
| 1860 |
procedure TAbZipItem.SetVersionMadeBy( Value : Word ); |
| 1861 |
begin |
| 1862 |
FItemInfo.VersionMadeBy := Value; |
| 1863 |
end; |
| 1864 |
{ -------------------------------------------------------------------------- } |
| 1865 |
procedure TAbZipItem.SetVersionNeededToExtract( Value : Word ); |
| 1866 |
begin |
| 1867 |
FItemInfo.VersionNeededToExtract := Value; |
| 1868 |
end; |
| 1869 |
{ -------------------------------------------------------------------------- } |
| 1870 |
procedure TAbZipItem.UpdateVersionNeededToExtract; |
| 1871 |
{calculates VersionNeededToExtract and VersionMadeBy based on used features} |
| 1872 |
begin |
| 1873 |
{According to AppNote.txt zipx compression methods should set the Version |
| 1874 |
Needed To Extract to the AppNote version the method was introduced in (e.g., |
| 1875 |
6.3 for PPMd). Most utilities just set it to 2.0 and rely on the extractor |
| 1876 |
detecting unsupported compression methods, since it's easier to add support |
| 1877 |
for decompression methods without implementing the entire newer spec. } |
| 1878 |
if ExtraField.Has(Ab_Zip64SubfieldID) then |
| 1879 |
VersionNeededToExtract := 45 |
| 1880 |
else if IsDirectory or IsEncrypted or not (CompressionMethod in [cmStored..cmImploded]) then |
| 1881 |
VersionNeededToExtract := 20 |
| 1882 |
else |
| 1883 |
VersionNeededToExtract := 10; |
| 1884 |
VersionMadeBy := (VersionMadeBy and $FF00) + Max(20, VersionNeededToExtract); |
| 1885 |
end; |
| 1886 |
{ -------------------------------------------------------------------------- } |
| 1887 |
procedure TAbZipItem.UpdateZip64ExtraHeader; |
| 1888 |
var |
| 1889 |
Changed: Boolean; |
| 1890 |
FieldStream: TMemoryStream; |
| 1891 |
begin |
| 1892 |
FieldStream := TMemoryStream.Create; |
| 1893 |
try |
| 1894 |
if UncompressedSize >= $FFFFFFFF then |
| 1895 |
FieldStream.WriteBuffer(FUncompressedSize, SizeOf(Int64)); |
| 1896 |
if CompressedSize >= $FFFFFFFF then |
| 1897 |
FieldStream.WriteBuffer(FCompressedSize, SizeOf(Int64)); |
| 1898 |
if RelativeOffset >= $FFFFFFFF then |
| 1899 |
FieldStream.WriteBuffer(FRelativeOffset, SizeOf(Int64)); |
| 1900 |
if DiskNumberStart >= $FFFF then |
| 1901 |
FieldStream.WriteBuffer(FDiskNumberStart, SizeOf(LongWord)); |
| 1902 |
Changed := (FieldStream.Size > 0) <> ExtraField.Has(Ab_Zip64SubfieldID); |
| 1903 |
if FieldStream.Size > 0 then |
| 1904 |
ExtraField.Put(Ab_Zip64SubfieldID, FieldStream.Memory^, FieldStream.Size) |
| 1905 |
else |
| 1906 |
ExtraField.Delete(Ab_Zip64SubfieldID); |
| 1907 |
if Changed then |
| 1908 |
UpdateVersionNeededToExtract; |
| 1909 |
finally |
| 1910 |
FieldStream.Free; |
| 1911 |
end; |
| 1912 |
end; |
| 1913 |
{ -------------------------------------------------------------------------- } |
| 1914 |
|
| 1915 |
|
| 1916 |
{ TAbZipArchive implementation ============================================= } |
| 1917 |
constructor TAbZipArchive.CreateFromStream( aStream : TStream; |
| 1918 |
const ArchiveName : string ); |
| 1919 |
begin |
| 1920 |
inherited CreateFromStream( aStream, ArchiveName ); |
| 1921 |
FCompressionMethodToUse := smBestMethod; |
| 1922 |
FInfo := TAbZipDirectoryFileFooter.Create; |
| 1923 |
StoreOptions := StoreOptions + [soStripDrive]; |
| 1924 |
FDeflationOption := doNormal; |
| 1925 |
FPasswordRetries := AbDefPasswordRetries; |
| 1926 |
FTempDir := ''; |
| 1927 |
SpanningThreshold := AbDefZipSpanningThreshold; |
| 1928 |
FSuspiciousLinks := TStringList.Create; |
| 1929 |
end; |
| 1930 |
{ -------------------------------------------------------------------------- } |
| 1931 |
destructor TAbZipArchive.Destroy; |
| 1932 |
begin |
| 1933 |
FInfo.Free; |
| 1934 |
FInfo := nil; |
| 1935 |
inherited Destroy; |
| 1936 |
FSuspiciousLinks.Free; |
| 1937 |
end; |
| 1938 |
{ -------------------------------------------------------------------------- } |
| 1939 |
function TAbZipArchive.CreateItem(const SourceFileName : string; |
| 1940 |
const ArchiveDirectory : string): TAbArchiveItem; |
| 1941 |
var |
| 1942 |
FullSourceFileName, FullArchiveFileName: string; |
| 1943 |
begin |
| 1944 |
Result := TAbZipItem.Create; |
| 1945 |
with TAbZipItem( Result ) do begin |
| 1946 |
CompressionMethod := cmDeflated; |
| 1947 |
GeneralPurposeBitFlag := 0; |
| 1948 |
CompressedSize := 0; |
| 1949 |
CRC32 := 0; |
| 1950 |
RelativeOffset := 0; |
| 1951 |
|
| 1952 |
MakeFullNames(SourceFileName, ArchiveDirectory, |
| 1953 |
FullSourceFileName, FullArchiveFileName); |
| 1954 |
|
| 1955 |
if mbDirectoryExists(FullSourceFileName) then begin |
| 1956 |
FullSourceFileName := IncludeTrailingPathDelimiter(FullSourceFileName); |
| 1957 |
end; |
| 1958 |
|
| 1959 |
Result.FileName := FullArchiveFileName; |
| 1960 |
Result.DiskFileName := FullSourceFileName; |
| 1961 |
end; |
| 1962 |
end; |
| 1963 |
{ -------------------------------------------------------------------------- } |
| 1964 |
procedure TAbZipArchive.DoExtractHelper(Index : Integer; const NewName : string); |
| 1965 |
begin |
| 1966 |
if Assigned(FExtractHelper) then |
| 1967 |
FExtractHelper(Self, ItemList[Index], NewName) |
| 1968 |
else |
| 1969 |
raise EAbZipNoExtraction.Create; |
| 1970 |
end; |
| 1971 |
{ -------------------------------------------------------------------------- } |
| 1972 |
procedure TAbZipArchive.DoExtractToStreamHelper(Index : Integer; |
| 1973 |
aStream : TStream); |
| 1974 |
begin |
| 1975 |
if Assigned(FExtractToStreamHelper) then |
| 1976 |
FExtractToStreamHelper(Self, ItemList[Index], aStream) |
| 1977 |
else |
| 1978 |
raise EAbZipNoExtraction.Create; |
| 1979 |
end; |
| 1980 |
{ -------------------------------------------------------------------------- } |
| 1981 |
procedure TAbZipArchive.DoTestHelper(Index : Integer); |
| 1982 |
begin |
| 1983 |
if Assigned(FTestHelper) then |
| 1984 |
FTestHelper(Self, ItemList[Index]) |
| 1985 |
else |
| 1986 |
raise EAbZipNoExtraction.Create; |
| 1987 |
end; |
| 1988 |
{ -------------------------------------------------------------------------- } |
| 1989 |
procedure TAbZipArchive.DoInsertHelper(Index : Integer; OutStream : TStream); |
| 1990 |
begin |
| 1991 |
if Assigned(FInsertHelper) then |
| 1992 |
FInsertHelper(Self, ItemList[Index], OutStream) |
| 1993 |
else |
| 1994 |
raise EAbZipNoInsertion.Create; |
| 1995 |
end; |
| 1996 |
{ -------------------------------------------------------------------------- } |
| 1997 |
procedure TAbZipArchive.DoInsertFromStreamHelper(Index : Integer; |
| 1998 |
OutStream : TStream); |
| 1999 |
begin |
| 2000 |
if Assigned(FInsertFromStreamHelper) then |
| 2001 |
FInsertFromStreamHelper(Self, ItemList[Index], OutStream, InStream) |
| 2002 |
else |
| 2003 |
raise EAbZipNoInsertion.Create; |
| 2004 |
end; |
| 2005 |
{ -------------------------------------------------------------------------- } |
| 2006 |
procedure TAbZipArchive.DoRequestDisk(const AMessage: string; var Abort : Boolean); |
| 2007 |
begin |
| 2008 |
{$IFDEF MSWINDOWS} |
| 2009 |
Abort := Windows.MessageBox( 0, PChar(AMessage), PChar(AbDiskRequestS), |
| 2010 |
MB_TASKMODAL or MB_OKCANCEL ) = IDCANCEL; |
| 2011 |
{$ENDIF} |
| 2012 |
{$IFDEF UnixDialogs} |
| 2013 |
{$IFDEF KYLIX} |
| 2014 |
Abort := QDialogs.MessageDlg(AbDiskRequestS, AMessage, mtWarning, |
| 2015 |
mbOKCancel, 0) = mrCancel; |
| 2016 |
{$ENDIF} |
| 2017 |
{$IFDEF LCL} |
| 2018 |
Abort := Dialogs.MessageDlg(AbDiskRequestS, AMessage, mtWarning, mbOKCancel, |
| 2019 |
0) = mrCancel; |
| 2020 |
{$ENDIF} |
| 2021 |
{$ELSE} |
| 2022 |
Abort := True; |
| 2023 |
{$ENDIF} |
| 2024 |
end; |
| 2025 |
{ -------------------------------------------------------------------------- } |
| 2026 |
procedure TAbZipArchive.DoRequestLastDisk( var Abort : Boolean ); |
| 2027 |
begin |
| 2028 |
Abort := False; |
| 2029 |
if Assigned( FOnRequestLastDisk ) then |
| 2030 |
FOnRequestLastDisk( Self, Abort ) |
| 2031 |
else |
| 2032 |
DoRequestDisk( AbLastDiskRequestS, Abort ); |
| 2033 |
end; |
| 2034 |
{ -------------------------------------------------------------------------- } |
| 2035 |
procedure TAbZipArchive.DoRequestNthDisk( Sender: TObject; |
| 2036 |
DiskNumber : Byte; |
| 2037 |
var Abort : Boolean ); |
| 2038 |
begin |
| 2039 |
Abort := False; |
| 2040 |
if Assigned( FOnRequestNthDisk ) then |
| 2041 |
FOnRequestNthDisk( Self, DiskNumber, Abort ) |
| 2042 |
else |
| 2043 |
DoRequestDisk( Format(AbDiskNumRequestS, [DiskNumber]), Abort ); |
| 2044 |
end; |
| 2045 |
{ -------------------------------------------------------------------------- } |
| 2046 |
procedure TAbZipArchive.DoRequestBlankDisk(Sender: TObject; var Abort : Boolean ); |
| 2047 |
begin |
| 2048 |
Abort := False; |
| 2049 |
FSpanned := True; |
| 2050 |
|
| 2051 |
if Assigned( FOnRequestBlankDisk ) then |
| 2052 |
FOnRequestBlankDisk( Self, Abort ) |
| 2053 |
else |
| 2054 |
DoRequestDisk( AbBlankDiskS, Abort ); |
| 2055 |
end; |
| 2056 |
{ -------------------------------------------------------------------------- } |
| 2057 |
procedure TAbZipArchive.DoRequestImage(Sender: TObject; ImageNumber : Integer; |
| 2058 |
var ImageName : string ; var Abort : Boolean); |
| 2059 |
begin |
| 2060 |
if Assigned(FOnRequestImage) then |
| 2061 |
FOnRequestImage(Self, ImageNumber, ImageName, Abort); |
| 2062 |
end; |
| 2063 |
{ -------------------------------------------------------------------------- } |
| 2064 |
procedure TAbZipArchive.ExtractItemAt(Index : Integer; const UseName : string); |
| 2065 |
begin |
| 2066 |
DoExtractHelper(Index, UseName); |
| 2067 |
end; |
| 2068 |
{ -------------------------------------------------------------------------- } |
| 2069 |
procedure TAbZipArchive.ExtractItemToStreamAt(Index : Integer; |
| 2070 |
aStream : TStream); |
| 2071 |
begin |
| 2072 |
DoExtractToStreamHelper(Index, aStream); |
| 2073 |
end; |
| 2074 |
{ -------------------------------------------------------------------------- } |
| 2075 |
function TAbZipArchive.FixName(const Value : string ) : string; |
| 2076 |
{-changes backslashes to forward slashes} |
| 2077 |
var |
| 2078 |
i : SmallInt; |
| 2079 |
lValue : string; |
| 2080 |
begin |
| 2081 |
lValue := Value; |
| 2082 |
{$IFDEF MSWINDOWS} |
| 2083 |
if DOSMode then begin |
| 2084 |
{Add the base directory to the filename before converting } |
| 2085 |
{the file spec to the short filespec format. } |
| 2086 |
if BaseDirectory <> '' then begin |
| 2087 |
{Does the filename contain a drive or a leading backslash? } |
| 2088 |
if not ((Pos(':', lValue) = 2) or (Pos(AbPathDelim, lValue) = 1)) then |
| 2089 |
{If not, add the BaseDirectory to the filename.} |
| 2090 |
lValue := AbAddBackSlash(BaseDirectory) + lValue; |
| 2091 |
end; |
| 2092 |
lValue := AbGetShortFileSpec( lValue ); |
| 2093 |
end; |
| 2094 |
{$ENDIF MSWINDOWS} |
| 2095 |
|
| 2096 |
{Zip files Always strip the drive path} |
| 2097 |
StoreOptions := StoreOptions + [soStripDrive]; |
| 2098 |
|
| 2099 |
{strip drive stuff} |
| 2100 |
if soStripDrive in StoreOptions then |
| 2101 |
AbStripDrive( lValue ); |
| 2102 |
|
| 2103 |
{check for a leading backslash} |
| 2104 |
if (Length(lValue) > 1) and (lValue[1] = AbPathDelim) then |
| 2105 |
System.Delete( lValue, 1, 1 ); |
| 2106 |
|
| 2107 |
if soStripPath in StoreOptions then begin |
| 2108 |
lValue := ExtractFileName( lValue ); |
| 2109 |
end; |
| 2110 |
|
| 2111 |
if soRemoveDots in StoreOptions then |
| 2112 |
AbStripDots( lValue ); |
| 2113 |
|
| 2114 |
for i := 1 to Length( lValue ) do |
| 2115 |
if lValue[i] = AbDosPathDelim then |
| 2116 |
lValue[i] := AbUnixPathDelim; |
| 2117 |
Result := lValue; |
| 2118 |
end; |
| 2119 |
{ -------------------------------------------------------------------------- } |
| 2120 |
function TAbZipArchive.GetItem( Index : Integer ) : TAbZipItem; |
| 2121 |
begin |
| 2122 |
Result := TAbZipItem(FItemList.Items[Index]); |
| 2123 |
end; |
| 2124 |
{ -------------------------------------------------------------------------- } |
| 2125 |
function TAbZipArchive.GetSupportsEmptyFolders: Boolean; |
| 2126 |
begin |
| 2127 |
Result := True; |
| 2128 |
end; |
| 2129 |
{ -------------------------------------------------------------------------- } |
| 2130 |
function TAbZipArchive.GetZipfileComment : AnsiString; |
| 2131 |
begin |
| 2132 |
Result := FInfo.ZipfileComment; |
| 2133 |
end; |
| 2134 |
{ -------------------------------------------------------------------------- } |
| 2135 |
procedure TAbZipArchive.LoadArchive; |
| 2136 |
var |
| 2137 |
Abort : Boolean; |
| 2138 |
TailPosition : int64; |
| 2139 |
Item : TAbZipItem; |
| 2140 |
Progress : Byte; |
| 2141 |
FileSignature : Longint; |
| 2142 |
Zip64Locator : TAbZip64EndOfCentralDirectoryLocator; |
| 2143 |
begin |
| 2144 |
Abort := False; |
| 2145 |
if FStream.Size = 0 then |
| 2146 |
Exit; |
| 2147 |
|
| 2148 |
{Get signature info} |
| 2149 |
FStream.Position := 0; |
| 2150 |
FStream.Read( FileSignature, sizeof( FileSignature ) ); |
| 2151 |
|
| 2152 |
{Get Executable Type; allow non-native stubs} |
| 2153 |
IsExecutable := |
| 2154 |
(LongRec(FileSignature).Lo = Ab_WindowsExeSignature) or |
| 2155 |
(FileSignature = Ab_LinuxExeSignature); |
| 2156 |
|
| 2157 |
{ try to locate central directory tail } |
| 2158 |
TailPosition := FindCentralDirectoryTail( FStream ); |
| 2159 |
if (TailPosition = -1) and (FileSignature = Ab_ZipSpannedSetSignature) and |
| 2160 |
FOwnsStream and AbDriveIsRemovable(ArchiveName) then begin |
| 2161 |
while TailPosition = -1 do begin |
| 2162 |
FreeAndNil(FStream); |
| 2163 |
DoRequestLastDisk(Abort); |
| 2164 |
if Abort then begin |
| 2165 |
FStatus := asInvalid; //TODO: Status updates are extremely inconsistent |
| 2166 |
raise EAbUserAbort.Create; |
| 2167 |
end; |
| 2168 |
FStream := TFileStreamEx.Create( ArchiveName, Mode ); |
| 2169 |
TailPosition := FindCentralDirectoryTail( FStream ); |
| 2170 |
end; |
| 2171 |
end; |
| 2172 |
|
| 2173 |
if TailPosition = -1 then begin |
| 2174 |
FStatus := asInvalid; |
| 2175 |
raise EAbZipInvalid.Create; |
| 2176 |
end; |
| 2177 |
|
| 2178 |
{ load the ZipDirectoryFileFooter } |
| 2179 |
FInfo.LoadFromStream(FStream); |
| 2180 |
|
| 2181 |
{ find Zip64 end of central directory locator; it will usually occur |
| 2182 |
immediately before the standard end of central directory record. |
| 2183 |
the actual Zip64 end of central directory may be on another disk } |
| 2184 |
if FInfo.IsZip64 then begin |
| 2185 |
Dec(TailPosition, SizeOf(Zip64Locator)); |
| 2186 |
repeat |
| 2187 |
if TailPosition < 0 then |
| 2188 |
raise EAbZipInvalid.Create; |
| 2189 |
FStream.Position := TailPosition; |
| 2190 |
FStream.ReadBuffer(Zip64Locator, SizeOf(Zip64Locator)); |
| 2191 |
Dec(TailPosition); |
| 2192 |
until Zip64Locator.Signature = Ab_Zip64EndCentralDirectoryLocatorSignature; |
| 2193 |
{ update current image number } |
| 2194 |
FInfo.DiskNumber := Zip64Locator.TotalDisks - 1; |
| 2195 |
end; |
| 2196 |
|
| 2197 |
{ setup spanning support and move to the start of the central directory } |
| 2198 |
FSpanned := FInfo.DiskNumber > 0; |
| 2199 |
|
| 2200 |
if FSpanned then begin |
| 2201 |
if FOwnsStream then begin |
| 2202 |
FStream := TAbSpanReadStream.Create( ArchiveName, FInfo.DiskNumber, FStream ); |
| 2203 |
TAbSpanReadStream(FStream).OnRequestImage := DoRequestImage; |
| 2204 |
TAbSpanReadStream(FStream).OnRequestNthDisk := DoRequestNthDisk; |
| 2205 |
if FInfo.IsZip64 then begin |
| 2206 |
TAbSpanReadStream(FStream).SeekImage(Zip64Locator.StartDiskNumber, |
| 2207 |
Zip64Locator.RelativeOffset); |
| 2208 |
FInfo.LoadZip64FromStream(FStream); |
| 2209 |
end; |
| 2210 |
TAbSpanReadStream(FStream).SeekImage(FInfo.StartDiskNumber, FInfo.DirectoryOffset); |
| 2211 |
end |
| 2212 |
else |
| 2213 |
raise EAbZipBadSpanStream.Create; |
| 2214 |
end |
| 2215 |
else begin |
| 2216 |
if FInfo.IsZip64 then begin |
| 2217 |
FStream.Position := Zip64Locator.RelativeOffset; |
| 2218 |
FInfo.LoadZip64FromStream(FStream); |
| 2219 |
end; |
| 2220 |
FStream.Position := FInfo.DirectoryOffset; |
| 2221 |
end; |
| 2222 |
|
| 2223 |
{ build Items list from central directory records } |
| 2224 |
FStubSize := High(LongWord); |
| 2225 |
while Count < FInfo.TotalEntries do begin |
| 2226 |
{ create new Item } |
| 2227 |
Item := TAbZipItem.Create; |
| 2228 |
try |
| 2229 |
Item.LoadFromStream(FStream); |
| 2230 |
Item.Action := aaNone; |
| 2231 |
FItemList.Add(Item); |
| 2232 |
except |
| 2233 |
Item.Free; |
| 2234 |
raise; |
| 2235 |
end; |
| 2236 |
|
| 2237 |
if IsExecutable and (Item.DiskNumberStart = 0) and |
| 2238 |
(Item.RelativeOffset < FStubSize) then |
| 2239 |
FStubSize := Item.RelativeOffset; |
| 2240 |
|
| 2241 |
Progress := (Count * 100) div FInfo.TotalEntries; |
| 2242 |
DoArchiveProgress( Progress, Abort ); |
| 2243 |
if Abort then begin |
| 2244 |
FStatus := asInvalid; |
| 2245 |
raise EAbUserAbort.Create; |
| 2246 |
end; |
| 2247 |
end; |
| 2248 |
|
| 2249 |
DoArchiveProgress(100, Abort); |
| 2250 |
FIsDirty := False; |
| 2251 |
end; |
| 2252 |
|
| 2253 |
{ -------------------------------------------------------------------------- } |
| 2254 |
procedure TAbZipArchive.PutItem( Index : Integer; Value : TAbZipItem ); |
| 2255 |
begin |
| 2256 |
FItemList.Items[Index] := Value; |
| 2257 |
end; |
| 2258 |
{ -------------------------------------------------------------------------- } |
| 2259 |
procedure TAbZipArchive.SaveArchive; |
| 2260 |
{builds a new archive and copies it to FStream} |
| 2261 |
var |
| 2262 |
APos : Int64; |
| 2263 |
Abort : Boolean; |
| 2264 |
MemStream : TMemoryStream; |
| 2265 |
HasDataDescriptor : Boolean; |
| 2266 |
i : LongWord; |
| 2267 |
LFH : TAbZipLocalFileHeader; |
| 2268 |
NewStream : TStream; |
| 2269 |
WorkingStream : TAbVirtualMemoryStream; |
| 2270 |
CurrItem : TAbZipItem; |
| 2271 |
Progress : Byte; |
| 2272 |
ATempName : String; |
| 2273 |
CreateArchive : Boolean; |
| 2274 |
begin |
| 2275 |
if Count = 0 then |
| 2276 |
Exit; |
| 2277 |
|
| 2278 |
{shouldn't be trying to overwrite an existing spanned archive} |
| 2279 |
if Spanned then begin |
| 2280 |
for i := 0 to Pred(Count) do |
| 2281 |
if ItemList[i].Action <> aaFailed then |
| 2282 |
ItemList[i].Action := aaNone; |
| 2283 |
FIsDirty := False; |
| 2284 |
raise EAbZipSpanOverwrite.Create; |
| 2285 |
end; |
| 2286 |
|
| 2287 |
{init new zip archive stream |
| 2288 |
can span only new archives, if SpanningThreshold > 0 or removable drive |
| 2289 |
spanning writes to original location, rather than writing to a temp stream first} |
| 2290 |
if FOwnsStream and (FStream.Size = 0) and not IsExecutable and |
| 2291 |
((SpanningThreshold > 0) or AbDriveIsRemovable(ArchiveName)) then begin |
| 2292 |
NewStream := TAbSpanWriteStream.Create(ArchiveName, FStream, SpanningThreshold); |
| 2293 |
FStream := nil; |
| 2294 |
TAbSpanWriteStream(NewStream).OnRequestBlankDisk := DoRequestBlankDisk; |
| 2295 |
TAbSpanWriteStream(NewStream).OnRequestImage := DoRequestImage; |
| 2296 |
end |
| 2297 |
else begin |
| 2298 |
CreateArchive:= FOwnsStream and (FStream.Size = 0) and (FStream is TFileStreamEx); |
| 2299 |
if CreateArchive then |
| 2300 |
NewStream := FStream |
| 2301 |
else begin |
| 2302 |
ATempName := GetTempName(FArchiveName); |
| 2303 |
NewStream := TFileStreamEx.Create(ATempName, fmCreate or fmShareDenyWrite); |
| 2304 |
end; |
| 2305 |
end; |
| 2306 |
|
| 2307 |
try {NewStream} |
| 2308 |
{copy the executable stub over to the output} |
| 2309 |
if IsExecutable then |
| 2310 |
NewStream.CopyFrom( FStream, StubSize ) |
| 2311 |
{assume spanned for spanning stream} |
| 2312 |
else if NewStream is TAbSpanWriteStream then |
| 2313 |
NewStream.Write(Ab_ZipSpannedSetSignature, |
| 2314 |
SizeOf(Ab_ZipSpannedSetSignature)); |
| 2315 |
|
| 2316 |
{build new zip archive from existing archive} |
| 2317 |
for i := 0 to pred( Count ) do begin |
| 2318 |
CurrItem := (ItemList[i] as TAbZipItem); |
| 2319 |
FCurrentItem := ItemList[i]; |
| 2320 |
|
| 2321 |
case CurrItem.Action of |
| 2322 |
aaNone, aaMove: begin |
| 2323 |
{just copy the file to new stream} |
| 2324 |
Assert(not (NewStream is TAbSpanWriteStream)); |
| 2325 |
FStream.Position := CurrItem.RelativeOffset; |
| 2326 |
CurrItem.DiskNumberStart := 0; |
| 2327 |
CurrItem.RelativeOffset := NewStream.Position; |
| 2328 |
{toss old local file header} |
| 2329 |
LFH := TAbZipLocalFileHeader.Create; |
| 2330 |
try {LFH} |
| 2331 |
LFH.LoadFromStream( FStream ); |
| 2332 |
if CurrItem.LFHExtraField.Count = 0 then |
| 2333 |
CurrItem.LFHExtraField.Assign(LFH.ExtraField); |
| 2334 |
finally {LFH} |
| 2335 |
LFH.Free; |
| 2336 |
end; {LFH} |
| 2337 |
{write out new local file header and append compressed data} |
| 2338 |
|
| 2339 |
CurrItem.SaveLFHToStream( NewStream ); |
| 2340 |
if (CurrItem.CompressedSize > 0) then |
| 2341 |
NewStream.CopyFrom(FStream, CurrItem.CompressedSize); |
| 2342 |
end; |
| 2343 |
|
| 2344 |
aaDelete: begin |
| 2345 |
{doing nothing omits file from new stream} |
| 2346 |
end; |
| 2347 |
|
| 2348 |
aaAdd, aaFreshen, aaReplace, aaStreamAdd: begin |
| 2349 |
{compress the file and add it to new stream} |
| 2350 |
try |
| 2351 |
if NewStream is TAbSpanWriteStream then |
| 2352 |
begin |
| 2353 |
WorkingStream := TAbVirtualMemoryStream.Create; |
| 2354 |
try {WorkingStream} |
| 2355 |
WorkingStream.SwapFileDirectory := FTempDir; |
| 2356 |
{compress the file} |
| 2357 |
if (CurrItem.Action = aaStreamAdd) then |
| 2358 |
DoInsertFromStreamHelper(i, WorkingStream) |
| 2359 |
else begin |
| 2360 |
DoInsertHelper(i, WorkingStream); |
| 2361 |
end; |
| 2362 |
{write local header} |
| 2363 |
MemStream := TMemoryStream.Create; |
| 2364 |
try |
| 2365 |
CurrItem.SaveLFHToStream(MemStream); |
| 2366 |
TAbSpanWriteStream(NewStream).WriteUnspanned( |
| 2367 |
MemStream.Memory^, MemStream.Size); |
| 2368 |
{calculate positions after the write in case it triggered a new span} |
| 2369 |
CurrItem.DiskNumberStart := TAbSpanWriteStream(NewStream).CurrentImage; |
| 2370 |
CurrItem.RelativeOffset := NewStream.Position - MemStream.Size; |
| 2371 |
finally |
| 2372 |
MemStream.Free; |
| 2373 |
end; |
| 2374 |
{copy compressed data} |
| 2375 |
NewStream.CopyFrom(WorkingStream, 0); |
| 2376 |
finally |
| 2377 |
WorkingStream.Free; |
| 2378 |
end; |
| 2379 |
end |
| 2380 |
else begin |
| 2381 |
{write local header} |
| 2382 |
CurrItem.DiskNumberStart := 0; |
| 2383 |
CurrItem.RelativeOffset := NewStream.Position; |
| 2384 |
CurrItem.UncompressedSize := mbFileSize(CurrItem.DiskFileName); |
| 2385 |
CurrItem.SaveLFHToStream(NewStream); |
| 2386 |
{compress the file} |
| 2387 |
if (CurrItem.Action = aaStreamAdd) then |
| 2388 |
DoInsertFromStreamHelper(i, NewStream) |
| 2389 |
else begin |
| 2390 |
DoInsertHelper(i, NewStream); |
| 2391 |
end; |
| 2392 |
{update local header} |
| 2393 |
APos:= NewStream.Position; |
| 2394 |
NewStream.Seek(CurrItem.RelativeOffset, soBeginning); |
| 2395 |
CurrItem.SaveLFHToStream(NewStream); |
| 2396 |
NewStream.Seek(APos, soBeginning); |
| 2397 |
end; |
| 2398 |
if CurrItem.IsEncrypted then |
| 2399 |
CurrItem.SaveDDToStream(NewStream); |
| 2400 |
except |
| 2401 |
on E : Exception do |
| 2402 |
begin |
| 2403 |
{ Exception was caused by a User Abort and Item Failure should not be called |
| 2404 |
Question: Do we want an New Event when this occurs or should the |
| 2405 |
exception just be re-raised [783614] } |
| 2406 |
if (E is EAbUserAbort) then |
| 2407 |
raise; |
| 2408 |
CurrItem.Action := aaDelete; |
| 2409 |
DoProcessItemFailure(CurrItem, ptAdd, ecFileOpenError, 0); |
| 2410 |
end; |
| 2411 |
end; |
| 2412 |
end; |
| 2413 |
end; { case } |
| 2414 |
|
| 2415 |
{ TODO: Check HasDataDescriptior behavior; seems like it's getting |
| 2416 |
written twice for encrypted files } |
| 2417 |
{Now add the data descriptor record to new stream} |
| 2418 |
HasDataDescriptor := (CurrItem.CompressionMethod = cmDeflated) and |
| 2419 |
((CurrItem.GeneralPurposeBitFlag and AbHasDataDescriptorFlag) <> 0); |
| 2420 |
if (CurrItem.Action <> aaDelete) and HasDataDescriptor then |
| 2421 |
CurrItem.SaveDDToStream(NewStream); |
| 2422 |
Progress := AbPercentage(9 * succ( i ), 10 * Count); |
| 2423 |
DoArchiveSaveProgress(Progress, Abort); |
| 2424 |
DoArchiveProgress(Progress, Abort); |
| 2425 |
if Abort then |
| 2426 |
raise EAbUserAbort.Create; |
| 2427 |
end; |
| 2428 |
|
| 2429 |
{write the central directory} |
| 2430 |
if NewStream is TAbSpanWriteStream then |
| 2431 |
FInfo.DiskNumber := TAbSpanWriteStream(NewStream).CurrentImage |
| 2432 |
else |
| 2433 |
FInfo.DiskNumber := 0; |
| 2434 |
FInfo.StartDiskNumber := FInfo.DiskNumber; |
| 2435 |
FInfo.DirectoryOffset := NewStream.Position; |
| 2436 |
FInfo.DirectorySize := 0; |
| 2437 |
FInfo.EntriesOnDisk := 0; |
| 2438 |
FInfo.TotalEntries := 0; |
| 2439 |
MemStream := TMemoryStream.Create; |
| 2440 |
try |
| 2441 |
{write central directory entries} |
| 2442 |
for i := 0 to Count - 1 do begin |
| 2443 |
if not (FItemList[i].Action in [aaDelete, aaFailed]) then begin |
| 2444 |
(FItemList[i] as TAbZipItem).SaveCDHToStream(MemStream); |
| 2445 |
if NewStream is TAbSpanWriteStream then begin |
| 2446 |
TAbSpanWriteStream(NewStream).WriteUnspanned(MemStream.Memory^, MemStream.Size); |
| 2447 |
{update tail info on span change} |
| 2448 |
if FInfo.DiskNumber <> TAbSpanWriteStream(NewStream).CurrentImage then begin |
| 2449 |
FInfo.DiskNumber := TAbSpanWriteStream(NewStream).CurrentImage; |
| 2450 |
FInfo.EntriesOnDisk := 0; |
| 2451 |
if FInfo.TotalEntries = 0 then begin |
| 2452 |
FInfo.StartDiskNumber := FInfo.DiskNumber; |
| 2453 |
FInfo.DirectoryOffset := NewStream.Position - MemStream.Size; |
| 2454 |
end; |
| 2455 |
end; |
| 2456 |
end |
| 2457 |
else |
| 2458 |
NewStream.WriteBuffer(MemStream.Memory^, MemStream.Size); |
| 2459 |
FInfo.DirectorySize := FInfo.DirectorySize + MemStream.Size; |
| 2460 |
FInfo.EntriesOnDisk := FInfo.EntriesOnDisk + 1; |
| 2461 |
FInfo.TotalEntries := FInfo.TotalEntries + 1; |
| 2462 |
MemStream.Clear; |
| 2463 |
end; |
| 2464 |
end; |
| 2465 |
{append the central directory footer} |
| 2466 |
FInfo.SaveToStream(MemStream, NewStream.Position); |
| 2467 |
if NewStream is TAbSpanWriteStream then begin |
| 2468 |
{update the footer if writing it would trigger a new span} |
| 2469 |
if not TAbSpanWriteStream(NewStream).WriteUnspanned(MemStream.Memory^, |
| 2470 |
MemStream.Size) then begin |
| 2471 |
FInfo.DiskNumber := TAbSpanWriteStream(NewStream).CurrentImage; |
| 2472 |
FInfo.EntriesOnDisk := 0; |
| 2473 |
FInfo.SaveToStream(NewStream); |
| 2474 |
end; |
| 2475 |
end |
| 2476 |
else |
| 2477 |
NewStream.WriteBuffer(MemStream.Memory^, MemStream.Size); |
| 2478 |
finally {MemStream} |
| 2479 |
MemStream.Free; |
| 2480 |
end; {MemStream} |
| 2481 |
|
| 2482 |
FSpanned := (FInfo.DiskNumber > 0); |
| 2483 |
|
| 2484 |
{update output stream} |
| 2485 |
if NewStream is TAbSpanWriteStream then begin |
| 2486 |
{zip has already been written to target location} |
| 2487 |
FStream := TAbSpanWriteStream(NewStream).ReleaseStream; |
| 2488 |
if Spanned then begin |
| 2489 |
{switch to read stream} |
| 2490 |
FStream := TAbSpanReadStream.Create(ArchiveName, FInfo.DiskNumber, FStream); |
| 2491 |
TAbSpanReadStream(FStream).OnRequestImage := DoRequestImage; |
| 2492 |
TAbSpanReadStream(FStream).OnRequestNthDisk := DoRequestNthDisk; |
| 2493 |
end |
| 2494 |
else begin |
| 2495 |
{replace spanned signature} |
| 2496 |
FStream.Position := 0; |
| 2497 |
FStream.Write(Ab_ZipPossiblySpannedSignature, |
| 2498 |
SizeOf(Ab_ZipPossiblySpannedSignature)); |
| 2499 |
end; |
| 2500 |
end |
| 2501 |
else begin |
| 2502 |
{copy new stream to FStream (non-spanned only)} |
| 2503 |
NewStream.Position := 0; |
| 2504 |
if (FStream is TMemoryStream) then |
| 2505 |
TMemoryStream(FStream).LoadFromStream(NewStream) |
| 2506 |
else begin |
| 2507 |
if FOwnsStream then |
| 2508 |
begin |
| 2509 |
{need new stream to write} |
| 2510 |
if CreateArchive then |
| 2511 |
NewStream := nil |
| 2512 |
else begin |
| 2513 |
FreeAndNil(FStream); |
| 2514 |
FreeAndNil(NewStream); |
| 2515 |
if (mbDeleteFile(FArchiveName) and mbRenameFile(ATempName, FArchiveName)) then |
| 2516 |
FStream := TFileStreamEx.Create(FArchiveName, fmOpenReadWrite or fmShareDenyWrite) |
| 2517 |
else |
| 2518 |
RaiseLastOSError; |
| 2519 |
end; |
| 2520 |
end |
| 2521 |
else begin |
| 2522 |
FStream.Size := 0; |
| 2523 |
FStream.Position := 0; |
| 2524 |
FStream.CopyFrom(NewStream, 0) |
| 2525 |
end; |
| 2526 |
end; |
| 2527 |
end; |
| 2528 |
|
| 2529 |
{update Items list} |
| 2530 |
for i := pred( Count ) downto 0 do begin |
| 2531 |
if FItemList[i].Action = aaDelete then |
| 2532 |
FItemList.Delete( i ) |
| 2533 |
else if FItemList[i].Action <> aaFailed then |
| 2534 |
FItemList[i].Action := aaNone; |
| 2535 |
end; |
| 2536 |
|
| 2537 |
DoArchiveSaveProgress( 100, Abort ); |
| 2538 |
DoArchiveProgress( 100, Abort ); |
| 2539 |
finally {NewStream} |
| 2540 |
if (FStream <> NewStream) then |
| 2541 |
NewStream.Free; |
| 2542 |
end; |
| 2543 |
end; |
| 2544 |
{ -------------------------------------------------------------------------- } |
| 2545 |
procedure TAbZipArchive.SetZipfileComment(const Value : AnsiString ); |
| 2546 |
begin |
| 2547 |
FInfo.FZipfileComment := Value; |
| 2548 |
FIsDirty := True; |
| 2549 |
end; |
| 2550 |
{ -------------------------------------------------------------------------- } |
| 2551 |
procedure TAbZipArchive.TestItemAt(Index : Integer); |
| 2552 |
begin |
| 2553 |
DoTestHelper(Index); |
| 2554 |
end; |
| 2555 |
|
| 2556 |
end. |