| 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: AbGzTyp.pas *} |
| 29 |
{*********************************************************} |
| 30 |
{* ABBREVIA: TAbGzipArchive, TAbGzipItem classes *} |
| 31 |
{*********************************************************} |
| 32 |
{* Misc. constants, types, and routines for working *} |
| 33 |
{* with GZip files *} |
| 34 |
{* See: RFC 1952 *} |
| 35 |
{* "GZIP file format specification version 4.3" *} |
| 36 |
{* for more information on GZip *} |
| 37 |
{* See "algorithm.doc" in Gzip source and "format.txt" *} |
| 38 |
{* on gzip.org for differences from RFC *} |
| 39 |
{*********************************************************} |
| 40 |
|
| 41 |
unit AbGzTyp; |
| 42 |
|
| 43 |
{$I AbDefine.inc} |
| 44 |
|
| 45 |
interface |
| 46 |
|
| 47 |
uses |
| 48 |
Classes, AbUtils, AbArcTyp, AbTarTyp; |
| 49 |
|
| 50 |
type |
| 51 |
{ pre-defined "operating system" (really more FILE system) |
| 52 |
types for the Gzip header } |
| 53 |
TAbGzFileSystem = |
| 54 |
(osFat, osAmiga, osVMS, osUnix, osVM_CMS, osAtariTOS, |
| 55 |
osHPFS, osMacintosh, osZSystem, osCP_M, osTOPS20, |
| 56 |
osNTFS, osQDOS, osAcornRISCOS, osVFAT, osMVS, osBeOS, |
| 57 |
osTandem, osTHEOS, osUnknown, osUndefined); |
| 58 |
|
| 59 |
type |
| 60 |
PAbGzHeader = ^TAbGzHeader; |
| 61 |
TAbGzHeader = packed record { SizeOf(TGzHeader) = 10} |
| 62 |
ID1 : Byte; { ID Byte, should always be $1F} |
| 63 |
ID2 : Byte; { ID Byte, should always be $8B} |
| 64 |
CompMethod : Byte; { compression method used} |
| 65 |
{ 0..7 reserved, 8 = deflate, others undefined as of this writing (4/27/2001)} |
| 66 |
Flags : Byte; { misc flags} |
| 67 |
{ Bit 0: FTEXT compressed file contains text, can be used for} |
| 68 |
{ cross platform line termination translation} |
| 69 |
{ Bit 1: FCONTINUATION file is a continuation of a multi-part gzip file} |
| 70 |
{ RFC 1952 says this is the header CRC16 flag, but gzip} |
| 71 |
{ reserves it and won't extract the file if this is set} |
| 72 |
{ header data includes part number after header record} |
| 73 |
{ Bit 2: FEXTRA header data contains Extra Data, starts after part} |
| 74 |
{ number (if any)} |
| 75 |
{ Bit 3: FNAME header data contains FileName, null terminated} |
| 76 |
{ string starting immediately after Extra Data (if any)} |
| 77 |
{ RFC 1952 says this is ISO 8859-1 encoded, but gzip} |
| 78 |
{ always uses the system encoding} |
| 79 |
{ Bit 4: FCOMMENT header data contains Comment, null terminated string} |
| 80 |
{ starting immediately after FileName (if any)} |
| 81 |
{ Bit 5: FENCRYPTED file is encrypted using zip-1.9 encryption } |
| 82 |
{ header data contains a 12-byte encryption header } |
| 83 |
{ starting immediately after Comment. Documented in} |
| 84 |
{ "algorithm.doc", but unsupported in gzip} |
| 85 |
{ Bits 6..7 are undefined and reserved as of this writing (8/25/2009)} |
| 86 |
ModTime : LongInt; { File Modification (Creation) time,} |
| 87 |
{ UNIX cdate format} |
| 88 |
XtraFlags : Byte; { additional flags} |
| 89 |
{ XtraFlags = 2 -- Deflate compressor used maximum compression algorithm} |
| 90 |
{ XtraFlags = 4 -- Deflate compressor used fastest algorithm} |
| 91 |
OS : Byte; { Operating system that created file,} |
| 92 |
{ see GZOsToStr routine for values} |
| 93 |
end; |
| 94 |
|
| 95 |
TAbGzTailRec = packed record |
| 96 |
CRC32 : LongInt; { crc for uncompressed data } |
| 97 |
ISize : LongWord; { size of uncompressed data } |
| 98 |
end; |
| 99 |
|
| 100 |
TAbGzExtraFieldSubID = array[0..1] of AnsiChar; |
| 101 |
|
| 102 |
type |
| 103 |
TAbGzipExtraField = class(TAbExtraField) |
| 104 |
private |
| 105 |
FGZHeader : PAbGzHeader; |
| 106 |
function GetID(aIndex : Integer): TAbGzExtraFieldSubID; |
| 107 |
protected |
| 108 |
procedure Changed; override; |
| 109 |
public |
| 110 |
constructor Create(aGZHeader : PAbGzHeader); |
| 111 |
procedure Delete(aID : TAbGzExtraFieldSubID); |
| 112 |
function Get(aID : TAbGzExtraFieldSubID; |
| 113 |
out aData : Pointer; out aDataSize : Word) : Boolean; |
| 114 |
procedure Put(aID : TAbGzExtraFieldSubID; const aData; aDataSize : Word); |
| 115 |
public |
| 116 |
property IDs[aIndex : Integer]: TAbGzExtraFieldSubID |
| 117 |
read GetID; |
| 118 |
end; |
| 119 |
|
| 120 |
TAbGzipItem = class(TAbArchiveItem) |
| 121 |
protected {private} |
| 122 |
FGZHeader : TAbGzHeader; |
| 123 |
FExtraField : TAbGzipExtraField; |
| 124 |
FFileComment : AnsiString; |
| 125 |
FRawFileName : AnsiString; |
| 126 |
|
| 127 |
protected |
| 128 |
function GetFileSystem: TAbGzFileSystem; |
| 129 |
function GetHasExtraField: Boolean; |
| 130 |
function GetHasFileComment: Boolean; |
| 131 |
function GetHasFileName: Boolean; |
| 132 |
function GetIsText: Boolean; |
| 133 |
|
| 134 |
procedure SetFileComment(const Value : AnsiString); |
| 135 |
procedure SetFileSystem(const Value: TAbGzFileSystem); |
| 136 |
procedure SetIsText(const Value: Boolean); |
| 137 |
|
| 138 |
function GetExternalFileAttributes : LongWord; override; |
| 139 |
function GetIsEncrypted : Boolean; override; |
| 140 |
function GetLastModFileDate : Word; override; |
| 141 |
function GetLastModFileTime : Word; override; |
| 142 |
function GetLastModTimeAsDateTime: TDateTime; override; |
| 143 |
function GetNativeLastModFileTime: Longint; override; |
| 144 |
|
| 145 |
procedure SetExternalFileAttributes( Value : LongWord ); override; |
| 146 |
procedure SetFileName(const Value : string); override; |
| 147 |
procedure SetIsEncrypted(Value : Boolean); override; |
| 148 |
procedure SetLastModFileDate(const Value : Word); override; |
| 149 |
procedure SetLastModFileTime(const Value : Word); override; |
| 150 |
procedure SetLastModTimeAsDateTime(const Value: TDateTime); override; |
| 151 |
|
| 152 |
procedure SaveGzHeaderToStream(AStream : TStream); |
| 153 |
procedure LoadGzHeaderFromStream(AStream : TStream); |
| 154 |
public |
| 155 |
property CompressionMethod : Byte |
| 156 |
read FGZHeader.CompMethod; |
| 157 |
|
| 158 |
property ExtraFlags : Byte {Default: 2} |
| 159 |
read FGZHeader.XtraFlags write FGZHeader.XtraFlags; |
| 160 |
|
| 161 |
property Flags : Byte |
| 162 |
read FGZHeader.Flags; |
| 163 |
|
| 164 |
property FileComment : AnsiString |
| 165 |
read FFileComment write SetFileComment; |
| 166 |
|
| 167 |
property FileSystem : TAbGzFileSystem {Default: osFat (Windows); osUnix (Linux)} |
| 168 |
read GetFileSystem write SetFileSystem; |
| 169 |
|
| 170 |
property ExtraField : TAbGzipExtraField |
| 171 |
read FExtraField; |
| 172 |
|
| 173 |
property IsEncrypted : Boolean |
| 174 |
read GetIsEncrypted; |
| 175 |
|
| 176 |
property HasExtraField : Boolean |
| 177 |
read GetHasExtraField; |
| 178 |
|
| 179 |
property HasFileName : Boolean |
| 180 |
read GetHasFileName; |
| 181 |
|
| 182 |
property HasFileComment : Boolean |
| 183 |
read GetHasFileComment; |
| 184 |
|
| 185 |
property IsText : Boolean |
| 186 |
read GetIsText write SetIsText; |
| 187 |
|
| 188 |
property GZHeader : TAbGzHeader |
| 189 |
read FGZHeader; |
| 190 |
|
| 191 |
constructor Create; |
| 192 |
destructor Destroy; override; |
| 193 |
end; |
| 194 |
|
| 195 |
TAbGzipStreamHelper = class(TAbArchiveStreamHelper) |
| 196 |
private |
| 197 |
function GetGzCRC: LongInt; |
| 198 |
function GetFileSize: LongInt; |
| 199 |
protected {private} |
| 200 |
FItem : TAbGzipItem; |
| 201 |
FTail : TAbGzTailRec; |
| 202 |
FArchive : TAbArchive; |
| 203 |
public |
| 204 |
constructor Create(AStream : TStream); overload; |
| 205 |
constructor Create(Archive : TAbArchive; AStream : TStream); overload; |
| 206 |
destructor Destroy; override; |
| 207 |
|
| 208 |
procedure ExtractItemData(AStream : TStream); override; |
| 209 |
function FindFirstItem : Boolean; override; |
| 210 |
function FindNextItem : Boolean; override; |
| 211 |
function SeekItem(Index : Integer): Boolean; override; |
| 212 |
procedure SeekToItemData; |
| 213 |
procedure WriteArchiveHeader; override; |
| 214 |
procedure WriteArchiveItem(AStream : TStream); override; |
| 215 |
procedure WriteArchiveTail; override; |
| 216 |
function GetItemCount : Integer; override; |
| 217 |
procedure ReadHeader; override; |
| 218 |
procedure ReadTail; override; |
| 219 |
|
| 220 |
property CRC : LongInt |
| 221 |
read GetGzCRC; |
| 222 |
property FileSize : LongInt |
| 223 |
read GetFileSize; |
| 224 |
property TailCRC : LongInt |
| 225 |
read FTail.CRC32; |
| 226 |
property TailSize : LongWord |
| 227 |
read FTail.ISize; |
| 228 |
end; |
| 229 |
|
| 230 |
TAbGzipArchiveState = (gsGzip, gsTar); |
| 231 |
|
| 232 |
TAbGzipArchive = class(TAbTarArchive) |
| 233 |
private |
| 234 |
FGZStream : TStream; { stream for GZip file} |
| 235 |
FGZItem : TAbArchiveList; { item in Gzip (only one, but need polymorphism of class)} |
| 236 |
FTarStream : TStream; { stream for possible contained Tar } |
| 237 |
FTarList : TAbArchiveList; { items in possible contained Tar } |
| 238 |
FTarAutoHandle: Boolean; |
| 239 |
FState : TAbGzipArchiveState; |
| 240 |
FIsGzippedTar : Boolean; |
| 241 |
|
| 242 |
procedure SetTarAutoHandle(const Value: Boolean); |
| 243 |
function GetIsGzippedTar: Boolean; |
| 244 |
procedure SwapToGzip; |
| 245 |
procedure SwapToTar; |
| 246 |
|
| 247 |
protected |
| 248 |
function CreateItem(const SourceFileName : string; |
| 249 |
const ArchiveDirectory : string): TAbArchiveItem; |
| 250 |
override; |
| 251 |
procedure ExtractItemAt(Index : Integer; const UseName : string); |
| 252 |
override; |
| 253 |
procedure ExtractItemToStreamAt(Index : Integer; aStream : TStream); |
| 254 |
override; |
| 255 |
procedure LoadArchive; |
| 256 |
override; |
| 257 |
procedure SaveArchive; |
| 258 |
override; |
| 259 |
procedure TestItemAt(Index : Integer); |
| 260 |
override; |
| 261 |
function FixName(const Value : string) : string; |
| 262 |
override; |
| 263 |
function GetSupportsEmptyFolders : Boolean; |
| 264 |
override; |
| 265 |
|
| 266 |
function GetItem(Index: Integer): TAbGzipItem; |
| 267 |
procedure PutItem(Index: Integer; const Value: TAbGzipItem); |
| 268 |
public {methods} |
| 269 |
constructor CreateFromStream(aStream : TStream; const aArchiveName : string); |
| 270 |
override; |
| 271 |
destructor Destroy; |
| 272 |
override; |
| 273 |
|
| 274 |
procedure DoSpanningMediaRequest(Sender : TObject; ImageNumber : Integer; |
| 275 |
var ImageName : string; var Abort : Boolean); override; |
| 276 |
|
| 277 |
property TarAutoHandle : Boolean |
| 278 |
read FTarAutoHandle write SetTarAutoHandle; |
| 279 |
|
| 280 |
property IsGzippedTar : Boolean |
| 281 |
read GetIsGzippedTar write FIsGzippedTar; |
| 282 |
|
| 283 |
property Items[Index : Integer] : TAbGzipItem |
| 284 |
read GetItem |
| 285 |
write PutItem; default; |
| 286 |
end; |
| 287 |
|
| 288 |
function VerifyGZip(Strm : TStream) : TAbArchiveType; |
| 289 |
function GZOsToStr(OS: Byte) : string; |
| 290 |
|
| 291 |
implementation |
| 292 |
|
| 293 |
uses |
| 294 |
SysUtils, BufStream, |
| 295 |
AbBitBkt, AbDfBase, AbZlibPrc, AbExcept, AbResString, AbProgress, |
| 296 |
AbVMStrm, DCOSUtils, DCClassesUtf8, DCConvertEncoding; |
| 297 |
|
| 298 |
const |
| 299 |
{ Header Signature Values} |
| 300 |
AB_GZ_HDR_ID1 = $1F; |
| 301 |
AB_GZ_HDR_ID2 = $8B; |
| 302 |
|
| 303 |
{ Test bits for TGzHeader.Flags field } |
| 304 |
AB_GZ_FLAG_FTEXT = $01; |
| 305 |
AB_GZ_FLAG_FCONTINUATION = $02; |
| 306 |
AB_GZ_FLAG_FEXTRA = $04; |
| 307 |
AB_GZ_FLAG_FNAME = $08; |
| 308 |
AB_GZ_FLAG_FCOMMENT = $10; |
| 309 |
AB_GZ_FLAG_FENCRYPTED = $20; |
| 310 |
AB_GZ_UNSUPPORTED_FLAGS = $E2; |
| 311 |
|
| 312 |
{ GZip OS source flags } |
| 313 |
AB_GZ_OS_ID_FAT = 0; |
| 314 |
AB_GZ_OS_ID_Amiga = 1; |
| 315 |
AB_GZ_OS_ID_VMS = 2; |
| 316 |
AB_GZ_OS_ID_Unix = 3; |
| 317 |
AB_GZ_OS_ID_VM_CMS = 4; |
| 318 |
AB_GZ_OS_ID_AtariTOS = 5; |
| 319 |
AB_GZ_OS_ID_HPFS = 6; |
| 320 |
AB_GZ_OS_ID_Macintosh = 7; |
| 321 |
AB_GZ_OS_ID_Z_System = 8; |
| 322 |
AB_GZ_OS_ID_CP_M = 9; |
| 323 |
AB_GZ_OS_ID_TOPS20 = 10; |
| 324 |
AB_GZ_OS_ID_NTFS = 11; |
| 325 |
AB_GZ_OS_ID_QDOS = 12; |
| 326 |
AB_GZ_OS_ID_AcornRISCOS = 13; |
| 327 |
AB_GZ_OS_ID_VFAT = 14; |
| 328 |
AB_GZ_OS_ID_MVS = 15; |
| 329 |
AB_GZ_OS_ID_BEOS = 16; |
| 330 |
AB_GZ_OS_ID_TANDEM = 17; |
| 331 |
AB_GZ_OS_ID_THEOS = 18; |
| 332 |
AB_GZ_OS_ID_unknown = 255; |
| 333 |
|
| 334 |
function GZOsToStr(OS: Byte) : string; |
| 335 |
{ |
| 336 |
Return a descriptive string for TGzHeader.OS field |
| 337 |
} |
| 338 |
begin |
| 339 |
case OS of |
| 340 |
AB_GZ_OS_ID_FAT : Result := AbGzOsFat; |
| 341 |
AB_GZ_OS_ID_Amiga : Result := AbGzOsAmiga; |
| 342 |
AB_GZ_OS_ID_VMS : Result := AbGzOsVMS; |
| 343 |
AB_GZ_OS_ID_Unix : Result := AbGzOsUnix; |
| 344 |
AB_GZ_OS_ID_VM_CMS : Result := AbGzOsVM_CMS; |
| 345 |
AB_GZ_OS_ID_AtariTOS : Result := AbGzOsAtari; |
| 346 |
AB_GZ_OS_ID_HPFS : Result := AbGzOsHPFS; |
| 347 |
AB_GZ_OS_ID_Macintosh : Result := AbGzOsMacintosh; |
| 348 |
AB_GZ_OS_ID_Z_System : Result := AbGzOsZ_System; |
| 349 |
AB_GZ_OS_ID_CP_M : Result := AbGzOsCP_M; |
| 350 |
AB_GZ_OS_ID_TOPS20 : Result := AbGzOsTOPS_20; |
| 351 |
AB_GZ_OS_ID_NTFS : Result := AbGzOsNTFS; |
| 352 |
AB_GZ_OS_ID_QDOS : Result := AbGzOsQDOS; |
| 353 |
AB_GZ_OS_ID_AcornRISCOS : Result := AbGzOsAcornRISCOS; |
| 354 |
AB_GZ_OS_ID_VFAT : Result := AbGzOsVFAT; |
| 355 |
AB_GZ_OS_ID_MVS : Result := AbGzOsMVS; |
| 356 |
AB_GZ_OS_ID_BEOS : Result := AbGzOsBeOS; |
| 357 |
AB_GZ_OS_ID_TANDEM : Result := AbGzOsTandem; |
| 358 |
AB_GZ_OS_ID_THEOS : Result := AbGzOsTHEOS; |
| 359 |
AB_GZ_OS_ID_unknown : Result := AbGzOsunknown; |
| 360 |
else |
| 361 |
Result := AbGzOsUndefined; |
| 362 |
end; |
| 363 |
end; |
| 364 |
|
| 365 |
|
| 366 |
function VerifyHeader(const Header : TAbGzHeader) : Boolean; |
| 367 |
begin |
| 368 |
{ check id fields and if deflated (only handle deflate anyway)} |
| 369 |
Result := (Header.ID1 = AB_GZ_HDR_ID1) and |
| 370 |
(Header.ID2 = AB_GZ_HDR_ID2) and |
| 371 |
(Header.CompMethod = 8 {deflate}); |
| 372 |
end; |
| 373 |
|
| 374 |
function VerifyGZip(Strm : TStream) : TAbArchiveType; |
| 375 |
var |
| 376 |
GHlp : TAbGzipStreamHelper; |
| 377 |
Hlpr : TAbDeflateHelper; |
| 378 |
PartialTarData : TMemoryStream; |
| 379 |
CurPos : Int64; |
| 380 |
begin |
| 381 |
Result := atUnknown; |
| 382 |
CurPos := Strm.Position; |
| 383 |
try |
| 384 |
Strm.Seek(0, soBeginning); |
| 385 |
|
| 386 |
{prepare for the try..finally} |
| 387 |
Hlpr := nil; |
| 388 |
PartialTarData := nil; |
| 389 |
|
| 390 |
GHlp := TAbGzipStreamHelper.Create(Strm); |
| 391 |
try |
| 392 |
{create the stream helper and read the item header} |
| 393 |
GHlp.ReadHeader; |
| 394 |
|
| 395 |
{ check id fields and if deflated (only handle deflate anyway)} |
| 396 |
if VerifyHeader(GHlp.FItem.FGZHeader) then begin |
| 397 |
Result := atGZip; { provisional } |
| 398 |
|
| 399 |
{ check if is actually a Gzipped Tar } |
| 400 |
{ partial extract contents, verify vs. Tar } |
| 401 |
PartialTarData := TMemoryStream.Create; |
| 402 |
GHlp.SeekToItemData; |
| 403 |
Hlpr := TAbDeflateHelper.Create; |
| 404 |
Hlpr.PartialSize := AB_TAR_RECORDSIZE * 4; |
| 405 |
PartialTarData.SetSize(Hlpr.PartialSize); |
| 406 |
Inflate(Strm, PartialTarData, Hlpr); |
| 407 |
|
| 408 |
{set to beginning of extracted data} |
| 409 |
PartialTarData.Position := 0; |
| 410 |
|
| 411 |
if (VerifyTar(PartialTarData) = atTar) then |
| 412 |
Result := atGZippedTar; |
| 413 |
end; |
| 414 |
finally |
| 415 |
GHlp.Free; |
| 416 |
Hlpr.Free; |
| 417 |
PartialTarData.Free; |
| 418 |
end; |
| 419 |
except |
| 420 |
on EReadError do |
| 421 |
Result := atUnknown; |
| 422 |
end; |
| 423 |
Strm.Position := CurPos; |
| 424 |
end; |
| 425 |
|
| 426 |
{ TAbGzipExtraField } |
| 427 |
|
| 428 |
constructor TAbGzipExtraField.Create(aGZHeader : PAbGzHeader); |
| 429 |
begin |
| 430 |
inherited Create; |
| 431 |
FGZHeader := aGZHeader; |
| 432 |
end; |
| 433 |
|
| 434 |
procedure TAbGzipExtraField.Changed; |
| 435 |
begin |
| 436 |
if Buffer = nil then |
| 437 |
FGzHeader.Flags := FGzHeader.Flags and not AB_GZ_FLAG_FEXTRA |
| 438 |
else |
| 439 |
FGzHeader.Flags := FGzHeader.Flags or AB_GZ_FLAG_FEXTRA; |
| 440 |
end; |
| 441 |
|
| 442 |
procedure TAbGzipExtraField.Delete(aID : TAbGzExtraFieldSubID); |
| 443 |
begin |
| 444 |
inherited Delete(Word(aID)); |
| 445 |
end; |
| 446 |
|
| 447 |
function TAbGzipExtraField.GetID(aIndex : Integer): TAbGzExtraFieldSubID; |
| 448 |
begin |
| 449 |
Result := TAbGzExtraFieldSubID(inherited IDs[aIndex]); |
| 450 |
end; |
| 451 |
|
| 452 |
function TAbGzipExtraField.Get(aID : TAbGzExtraFieldSubID; out aData : Pointer; |
| 453 |
out aDataSize : Word) : Boolean; |
| 454 |
begin |
| 455 |
Result := inherited Get(Word(aID), aData, aDataSize); |
| 456 |
end; |
| 457 |
|
| 458 |
procedure TAbGzipExtraField.Put(aID : TAbGzExtraFieldSubID; const aData; aDataSize : Word); |
| 459 |
begin |
| 460 |
inherited Put(Word(aID), aData, aDataSize); |
| 461 |
end; |
| 462 |
|
| 463 |
|
| 464 |
{ TAbGzipStreamHelper } |
| 465 |
|
| 466 |
constructor TAbGzipStreamHelper.Create(AStream: TStream); |
| 467 |
begin |
| 468 |
inherited Create(AStream); |
| 469 |
FItem := TAbGzipItem.Create; |
| 470 |
end; |
| 471 |
|
| 472 |
constructor TAbGzipStreamHelper.Create(Archive : TAbArchive; AStream: TStream); |
| 473 |
begin |
| 474 |
Create(AStream); |
| 475 |
FArchive := Archive; |
| 476 |
end; |
| 477 |
|
| 478 |
destructor TAbGzipStreamHelper.Destroy; |
| 479 |
begin |
| 480 |
FItem.Free; |
| 481 |
inherited; |
| 482 |
end; |
| 483 |
|
| 484 |
function ReadCStringInStream(AStream: TStream): AnsiString; |
| 485 |
{ |
| 486 |
locate next instance of a null character in a stream |
| 487 |
leaves stream positioned just past that, |
| 488 |
or at end of stream if not found or null is last byte in stream. |
| 489 |
Result is the entire read string. |
| 490 |
} |
| 491 |
const |
| 492 |
BuffSiz = 1024; |
| 493 |
var |
| 494 |
Buff : array [0..BuffSiz-1] of AnsiChar; |
| 495 |
Len, DataRead : LongInt; |
| 496 |
begin |
| 497 |
{ basically what this is supposed to do is...} |
| 498 |
{ |
| 499 |
repeat |
| 500 |
AStream.Read(C, 1); |
| 501 |
Result := Result + C; |
| 502 |
until (AStream.Position = AStream.Size) or (C = #0); |
| 503 |
} |
| 504 |
Result := ''; |
| 505 |
repeat |
| 506 |
DataRead := AStream.Read(Buff, BuffSiz - 1); |
| 507 |
Buff[DataRead] := #0; |
| 508 |
Len := StrLen(Buff); |
| 509 |
if Len > 0 then begin |
| 510 |
SetLength(Result, Length(Result) + Len); |
| 511 |
Move(Buff, Result[Length(Result) - Len + 1], Len); |
| 512 |
end; |
| 513 |
if Len < DataRead then begin |
| 514 |
AStream.Seek(Len - DataRead + 1, soCurrent); |
| 515 |
Break; |
| 516 |
end; |
| 517 |
until DataRead = 0; |
| 518 |
end; |
| 519 |
|
| 520 |
procedure TAbGzipStreamHelper.SeekToItemData; |
| 521 |
{find end of header data, including FileName etc.} |
| 522 |
begin |
| 523 |
{** Seek to Compressed Data **} |
| 524 |
FStream.Seek(0, soBeginning); |
| 525 |
FItem.LoadGzHeaderFromStream(FStream); |
| 526 |
end; |
| 527 |
|
| 528 |
procedure TAbGzipStreamHelper.ExtractItemData(AStream: TStream); |
| 529 |
var |
| 530 |
Helper : TAbDeflateHelper; |
| 531 |
begin |
| 532 |
Helper := TAbDeflateHelper.Create; |
| 533 |
try |
| 534 |
if (AStream is TAbBitBucketStream) then |
| 535 |
Helper.Options := Helper.Options or dfc_TestOnly; |
| 536 |
FItem.CRC32 := Inflate(FStream, AStream, Helper); |
| 537 |
FItem.UncompressedSize := Helper.NormalSize; |
| 538 |
finally |
| 539 |
Helper.Free; |
| 540 |
end; |
| 541 |
end; |
| 542 |
|
| 543 |
function TAbGzipStreamHelper.FindFirstItem: Boolean; |
| 544 |
var |
| 545 |
GZH : TAbGzHeader; |
| 546 |
DataRead : Integer; |
| 547 |
begin |
| 548 |
Result := False; |
| 549 |
FStream.Seek(0, soBeginning); |
| 550 |
DataRead := FStream.Read(GZH, SizeOf(TAbGzHeader)); |
| 551 |
if (DataRead = SizeOf(TAbGzHeader)) and VerifyHeader(GZH) then begin |
| 552 |
FItem.FGZHeader := GZH; |
| 553 |
Result := True; |
| 554 |
end; |
| 555 |
FStream.Seek(0, soBeginning); |
| 556 |
end; |
| 557 |
|
| 558 |
function TAbGzipStreamHelper.FindNextItem: Boolean; |
| 559 |
begin |
| 560 |
{ only one item in a GZip } |
| 561 |
Result := False; |
| 562 |
end; |
| 563 |
|
| 564 |
function TAbGzipStreamHelper.SeekItem(Index: Integer): Boolean; |
| 565 |
begin |
| 566 |
if Index > 0 then |
| 567 |
Result := False |
| 568 |
else |
| 569 |
Result := FindFirstItem; |
| 570 |
end; |
| 571 |
|
| 572 |
procedure TAbGzipStreamHelper.WriteArchiveHeader; |
| 573 |
begin |
| 574 |
FItem.SaveGzHeaderToStream(FStream); |
| 575 |
end; |
| 576 |
|
| 577 |
procedure TAbGzipStreamHelper.WriteArchiveItem(AStream: TStream); |
| 578 |
var |
| 579 |
Helper : TAbDeflateHelper; |
| 580 |
begin |
| 581 |
Helper := TAbDeflateHelper.Create; |
| 582 |
try |
| 583 |
case FArchive.CompressionLevel of |
| 584 |
1 : Helper.PKZipOption := 's'; |
| 585 |
3 : Helper.PKZipOption := 'f'; |
| 586 |
6 : Helper.PKZipOption := 'n'; |
| 587 |
9 : Helper.PKZipOption := 'x'; |
| 588 |
end; |
| 589 |
FItem.CRC32 := Deflate(AStream, FStream, Helper); |
| 590 |
FItem.UncompressedSize := AStream.Size; |
| 591 |
finally |
| 592 |
Helper.Free; |
| 593 |
end; |
| 594 |
end; |
| 595 |
|
| 596 |
procedure TAbGzipStreamHelper.WriteArchiveTail; |
| 597 |
var |
| 598 |
Tail : TAbGzTailRec; |
| 599 |
begin |
| 600 |
Tail.CRC32 := FItem.CRC32; |
| 601 |
Tail.ISize := FItem.UncompressedSize; |
| 602 |
FStream.Write(Tail, SizeOf(TAbGzTailRec)); |
| 603 |
end; |
| 604 |
|
| 605 |
function TAbGzipStreamHelper.GetItemCount: Integer; |
| 606 |
begin |
| 607 |
{ only one item in a gzip } |
| 608 |
Result := 1; |
| 609 |
end; |
| 610 |
|
| 611 |
procedure TAbGzipStreamHelper.ReadHeader; |
| 612 |
begin |
| 613 |
FItem.LoadGzHeaderFromStream(FStream); |
| 614 |
end; |
| 615 |
|
| 616 |
procedure TAbGzipStreamHelper.ReadTail; |
| 617 |
begin |
| 618 |
FStream.Read(FTail, SizeOf(TAbGzTailRec)); |
| 619 |
end; |
| 620 |
|
| 621 |
function TAbGzipStreamHelper.GetGzCRC: LongInt; |
| 622 |
begin |
| 623 |
Result := FItem.CRC32; |
| 624 |
end; |
| 625 |
|
| 626 |
function TAbGzipStreamHelper.GetFileSize: LongInt; |
| 627 |
begin |
| 628 |
Result := FItem.UncompressedSize; |
| 629 |
end; |
| 630 |
|
| 631 |
{ TAbGzipItem } |
| 632 |
|
| 633 |
constructor TAbGzipItem.Create; |
| 634 |
begin |
| 635 |
inherited Create; |
| 636 |
|
| 637 |
{ default ID fields } |
| 638 |
FGzHeader.ID1 := AB_GZ_HDR_ID1; |
| 639 |
FGzHeader.ID2 := AB_GZ_HDR_ID2; |
| 640 |
|
| 641 |
{ compression method } |
| 642 |
FGzHeader.CompMethod := 8; { deflate } |
| 643 |
|
| 644 |
{ Maxium Compression } |
| 645 |
FGzHeader.XtraFlags := 2; |
| 646 |
|
| 647 |
FFileName := ''; |
| 648 |
FFileComment := ''; |
| 649 |
FExtraField := TAbGzipExtraField.Create(@FGzHeader); |
| 650 |
|
| 651 |
{ source OS ID } |
| 652 |
{$IFDEF LINUX } {assume EXT2 system } |
| 653 |
FGzHeader.OS := AB_GZ_OS_ID_Unix; |
| 654 |
{$ENDIF LINUX } |
| 655 |
{$IFDEF MSWINDOWS } {assume FAT system } |
| 656 |
FGzHeader.OS := AB_GZ_OS_ID_FAT; |
| 657 |
{$ENDIF MSWINDOWS } |
| 658 |
end; |
| 659 |
|
| 660 |
destructor TAbGzipItem.Destroy; |
| 661 |
begin |
| 662 |
FExtraField.Free; |
| 663 |
inherited; |
| 664 |
end; |
| 665 |
|
| 666 |
function TAbGzipItem.GetExternalFileAttributes: LongWord; |
| 667 |
begin |
| 668 |
{ GZip has no provision for storing attributes } |
| 669 |
Result := 0; |
| 670 |
end; |
| 671 |
|
| 672 |
function TAbGzipItem.GetFileSystem: TAbGzFileSystem; |
| 673 |
begin |
| 674 |
case FGzHeader.OS of |
| 675 |
0..18: Result := TAbGzFileSystem(FGzHeader.OS); |
| 676 |
255: Result := osUnknown; |
| 677 |
else |
| 678 |
Result := osUndefined; |
| 679 |
end; { case } |
| 680 |
end; |
| 681 |
|
| 682 |
function TAbGzipItem.GetIsEncrypted: Boolean; |
| 683 |
begin |
| 684 |
Result := (FGZHeader.Flags and AB_GZ_FLAG_FENCRYPTED) = AB_GZ_FLAG_FENCRYPTED; |
| 685 |
end; |
| 686 |
|
| 687 |
function TAbGzipItem.GetHasExtraField: Boolean; |
| 688 |
begin |
| 689 |
Result := (FGZHeader.Flags and AB_GZ_FLAG_FEXTRA) = AB_GZ_FLAG_FEXTRA; |
| 690 |
end; |
| 691 |
|
| 692 |
function TAbGzipItem.GetHasFileComment: Boolean; |
| 693 |
begin |
| 694 |
Result := (FGZHeader.Flags and AB_GZ_FLAG_FCOMMENT) = AB_GZ_FLAG_FCOMMENT; |
| 695 |
end; |
| 696 |
|
| 697 |
function TAbGzipItem.GetHasFileName: Boolean; |
| 698 |
begin |
| 699 |
Result := (FGZHeader.Flags and AB_GZ_FLAG_FNAME) = AB_GZ_FLAG_FNAME; |
| 700 |
end; |
| 701 |
|
| 702 |
function TAbGzipItem.GetIsText: Boolean; |
| 703 |
begin |
| 704 |
Result := (FGZHeader.Flags and AB_GZ_FLAG_FTEXT) = AB_GZ_FLAG_FTEXT; |
| 705 |
end; |
| 706 |
|
| 707 |
function TAbGzipItem.GetLastModFileDate: Word; |
| 708 |
begin |
| 709 |
{ convert to local DOS file Date } |
| 710 |
Result := LongRec(AbDateTimeToDosFileDate(LastModTimeAsDateTime)).Hi; |
| 711 |
end; |
| 712 |
|
| 713 |
function TAbGzipItem.GetLastModFileTime: Word; |
| 714 |
begin |
| 715 |
{ convert to local DOS file Time } |
| 716 |
Result := LongRec(AbDateTimeToDosFileDate(LastModTimeAsDateTime)).Lo; |
| 717 |
end; |
| 718 |
|
| 719 |
function TAbGzipItem.GetLastModTimeAsDateTime: TDateTime; |
| 720 |
begin |
| 721 |
Result := AbUnixTimeToLocalDateTime(FGZHeader.ModTime); |
| 722 |
end; |
| 723 |
|
| 724 |
function TAbGzipItem.GetNativeLastModFileTime: Longint; |
| 725 |
{$IFDEF MSWINDOWS} |
| 726 |
var |
| 727 |
DateTime: TDateTime; |
| 728 |
{$ENDIF} |
| 729 |
begin |
| 730 |
Result := FGZHeader.ModTime; |
| 731 |
|
| 732 |
{$IFDEF MSWINDOWS} |
| 733 |
DateTime := AbUnixTimeToLocalDateTime(Result); |
| 734 |
Result := AbDateTimeToDosFileDate(DateTime); |
| 735 |
{$ENDIF} |
| 736 |
end; |
| 737 |
|
| 738 |
procedure TAbGzipItem.LoadGzHeaderFromStream(AStream: TStream); |
| 739 |
var |
| 740 |
LenW : Word; |
| 741 |
begin |
| 742 |
FGzHeader.ID1 := 0; |
| 743 |
|
| 744 |
AStream.Read(FGzHeader, SizeOf(TAbGzHeader)); |
| 745 |
if not VerifyHeader(FGzHeader) then |
| 746 |
Exit; |
| 747 |
|
| 748 |
{ Skip part number, if any } |
| 749 |
if (FGzHeader.Flags and AB_GZ_FLAG_FCONTINUATION) = AB_GZ_FLAG_FCONTINUATION then |
| 750 |
AStream.Seek(SizeOf(Word), soCurrent); |
| 751 |
|
| 752 |
if HasExtraField then begin |
| 753 |
{ get length of extra data } |
| 754 |
AStream.Read(LenW, SizeOf(Word)); |
| 755 |
FExtraField.LoadFromStream(AStream, LenW); |
| 756 |
end |
| 757 |
else |
| 758 |
FExtraField.Clear; |
| 759 |
|
| 760 |
{ Get Filename, if any } |
| 761 |
if HasFileName then begin |
| 762 |
FRawFileName := ReadCStringInStream(AStream); |
| 763 |
FFileName := CeRawToUtf8(FRawFileName) |
| 764 |
end |
| 765 |
else |
| 766 |
FFileName := 'unknown'; |
| 767 |
|
| 768 |
{ any comment present? } |
| 769 |
if HasFileComment then |
| 770 |
FFileComment := ReadCStringInStream(AStream) |
| 771 |
else |
| 772 |
FFileComment := ''; |
| 773 |
|
| 774 |
|
| 775 |
{Assert: stream should now be located at start of compressed data } |
| 776 |
{If file was compressed with 3.3 spec this will be invalid so use with care} |
| 777 |
CompressedSize := AStream.Size - AStream.Position - SizeOf(TAbGzTailRec); |
| 778 |
|
| 779 |
FDiskFileName := FileName; |
| 780 |
AbUnfixName(FDiskFileName); |
| 781 |
Action := aaNone; |
| 782 |
Tagged := False; |
| 783 |
end; |
| 784 |
|
| 785 |
procedure TAbGzipItem.SaveGzHeaderToStream(AStream: TStream); |
| 786 |
var |
| 787 |
LenW : Word; |
| 788 |
begin |
| 789 |
{ default ID fields } |
| 790 |
FGzHeader.ID1 := AB_GZ_HDR_ID1; |
| 791 |
FGzHeader.ID2 := AB_GZ_HDR_ID2; |
| 792 |
|
| 793 |
{ compression method } |
| 794 |
FGzHeader.CompMethod := 8; { deflate } |
| 795 |
|
| 796 |
{ reset unsupported flags } |
| 797 |
FGzHeader.Flags := FGzHeader.Flags and not AB_GZ_UNSUPPORTED_FLAGS; |
| 798 |
|
| 799 |
{ main header data } |
| 800 |
AStream.Write(FGzHeader, SizeOf(TAbGzHeader)); |
| 801 |
|
| 802 |
{ add extra field if any } |
| 803 |
if HasExtraField then begin |
| 804 |
LenW := Length(FExtraField.Buffer); |
| 805 |
AStream.Write(LenW, SizeOf(LenW)); |
| 806 |
if LenW > 0 then |
| 807 |
AStream.Write(FExtraField.Buffer[0], LenW); |
| 808 |
end; |
| 809 |
|
| 810 |
{ add filename if any (and include final #0 from string) } |
| 811 |
if HasFileName then |
| 812 |
AStream.Write(FRawFileName[1], Length(FRawFileName) + 1); |
| 813 |
|
| 814 |
{ add file comment if any (and include final #0 from string) } |
| 815 |
if HasFileComment then |
| 816 |
AStream.Write(FFileComment[1], Length(FFileComment) + 1); |
| 817 |
end; |
| 818 |
|
| 819 |
procedure TAbGzipItem.SetExternalFileAttributes(Value: LongWord); |
| 820 |
begin |
| 821 |
{ do nothing } |
| 822 |
end; |
| 823 |
|
| 824 |
procedure TAbGzipItem.SetFileComment(const Value: AnsiString); |
| 825 |
begin |
| 826 |
FFileComment := Value; |
| 827 |
if FFileComment <> '' then |
| 828 |
FGzHeader.Flags := FGzHeader.Flags or AB_GZ_FLAG_FCOMMENT |
| 829 |
else |
| 830 |
FGzHeader.Flags := FGzHeader.Flags and not AB_GZ_FLAG_FCOMMENT; |
| 831 |
end; |
| 832 |
|
| 833 |
procedure TAbGzipItem.SetFileName(const Value: string); |
| 834 |
begin |
| 835 |
FFileName := Value; |
| 836 |
FRawFileName := CeUtf8ToSys(Value); |
| 837 |
if Value <> '' then |
| 838 |
FGzHeader.Flags := FGzHeader.Flags or AB_GZ_FLAG_FNAME |
| 839 |
else |
| 840 |
FGzHeader.Flags := FGzHeader.Flags and not AB_GZ_FLAG_FNAME; |
| 841 |
end; |
| 842 |
|
| 843 |
procedure TAbGzipItem.SetFileSystem(const Value: TAbGzFileSystem); |
| 844 |
begin |
| 845 |
if Value = osUnknown then |
| 846 |
FGzHeader.OS := 255 |
| 847 |
else |
| 848 |
FGzHeader.OS := Ord(Value); |
| 849 |
end; |
| 850 |
|
| 851 |
procedure TAbGzipItem.SetIsEncrypted(Value: Boolean); |
| 852 |
begin |
| 853 |
{ do nothing } |
| 854 |
end; |
| 855 |
|
| 856 |
procedure TAbGzipItem.SetIsText(const Value: Boolean); |
| 857 |
begin |
| 858 |
if Value then |
| 859 |
FGzHeader.Flags := FGzHeader.Flags or AB_GZ_FLAG_FTEXT |
| 860 |
else |
| 861 |
FGzHeader.Flags := FGzHeader.Flags and not AB_GZ_FLAG_FTEXT; |
| 862 |
end; |
| 863 |
|
| 864 |
procedure TAbGzipItem.SetLastModFileDate(const Value: Word); |
| 865 |
begin |
| 866 |
{ replace date, keep existing time } |
| 867 |
LastModTimeAsDateTime := |
| 868 |
EncodeDate( |
| 869 |
Value shr 9 + 1980, |
| 870 |
Value shr 5 and 15, |
| 871 |
Value and 31) + |
| 872 |
Frac(LastModTimeAsDateTime); |
| 873 |
end; |
| 874 |
|
| 875 |
procedure TAbGzipItem.SetLastModFileTime(const Value: Word); |
| 876 |
begin |
| 877 |
{ keep current date, replace time } |
| 878 |
LastModTimeAsDateTime := |
| 879 |
Trunc(LastModTimeAsDateTime) + |
| 880 |
EncodeTime( |
| 881 |
Value shr 11, |
| 882 |
Value shr 5 and 63, |
| 883 |
Value and 31 shl 1, 0); |
| 884 |
end; |
| 885 |
|
| 886 |
procedure TAbGzipItem.SetLastModTimeAsDateTime(const Value: TDateTime); |
| 887 |
begin |
| 888 |
FGZHeader.ModTime := AbLocalDateTimeToUnixTime(Value); |
| 889 |
end; |
| 890 |
|
| 891 |
{ TAbGzipArchive } |
| 892 |
|
| 893 |
constructor TAbGzipArchive.CreateFromStream(aStream : TStream; |
| 894 |
const aArchiveName : string); |
| 895 |
begin |
| 896 |
inherited CreateFromStream(aStream, aArchiveName); |
| 897 |
FState := gsGzip; |
| 898 |
FGZStream := FStream; |
| 899 |
FGZItem := FItemList; |
| 900 |
FTarStream := TAbVirtualMemoryStream.Create; |
| 901 |
FTarList := TAbArchiveList.Create(True); |
| 902 |
end; |
| 903 |
|
| 904 |
procedure TAbGzipArchive.SwapToTar; |
| 905 |
begin |
| 906 |
FStream := FTarStream; |
| 907 |
FItemList := FTarList; |
| 908 |
FState := gsTar; |
| 909 |
end; |
| 910 |
|
| 911 |
procedure TAbGzipArchive.SwapToGzip; |
| 912 |
begin |
| 913 |
FStream := FGzStream; |
| 914 |
FItemList := FGzItem; |
| 915 |
FState := gsGzip; |
| 916 |
end; |
| 917 |
|
| 918 |
function TAbGzipArchive.CreateItem(const SourceFileName : string; |
| 919 |
const ArchiveDirectory : string): TAbArchiveItem; |
| 920 |
var |
| 921 |
GzItem : TAbGzipItem; |
| 922 |
FullSourceFileName, FullArchiveFileName: String; |
| 923 |
begin |
| 924 |
if IsGZippedTar and TarAutoHandle then begin |
| 925 |
SwapToTar; |
| 926 |
Result := inherited CreateItem(SourceFileName, ArchiveDirectory); |
| 927 |
end |
| 928 |
else begin |
| 929 |
SwapToGzip; |
| 930 |
GzItem := TAbGzipItem.Create; |
| 931 |
try |
| 932 |
MakeFullNames(SourceFileName, ArchiveDirectory, |
| 933 |
FullSourceFileName, FullArchiveFileName); |
| 934 |
|
| 935 |
GzItem.FileName := FullArchiveFileName; |
| 936 |
GzItem.DiskFileName := FullSourceFileName; |
| 937 |
|
| 938 |
Result := GzItem; |
| 939 |
except |
| 940 |
Result := nil; |
| 941 |
raise; |
| 942 |
end; |
| 943 |
end; |
| 944 |
end; |
| 945 |
|
| 946 |
destructor TAbGzipArchive.Destroy; |
| 947 |
begin |
| 948 |
SwapToGzip; |
| 949 |
FTarList.Free; |
| 950 |
FTarStream.Free; |
| 951 |
inherited Destroy; |
| 952 |
end; |
| 953 |
|
| 954 |
|
| 955 |
procedure TAbGzipArchive.ExtractItemAt(Index: Integer; |
| 956 |
const UseName: string); |
| 957 |
var |
| 958 |
OutStream : TStream; |
| 959 |
CurItem : TAbGzipItem; |
| 960 |
begin |
| 961 |
if IsGZippedTar and TarAutoHandle then begin |
| 962 |
SwapToTar; |
| 963 |
inherited ExtractItemAt(Index, UseName); |
| 964 |
end |
| 965 |
else begin |
| 966 |
SwapToGzip; |
| 967 |
if Index > 0 then Index := 0; { only one item in a GZip} |
| 968 |
|
| 969 |
CurItem := TAbGzipItem(ItemList[Index]); |
| 970 |
|
| 971 |
OutStream := TFileStreamEx.Create(UseName, fmCreate or fmShareDenyNone); |
| 972 |
try |
| 973 |
try {OutStream} |
| 974 |
ExtractItemToStreamAt(Index, OutStream); |
| 975 |
finally {OutStream} |
| 976 |
OutStream.Free; |
| 977 |
end; {OutStream} |
| 978 |
AbSetFileTime(UseName, CurItem.LastModTimeAsDateTime); |
| 979 |
AbSetFileAttr(UseName, CurItem.NativeFileAttributes); |
| 980 |
except |
| 981 |
on E : EAbUserAbort do begin |
| 982 |
FStatus := asInvalid; |
| 983 |
if mbFileExists(UseName) then |
| 984 |
mbDeleteFile(UseName); |
| 985 |
raise; |
| 986 |
end else begin |
| 987 |
if mbFileExists(UseName) then |
| 988 |
mbDeleteFile(UseName); |
| 989 |
raise; |
| 990 |
end; |
| 991 |
end; |
| 992 |
end; |
| 993 |
end; |
| 994 |
|
| 995 |
procedure TAbGzipArchive.ExtractItemToStreamAt(Index: Integer; |
| 996 |
aStream: TStream); |
| 997 |
var |
| 998 |
GzHelp : TAbGzipStreamHelper; |
| 999 |
ProxyStream : TAbProgressReadStream; |
| 1000 |
begin |
| 1001 |
if IsGzippedTar and TarAutoHandle then begin |
| 1002 |
SwapToTar; |
| 1003 |
inherited ExtractItemToStreamAt(Index, aStream); |
| 1004 |
end |
| 1005 |
else begin |
| 1006 |
SwapToGzip; |
| 1007 |
{ note Index ignored as there's only one item in a GZip } |
| 1008 |
|
| 1009 |
ProxyStream := TAbProgressReadStream.Create(FGzStream, FOnProgress); |
| 1010 |
try |
| 1011 |
GZHelp := TAbGzipStreamHelper.Create(ProxyStream); |
| 1012 |
try |
| 1013 |
FGzStream.Seek(0, soBeginning); |
| 1014 |
|
| 1015 |
{ read GZip Header } |
| 1016 |
GzHelp.ReadHeader; |
| 1017 |
|
| 1018 |
repeat |
| 1019 |
{ extract copy data from GZip} |
| 1020 |
GzHelp.ExtractItemData(aStream); |
| 1021 |
|
| 1022 |
{ Get validation data } |
| 1023 |
GzHelp.ReadTail; |
| 1024 |
|
| 1025 |
{$IFDEF STRICTGZIP} |
| 1026 |
{ According to |
| 1027 |
http://www.gzip.org/zlib/rfc1952.txt |
| 1028 |
|
| 1029 |
A compliant gzip compressor should calculate and set the CRC32 and ISIZE. |
| 1030 |
However, a compliant decompressor should not check these values. |
| 1031 |
|
| 1032 |
If you want to check the the values of the CRC32 and ISIZE in a GZIP file |
| 1033 |
when decompressing enable the STRICTGZIP define contained in AbDefine.inc } |
| 1034 |
|
| 1035 |
{ validate against CRC } |
| 1036 |
if GzHelp.FItem.Crc32 <> GzHelp.TailCRC then |
| 1037 |
raise EAbGzipBadCRC.Create; |
| 1038 |
|
| 1039 |
{ validate against file size } |
| 1040 |
if GzHelp.FItem.UncompressedSize <> GZHelp.TailSize then |
| 1041 |
raise EAbGzipBadFileSize.Create; |
| 1042 |
{$ENDIF} |
| 1043 |
{ try concatenated streams } |
| 1044 |
GzHelp.ReadHeader; |
| 1045 |
until not VerifyHeader(GZHelp.FItem.FGzHeader); |
| 1046 |
finally |
| 1047 |
GzHelp.Free; |
| 1048 |
end; |
| 1049 |
finally |
| 1050 |
ProxyStream.Free; |
| 1051 |
end; |
| 1052 |
end; |
| 1053 |
end; |
| 1054 |
|
| 1055 |
function TAbGzipArchive.FixName(const Value: string): string; |
| 1056 |
{ fix up fileaname for storage } |
| 1057 |
begin |
| 1058 |
if FState = gsTar then |
| 1059 |
Result := inherited FixName( Value ) |
| 1060 |
else begin |
| 1061 |
{GZip files Always strip the file path} |
| 1062 |
StoreOptions := StoreOptions + [soStripDrive, soStripPath]; |
| 1063 |
Result := ''; |
| 1064 |
if Value <> '' then |
| 1065 |
Result := ExtractFileName(Value); |
| 1066 |
end; |
| 1067 |
end; |
| 1068 |
|
| 1069 |
function TAbGzipArchive.GetIsGzippedTar: Boolean; |
| 1070 |
begin |
| 1071 |
Result := FIsGzippedTar; |
| 1072 |
end; |
| 1073 |
|
| 1074 |
function TAbGzipArchive.GetItem(Index: Integer): TAbGzipItem; |
| 1075 |
begin |
| 1076 |
Result := nil; |
| 1077 |
if Index = 0 then |
| 1078 |
Result := TAbGzipItem(FItemList.Items[Index]); |
| 1079 |
end; |
| 1080 |
|
| 1081 |
function TAbGzipArchive.GetSupportsEmptyFolders : Boolean; |
| 1082 |
begin |
| 1083 |
Result := IsGzippedTar and TarAutoHandle; |
| 1084 |
end; |
| 1085 |
|
| 1086 |
procedure TAbGzipArchive.LoadArchive; |
| 1087 |
var |
| 1088 |
GzHelp : TAbGzipStreamHelper; |
| 1089 |
Item : TAbGzipItem; |
| 1090 |
Abort : Boolean; |
| 1091 |
begin |
| 1092 |
SwapToGzip; |
| 1093 |
if FGzStream.Size > 0 then begin |
| 1094 |
GzHelp := TAbGzipStreamHelper.Create(FGzStream); |
| 1095 |
try |
| 1096 |
if GzHelp.FindFirstItem then begin |
| 1097 |
Item := TAbGzipItem.Create; |
| 1098 |
Item.LoadGzHeaderFromStream(FGzStream); |
| 1099 |
FGzStream.Seek(-SizeOf(TAbGzTailRec), soEnd); |
| 1100 |
GZHelp.ReadTail; |
| 1101 |
Item.CRC32 := GZHelp.TailCRC; |
| 1102 |
Item.UncompressedSize := GZHelp.TailSize; |
| 1103 |
|
| 1104 |
Item.Action := aaNone; |
| 1105 |
FGZItem.Add(Item); |
| 1106 |
|
| 1107 |
if IsGzippedTar and TarAutoHandle then begin |
| 1108 |
{ extract Tar and set stream up } |
| 1109 |
FGzStream.Seek(0, soBeginning); |
| 1110 |
GzHelp.ReadHeader; |
| 1111 |
repeat |
| 1112 |
GzHelp.ExtractItemData(FTarStream); |
| 1113 |
GzHelp.ReadTail; |
| 1114 |
GzHelp.ReadHeader; |
| 1115 |
until not VerifyHeader(GZHelp.FItem.FGzHeader); |
| 1116 |
SwapToTar; |
| 1117 |
inherited LoadArchive; |
| 1118 |
end; |
| 1119 |
end; |
| 1120 |
|
| 1121 |
DoArchiveProgress(100, Abort); |
| 1122 |
FIsDirty := False; |
| 1123 |
finally |
| 1124 |
{ Clean Up } |
| 1125 |
GzHelp.Free; |
| 1126 |
end; |
| 1127 |
end; |
| 1128 |
end; |
| 1129 |
|
| 1130 |
procedure TAbGzipArchive.PutItem(Index: Integer; const Value: TAbGzipItem); |
| 1131 |
begin |
| 1132 |
if Index = 0 then |
| 1133 |
FItemList.Items[Index] := Value; |
| 1134 |
end; |
| 1135 |
|
| 1136 |
procedure TAbGzipArchive.SaveArchive; |
| 1137 |
var |
| 1138 |
InGzHelp, OutGzHelp : TAbGzipStreamHelper; |
| 1139 |
CompStream : TDeflateStream; |
| 1140 |
Abort : Boolean; |
| 1141 |
i : Integer; |
| 1142 |
NewStream : TStream; |
| 1143 |
UncompressedStream : TStream; |
| 1144 |
CurItem : TAbGzipItem; |
| 1145 |
CreateArchive : Boolean; |
| 1146 |
ATempName : String; |
| 1147 |
Tail : TAbGzTailRec; |
| 1148 |
begin |
| 1149 |
{prepare for the try..finally} |
| 1150 |
OutGzHelp := nil; |
| 1151 |
NewStream := nil; |
| 1152 |
|
| 1153 |
try |
| 1154 |
InGzHelp := TAbGzipStreamHelper.Create(Self, FGzStream); |
| 1155 |
|
| 1156 |
try |
| 1157 |
{init new archive stream} |
| 1158 |
CreateArchive:= FOwnsStream and (FGzStream.Size = 0) and (FGzStream is TFileStreamEx); |
| 1159 |
if CreateArchive then |
| 1160 |
NewStream := FGzStream |
| 1161 |
else begin |
| 1162 |
ATempName := GetTempName(FArchiveName); |
| 1163 |
NewStream := TFileStreamEx.Create(ATempName, fmCreate or fmShareDenyWrite); |
| 1164 |
end; |
| 1165 |
OutGzHelp := TAbGzipStreamHelper.Create(Self, NewStream); |
| 1166 |
|
| 1167 |
{ save the Tar data } |
| 1168 |
if IsGzippedTar and TarAutoHandle then begin |
| 1169 |
SwapToTar; |
| 1170 |
if FGZItem.Count = 0 then begin |
| 1171 |
CurItem := TAbGzipItem.Create; |
| 1172 |
FGZItem.Add(CurItem); |
| 1173 |
end; |
| 1174 |
CurItem := FGZItem[0] as TAbGzipItem; |
| 1175 |
CurItem.Action := aaNone; |
| 1176 |
CurItem.LastModTimeAsDateTime := Now; |
| 1177 |
CurItem.SaveGzHeaderToStream(NewStream); |
| 1178 |
FTarStream.Position := 0; |
| 1179 |
CompStream := TDeflateStream.Create(CompressionLevel, NewStream); |
| 1180 |
try |
| 1181 |
FTargetStream := TWriteBufStream.Create(CompStream, $40000); |
| 1182 |
try |
| 1183 |
inherited SaveArchive; |
| 1184 |
finally |
| 1185 |
FreeAndNil(FTargetStream); |
| 1186 |
end; |
| 1187 |
CurItem.CRC32 := LongInt(CompStream.Hash); |
| 1188 |
CurItem.UncompressedSize := CompStream.Seek(0, soCurrent); |
| 1189 |
finally |
| 1190 |
CompStream.Free; |
| 1191 |
end; |
| 1192 |
Tail.CRC32 := CurItem.CRC32; |
| 1193 |
Tail.ISize := CurItem.UncompressedSize; |
| 1194 |
NewStream.Write(Tail, SizeOf(TAbGzTailRec)); |
| 1195 |
end |
| 1196 |
else begin |
| 1197 |
SwapToGzip; |
| 1198 |
|
| 1199 |
{build new archive from existing archive} |
| 1200 |
for i := 0 to pred(Count) do begin |
| 1201 |
FCurrentItem := ItemList[i]; |
| 1202 |
CurItem := TAbGzipItem(ItemList[i]); |
| 1203 |
InGzHelp.SeekToItemData; |
| 1204 |
|
| 1205 |
case CurItem.Action of |
| 1206 |
aaNone, aaMove : begin |
| 1207 |
{just copy the file to new stream} |
| 1208 |
CurItem.SaveGzHeaderToStream(NewStream); |
| 1209 |
InGzHelp.SeekToItemData; |
| 1210 |
NewStream.CopyFrom(FGZStream, FGZStream.Size - FGZStream.Position); |
| 1211 |
end; |
| 1212 |
|
| 1213 |
aaDelete: {doing nothing omits file from new stream} ; |
| 1214 |
|
| 1215 |
aaAdd, aaFreshen, aaReplace, aaStreamAdd: begin |
| 1216 |
try |
| 1217 |
if (CurItem.Action = aaStreamAdd) then begin |
| 1218 |
{ adding from a stream } |
| 1219 |
CurItem.SaveGzHeaderToStream(NewStream); |
| 1220 |
CurItem.UncompressedSize := InStream.Size; |
| 1221 |
OutGzHelp.WriteArchiveItem(InStream); |
| 1222 |
OutGzHelp.WriteArchiveTail; |
| 1223 |
end |
| 1224 |
else begin |
| 1225 |
CurItem.LastModTimeAsDateTime := AbGetFileTime(CurItem.DiskFileName); |
| 1226 |
UncompressedStream := TFileStreamEx.Create(CurItem.DiskFileName, |
| 1227 |
fmOpenRead or fmShareDenyWrite); |
| 1228 |
|
| 1229 |
try |
| 1230 |
CurItem.UncompressedSize := UncompressedStream.Size; |
| 1231 |
CurItem.SaveGzHeaderToStream(NewStream); |
| 1232 |
CompStream := TDeflateStream.Create(CompressionLevel, NewStream); |
| 1233 |
try |
| 1234 |
with TAbProgressWriteStream.Create(CompStream, CurItem.UncompressedSize, OnProgress) do |
| 1235 |
try |
| 1236 |
CopyFrom(UncompressedStream, CurItem.UncompressedSize); |
| 1237 |
finally |
| 1238 |
Free; |
| 1239 |
end; |
| 1240 |
CurItem.CRC32 := LongInt(CompStream.Hash); |
| 1241 |
CurItem.UncompressedSize := CompStream.Seek(0, soCurrent); |
| 1242 |
finally |
| 1243 |
CompStream.Free; |
| 1244 |
end; |
| 1245 |
Tail.CRC32 := CurItem.CRC32; |
| 1246 |
Tail.ISize := CurItem.UncompressedSize; |
| 1247 |
NewStream.Write(Tail, SizeOf(TAbGzTailRec)); |
| 1248 |
|
| 1249 |
finally {UncompressedStream} |
| 1250 |
UncompressedStream.Free; |
| 1251 |
end; {UncompressedStream} |
| 1252 |
end; |
| 1253 |
except |
| 1254 |
ItemList[i].Action := aaDelete; |
| 1255 |
DoProcessItemFailure(ItemList[i], ptAdd, ecFileOpenError, 0); |
| 1256 |
end; |
| 1257 |
end; |
| 1258 |
end; {case} |
| 1259 |
end; { for } |
| 1260 |
end; |
| 1261 |
finally |
| 1262 |
InGzHelp.Free; |
| 1263 |
end; |
| 1264 |
|
| 1265 |
{copy new stream to FStream} |
| 1266 |
SwapToGzip; |
| 1267 |
NewStream.Position := 0; |
| 1268 |
if (FStream is TMemoryStream) then |
| 1269 |
TMemoryStream(FStream).LoadFromStream(NewStream) |
| 1270 |
else begin |
| 1271 |
{ need new stream to write } |
| 1272 |
if CreateArchive then |
| 1273 |
NewStream := nil |
| 1274 |
else begin |
| 1275 |
if FOwnsStream then |
| 1276 |
begin |
| 1277 |
{need new stream to write} |
| 1278 |
if CreateArchive then |
| 1279 |
NewStream := nil |
| 1280 |
else begin |
| 1281 |
FGZStream := nil; |
| 1282 |
FreeAndNil(FStream); |
| 1283 |
FreeAndNil(NewStream); |
| 1284 |
if (mbDeleteFile(FArchiveName) and mbRenameFile(ATempName, FArchiveName)) then |
| 1285 |
FStream := TFileStreamEx.Create(FArchiveName, fmOpenReadWrite or fmShareDenyWrite) |
| 1286 |
else begin |
| 1287 |
RaiseLastOSError; |
| 1288 |
end; |
| 1289 |
FGZStream := FStream; |
| 1290 |
end; |
| 1291 |
end |
| 1292 |
else begin |
| 1293 |
FStream.Size := 0; |
| 1294 |
FStream.Position := 0; |
| 1295 |
FStream.CopyFrom(NewStream, 0) |
| 1296 |
end; |
| 1297 |
end; |
| 1298 |
end; |
| 1299 |
|
| 1300 |
{update Items list} |
| 1301 |
for i := pred( Count ) downto 0 do begin |
| 1302 |
if ItemList[i].Action = aaDelete then |
| 1303 |
FItemList.Delete( i ) |
| 1304 |
else if ItemList[i].Action <> aaFailed then |
| 1305 |
ItemList[i].Action := aaNone; |
| 1306 |
end; |
| 1307 |
|
| 1308 |
if IsGzippedTar and TarAutoHandle then |
| 1309 |
SwapToTar; |
| 1310 |
|
| 1311 |
DoArchiveSaveProgress( 100, Abort ); |
| 1312 |
DoArchiveProgress( 100, Abort ); |
| 1313 |
finally {NewStream} |
| 1314 |
OutGzHelp.Free; |
| 1315 |
if (FStream <> NewStream) then |
| 1316 |
NewStream.Free; |
| 1317 |
end; |
| 1318 |
end; |
| 1319 |
|
| 1320 |
procedure TAbGzipArchive.SetTarAutoHandle(const Value: Boolean); |
| 1321 |
begin |
| 1322 |
if Value then |
| 1323 |
SwapToTar |
| 1324 |
else |
| 1325 |
SwapToGzip; |
| 1326 |
FTarAutoHandle := Value; |
| 1327 |
end; |
| 1328 |
|
| 1329 |
procedure TAbGzipArchive.TestItemAt(Index: Integer); |
| 1330 |
var |
| 1331 |
SavePos : LongInt; |
| 1332 |
GZType : TAbArchiveType; |
| 1333 |
BitBucket : TAbBitBucketStream; |
| 1334 |
GZHelp : TAbGzipStreamHelper; |
| 1335 |
begin |
| 1336 |
if IsGzippedTar and TarAutoHandle then begin |
| 1337 |
inherited TestItemAt(Index); |
| 1338 |
end |
| 1339 |
else begin |
| 1340 |
{ note Index ignored as there's only one item in a GZip } |
| 1341 |
SavePos := FGzStream.Position; |
| 1342 |
GZType := VerifyGZip(FGZStream); |
| 1343 |
if not (GZType in [atGZip, atGZippedTar]) then |
| 1344 |
raise EAbGzipInvalid.Create; |
| 1345 |
|
| 1346 |
BitBucket := nil; |
| 1347 |
GZHelp := nil; |
| 1348 |
try |
| 1349 |
BitBucket := TAbBitBucketStream.Create(1024); |
| 1350 |
GZHelp := TAbGzipStreamHelper.Create(FGZStream); |
| 1351 |
|
| 1352 |
Index := 0; |
| 1353 |
FGZStream.Seek(0, soBeginning); |
| 1354 |
GZHelp.ReadHeader; |
| 1355 |
repeat |
| 1356 |
GZHelp.ExtractItemData(BitBucket); |
| 1357 |
GZHelp.ReadTail; |
| 1358 |
|
| 1359 |
{ validate against CRC } |
| 1360 |
if GzHelp.FItem.Crc32 <> GZHelp.TailCRC then |
| 1361 |
raise EAbGzipBadCRC.Create; |
| 1362 |
|
| 1363 |
Inc(Index); |
| 1364 |
GzHelp.ReadHeader; |
| 1365 |
until not VerifyHeader(GZHelp.FItem.FGzHeader); |
| 1366 |
|
| 1367 |
if (Index < 2) then |
| 1368 |
begin |
| 1369 |
{ validate against file size } |
| 1370 |
if GzHelp.FItem.UncompressedSize <> GZHelp.TailSize then |
| 1371 |
raise EAbGzipBadFileSize.Create; |
| 1372 |
end; |
| 1373 |
|
| 1374 |
finally |
| 1375 |
GZHelp.Free; |
| 1376 |
BitBucket.Free; |
| 1377 |
end; |
| 1378 |
|
| 1379 |
FGzStream.Position := SavePos; |
| 1380 |
end; |
| 1381 |
end; |
| 1382 |
|
| 1383 |
procedure TAbGzipArchive.DoSpanningMediaRequest(Sender: TObject; |
| 1384 |
ImageNumber: Integer; var ImageName: string; var Abort: Boolean); |
| 1385 |
begin |
| 1386 |
Abort := False; |
| 1387 |
end; |
| 1388 |
|
| 1389 |
end. |