| 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 |
* |
| 24 |
* ***** END LICENSE BLOCK ***** *) |
| 25 |
|
| 26 |
{*********************************************************} |
| 27 |
{* ABBREVIA: AbZipPrc.pas *} |
| 28 |
{*********************************************************} |
| 29 |
{* ABBREVIA: TABZipHelper class *} |
| 30 |
{*********************************************************} |
| 31 |
|
| 32 |
unit AbZipPrc; |
| 33 |
|
| 34 |
{$I AbDefine.inc} |
| 35 |
|
| 36 |
interface |
| 37 |
|
| 38 |
uses |
| 39 |
Classes, |
| 40 |
AbZipTyp; |
| 41 |
|
| 42 |
procedure AbZip( Sender : TAbZipArchive; Item : TAbZipItem; |
| 43 |
OutStream : TStream ); |
| 44 |
|
| 45 |
procedure AbZipFromStream(Sender : TAbZipArchive; Item : TAbZipItem; |
| 46 |
OutStream, InStream : TStream); |
| 47 |
|
| 48 |
implementation |
| 49 |
|
| 50 |
uses |
| 51 |
SysUtils, |
| 52 |
AbArcTyp, |
| 53 |
AbExcept, |
| 54 |
AbUtils, |
| 55 |
AbDfCryS, |
| 56 |
AbVMStrm, |
| 57 |
AbDfBase, |
| 58 |
AbZlibPrc, |
| 59 |
AbZipxPrc, |
| 60 |
DCcrc32, |
| 61 |
DCClassesUtf8; |
| 62 |
|
| 63 |
|
| 64 |
{ ========================================================================== } |
| 65 |
procedure DoDeflate(Archive : TAbZipArchive; Item : TAbZipItem; OutStream, InStream : TStream); |
| 66 |
const |
| 67 |
DEFLATE_NORMAL_MASK = $00; |
| 68 |
DEFLATE_MAXIMUM_MASK = $02; |
| 69 |
DEFLATE_FAST_MASK = $04; |
| 70 |
DEFLATE_SUPERFAST_MASK = $06; |
| 71 |
var |
| 72 |
Hlpr : TAbDeflateHelper; |
| 73 |
begin |
| 74 |
Item.CompressionMethod := cmDeflated; |
| 75 |
|
| 76 |
Hlpr := TAbDeflateHelper.Create; |
| 77 |
|
| 78 |
{anything dealing with store options, etc. should already be done.} |
| 79 |
|
| 80 |
try {Hlpr} |
| 81 |
Hlpr.StreamSize := InStream.Size; |
| 82 |
|
| 83 |
{ set deflation level desired } |
| 84 |
Hlpr.PKZipOption := '0'; |
| 85 |
|
| 86 |
case Archive.DeflationOption of |
| 87 |
doNormal : begin |
| 88 |
Hlpr.PKZipOption := 'n'; |
| 89 |
Item.GeneralPurposeBitFlag := |
| 90 |
Item.GeneralPurposeBitFlag or DEFLATE_NORMAL_MASK; |
| 91 |
end; |
| 92 |
|
| 93 |
doMaximum : begin |
| 94 |
Hlpr.PKZipOption := 'x'; |
| 95 |
Item.GeneralPurposeBitFlag := |
| 96 |
Item.GeneralPurposeBitFlag or DEFLATE_MAXIMUM_MASK; |
| 97 |
end; |
| 98 |
|
| 99 |
doFast : begin |
| 100 |
Hlpr.PKZipOption := 'f'; |
| 101 |
Item.GeneralPurposeBitFlag := |
| 102 |
Item.GeneralPurposeBitFlag or DEFLATE_FAST_MASK; |
| 103 |
end; |
| 104 |
|
| 105 |
doSuperFast : begin |
| 106 |
Hlpr.PKZipOption := 's'; |
| 107 |
Item.GeneralPurposeBitFlag := |
| 108 |
Item.GeneralPurposeBitFlag or DEFLATE_SUPERFAST_MASK; |
| 109 |
end; |
| 110 |
end; |
| 111 |
|
| 112 |
{ attach progress notification method } |
| 113 |
Hlpr.OnProgressStep := Archive.DoInflateProgress; |
| 114 |
|
| 115 |
{ provide encryption check value } |
| 116 |
Item.CRC32 := Deflate(InStream, OutStream, Hlpr); |
| 117 |
|
| 118 |
finally {Hlpr} |
| 119 |
Hlpr.Free; |
| 120 |
end; {Hlpr} |
| 121 |
end; |
| 122 |
{ ========================================================================== } |
| 123 |
procedure DoStore(Archive : TAbZipArchive; Item : TAbZipItem; OutStream, InStream : TStream); |
| 124 |
var |
| 125 |
CRC32 : UInt32; |
| 126 |
Percent : LongInt; |
| 127 |
LastPercent : LongInt; |
| 128 |
InSize : Int64; |
| 129 |
DataRead : Int64; |
| 130 |
Total : Int64; |
| 131 |
Abort : Boolean; |
| 132 |
Buffer : array [0..16383] of byte; |
| 133 |
begin |
| 134 |
{ setup } |
| 135 |
Item.CompressionMethod := cmStored; |
| 136 |
Abort := False; |
| 137 |
CRC32 := 0; |
| 138 |
Total := 0; |
| 139 |
Percent := 0; |
| 140 |
LastPercent := 0; |
| 141 |
InSize := InStream.Size; |
| 142 |
|
| 143 |
{ get first bufferful } |
| 144 |
DataRead := InStream.Read(Buffer, SizeOf(Buffer)); |
| 145 |
{ while more data has been read and we're not told to bail } |
| 146 |
while (DataRead <> 0) and not Abort do begin |
| 147 |
{report the progress} |
| 148 |
if Assigned(Archive.OnProgress) then begin |
| 149 |
Total := Total + DataRead; |
| 150 |
Percent := Round((100.0 * Total) / InSize); |
| 151 |
if (LastPercent <> Percent) then |
| 152 |
Archive.OnProgress(Percent, Abort); |
| 153 |
LastPercent := Percent; |
| 154 |
end; |
| 155 |
|
| 156 |
{ update CRC} |
| 157 |
CRC32 := crc32_16bytes(Buffer, DataRead, CRC32); |
| 158 |
|
| 159 |
{ write data (encrypting if needed) } |
| 160 |
OutStream.WriteBuffer(Buffer, DataRead); |
| 161 |
|
| 162 |
{ get next bufferful } |
| 163 |
DataRead := InStream.Read(Buffer, SizeOf(Buffer)); |
| 164 |
end; |
| 165 |
|
| 166 |
{ finish CRC calculation } |
| 167 |
Item.CRC32 := LongInt(CRC32); |
| 168 |
|
| 169 |
{ show final progress increment } |
| 170 |
if (Percent < 100) and Assigned(Archive.OnProgress) then |
| 171 |
Archive.OnProgress(100, Abort); |
| 172 |
|
| 173 |
{ User wants to bail } |
| 174 |
if Abort then begin |
| 175 |
raise EAbUserAbort.Create; |
| 176 |
end; |
| 177 |
|
| 178 |
end; |
| 179 |
{ ========================================================================== } |
| 180 |
procedure DoZipFromStream(Sender : TAbZipArchive; Item : TAbZipItem; |
| 181 |
OutStream, InStream : TStream); |
| 182 |
var |
| 183 |
ZipArchive : TAbZipArchive; |
| 184 |
InStartPos : Int64; |
| 185 |
OutStartPos : Int64; |
| 186 |
TempOut : TAbVirtualMemoryStream; |
| 187 |
DestStrm : TStream; |
| 188 |
begin |
| 189 |
ZipArchive := TAbZipArchive(Sender); |
| 190 |
|
| 191 |
{ save starting point } |
| 192 |
OutStartPos := OutStream.Position; |
| 193 |
|
| 194 |
{ configure Item } |
| 195 |
Item.UncompressedSize := InStream.Size; |
| 196 |
Item.GeneralPurposeBitFlag := Item.GeneralPurposeBitFlag and AbLanguageEncodingFlag; |
| 197 |
|
| 198 |
if ZipArchive.Password <> '' then { encrypt the stream } |
| 199 |
DestStrm := TAbDfEncryptStream.Create(OutStream, |
| 200 |
LongInt(Item.LastModFileTime shl $10), |
| 201 |
ZipArchive.Password) |
| 202 |
else |
| 203 |
DestStrm := OutStream; |
| 204 |
try |
| 205 |
if InStream.Size > 0 then begin |
| 206 |
|
| 207 |
if SameText(ExtractFileExt(Sender.ArchiveName), '.zipx') then |
| 208 |
begin |
| 209 |
case ZipArchive.CompressionMethod of |
| 210 |
IntPtr(cmXz): DoCompressXz(ZipArchive, Item, DestStrm, InStream); |
| 211 |
IntPtr(cmZstd): DoCompressZstd(ZipArchive, Item, DestStrm, InStream); |
| 212 |
else raise Exception.Create(EmptyStr); |
| 213 |
end; |
| 214 |
end |
| 215 |
else |
| 216 |
{ determine how to store Item based on specified CompressionMethodToUse } |
| 217 |
case ZipArchive.CompressionMethodToUse of |
| 218 |
smDeflated : begin |
| 219 |
{ Item is to be deflated regarless } |
| 220 |
{ deflate item } |
| 221 |
DoDeflate(ZipArchive, Item, DestStrm, InStream); |
| 222 |
end; |
| 223 |
|
| 224 |
smStored : begin |
| 225 |
{ Item is to be stored regardless } |
| 226 |
{ store item } |
| 227 |
DoStore(ZipArchive, Item, DestStrm, InStream); |
| 228 |
end; |
| 229 |
|
| 230 |
smBestMethod : begin |
| 231 |
{ Item is to be archived using method producing best compression } |
| 232 |
TempOut := TAbVirtualMemoryStream.Create; |
| 233 |
try |
| 234 |
TempOut.SwapFileDirectory := Sender.TempDirectory; |
| 235 |
|
| 236 |
{ save starting points } |
| 237 |
InStartPos := InStream.Position; |
| 238 |
|
| 239 |
{ try deflating item } |
| 240 |
DoDeflate(ZipArchive, Item, TempOut, InStream); |
| 241 |
{ if deflated size > input size then got negative compression } |
| 242 |
{ so storing the item is more efficient } |
| 243 |
|
| 244 |
if TempOut.Size > InStream.Size then begin { store item instead } |
| 245 |
{ reset streams to original positions } |
| 246 |
InStream.Position := InStartPos; |
| 247 |
TempOut.Free; |
| 248 |
TempOut := TAbVirtualMemoryStream.Create; |
| 249 |
TempOut.SwapFileDirectory := Sender.TempDirectory; |
| 250 |
|
| 251 |
{ store item } |
| 252 |
DoStore(ZipArchive, Item, TempOut, InStream); |
| 253 |
end {if}; |
| 254 |
|
| 255 |
TempOut.Seek(0, soBeginning); |
| 256 |
DestStrm.CopyFrom(TempOut, TempOut.Size); |
| 257 |
finally |
| 258 |
TempOut.Free; |
| 259 |
end; |
| 260 |
end; |
| 261 |
end; { case } |
| 262 |
|
| 263 |
end |
| 264 |
else begin |
| 265 |
{ InStream is zero length} |
| 266 |
Item.CRC32 := 0; |
| 267 |
{ ignore any storage indicator and treat as stored } |
| 268 |
DoStore(ZipArchive, Item, DestStrm, InStream); |
| 269 |
end; |
| 270 |
finally |
| 271 |
if DestStrm <> OutStream then |
| 272 |
DestStrm.Free; |
| 273 |
end; |
| 274 |
|
| 275 |
{ update item } |
| 276 |
Item.CompressedSize := OutStream.Position - OutStartPos; |
| 277 |
Item.InternalFileAttributes := 0; { don't care } |
| 278 |
if (ZipArchive.Password <> '') then |
| 279 |
Item.GeneralPurposeBitFlag := Item.GeneralPurposeBitFlag |
| 280 |
or AbFileIsEncryptedFlag or AbHasDataDescriptorFlag; |
| 281 |
end; |
| 282 |
{ -------------------------------------------------------------------------- } |
| 283 |
procedure AbZipFromStream(Sender : TAbZipArchive; Item : TAbZipItem; |
| 284 |
OutStream, InStream : TStream); |
| 285 |
var |
| 286 |
FileTimeStamp : LongInt; |
| 287 |
begin |
| 288 |
// Set item properties for non-file streams |
| 289 |
Item.ExternalFileAttributes := 0; |
| 290 |
FileTimeStamp := DateTimeToFileDate(SysUtils.Now); |
| 291 |
Item.LastModFileTime := LongRec(FileTimeStamp).Lo; |
| 292 |
Item.LastModFileDate := LongRec(FileTimeStamp).Hi; |
| 293 |
|
| 294 |
DoZipFromStream(Sender, Item, OutStream, InStream); |
| 295 |
end; |
| 296 |
{ -------------------------------------------------------------------------- } |
| 297 |
procedure AbZip( Sender : TAbZipArchive; Item : TAbZipItem; |
| 298 |
OutStream : TStream ); |
| 299 |
var |
| 300 |
UncompressedStream : TStream; |
| 301 |
AttrEx : TAbAttrExRec; |
| 302 |
begin |
| 303 |
if not AbFileGetAttrEx(Item.DiskFileName, AttrEx) then |
| 304 |
Raise EAbFileNotFound.Create; |
| 305 |
if ((AttrEx.Attr and faDirectory) <> 0) then |
| 306 |
UncompressedStream := TMemoryStream.Create |
| 307 |
else |
| 308 |
UncompressedStream := TFileStreamEx.Create(Item.DiskFileName, fmOpenRead or fmShareDenyWrite); |
| 309 |
try {UncompressedStream} |
| 310 |
{$IFDEF UNIX} |
| 311 |
Item.ExternalFileAttributes := LongWord(AttrEx.Mode) shl 16 + LongWord(AttrEx.Attr); |
| 312 |
{$ELSE} |
| 313 |
Item.ExternalFileAttributes := AttrEx.Attr; |
| 314 |
{$ENDIF} |
| 315 |
Item.LastModTimeAsDateTime := AttrEx.Time; |
| 316 |
DoZipFromStream(Sender, Item, OutStream, UncompressedStream); |
| 317 |
finally {UncompressedStream} |
| 318 |
UncompressedStream.Free; |
| 319 |
end; {UncompressedStream} |
| 320 |
end; |
| 321 |
{ -------------------------------------------------------------------------- } |
| 322 |
|
| 323 |
end. |
| 324 |
|