| 1 |
unit AbZlibPrc; |
| 2 |
|
| 3 |
{$mode objfpc}{$H+} |
| 4 |
|
| 5 |
interface |
| 6 |
|
| 7 |
uses |
| 8 |
Classes, SysUtils, ZStream, AbDfBase; |
| 9 |
|
| 10 |
type |
| 11 |
|
| 12 |
{ TDeflateStream } |
| 13 |
|
| 14 |
TDeflateStream = class(TCompressionStream) |
| 15 |
private |
| 16 |
FSize: Int64; |
| 17 |
FHash: UInt32; |
| 18 |
FOnProgressStep: TAbProgressStep; |
| 19 |
public |
| 20 |
constructor Create(ALevel: Integer; ADest: TStream); |
| 21 |
function Write(const Buffer; Count: Longint): Longint; override; |
| 22 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
| 23 |
property Hash: UInt32 read FHash; |
| 24 |
end; |
| 25 |
|
| 26 |
{ TInflateStream } |
| 27 |
|
| 28 |
TInflateStream = class(TDecompressionStream) |
| 29 |
private |
| 30 |
FHash: UInt32; |
| 31 |
public |
| 32 |
function CopyInto(ATarget: TStream; ACount: Int64): Int64; |
| 33 |
function Read(var Buffer; Count: LongInt): LongInt; override; |
| 34 |
end; |
| 35 |
|
| 36 |
function Deflate(aSource : TStream; aDest : TStream; |
| 37 |
aHelper : TAbDeflateHelper) : longint; |
| 38 |
|
| 39 |
function Inflate(aSource : TStream; aDest : TStream; |
| 40 |
aHelper : TAbDeflateHelper) : longint; |
| 41 |
|
| 42 |
implementation |
| 43 |
|
| 44 |
uses |
| 45 |
Math, ZDeflate, ZBase, DCcrc32; |
| 46 |
|
| 47 |
function Deflate(aSource : TStream; aDest : TStream; |
| 48 |
aHelper : TAbDeflateHelper): longint; |
| 49 |
var |
| 50 |
ALevel: Integer; |
| 51 |
ADeflateStream: TDeflateStream; |
| 52 |
begin |
| 53 |
case aHelper.PKZipOption of |
| 54 |
'n': ALevel:= 6; |
| 55 |
'x': ALevel:= 9; |
| 56 |
'f': ALevel:= 3; |
| 57 |
's': ALevel:= 1; |
| 58 |
else ALevel:= Z_DEFAULT_COMPRESSION; |
| 59 |
end; |
| 60 |
|
| 61 |
{ if the helper's stream size <= 0, calculate |
| 62 |
the stream size from the stream itself } |
| 63 |
if (aHelper.StreamSize <= 0) then |
| 64 |
aHelper.StreamSize := aSource.Size; |
| 65 |
|
| 66 |
ADeflateStream:= TDeflateStream.Create(ALevel, aDest); |
| 67 |
try |
| 68 |
ADeflateStream.FSize:= aHelper.StreamSize; |
| 69 |
{ attach progress notification method } |
| 70 |
ADeflateStream.FOnProgressStep:= aHelper.OnProgressStep; |
| 71 |
ADeflateStream.CopyFrom(aSource, aHelper.StreamSize); |
| 72 |
{ save the uncompressed and compressed sizes } |
| 73 |
aHelper.NormalSize:= ADeflateStream.raw_written; |
| 74 |
aHelper.CompressedSize:= ADeflateStream.compressed_written; |
| 75 |
{ provide encryption check value } |
| 76 |
Result := LongInt(ADeflateStream.FHash); |
| 77 |
finally |
| 78 |
ADeflateStream.Free; |
| 79 |
end; |
| 80 |
end; |
| 81 |
|
| 82 |
function Inflate(aSource: TStream; aDest: TStream; |
| 83 |
aHelper: TAbDeflateHelper): longint; |
| 84 |
var |
| 85 |
ACount: Int64; |
| 86 |
AInflateStream: TInflateStream; |
| 87 |
begin |
| 88 |
AInflateStream:= TInflateStream.Create(aSource, True); |
| 89 |
try |
| 90 |
if aHelper.PartialSize > 0 then |
| 91 |
begin |
| 92 |
ACount:= aHelper.PartialSize; |
| 93 |
aHelper.NormalSize:= AInflateStream.CopyInto(aDest, ACount); |
| 94 |
end |
| 95 |
else begin |
| 96 |
ACount:= aHelper.NormalSize; |
| 97 |
aHelper.NormalSize:= aDest.CopyFrom(AInflateStream, ACount); |
| 98 |
end; |
| 99 |
aHelper.CompressedSize:= AInflateStream.compressed_read; |
| 100 |
Result:= LongInt(AInflateStream.FHash); |
| 101 |
finally |
| 102 |
AInflateStream.Free; |
| 103 |
end; |
| 104 |
end; |
| 105 |
|
| 106 |
{ TInflateStream } |
| 107 |
|
| 108 |
function TInflateStream.CopyInto(ATarget: TStream; ACount: Int64): Int64; |
| 109 |
var |
| 110 |
ARead, ASize: Integer; |
| 111 |
ABuffer: array of Byte; |
| 112 |
begin |
| 113 |
Result:= 0; |
| 114 |
ASize:= Min(ACount, $8000); |
| 115 |
SetLength(ABuffer, ASize); |
| 116 |
repeat |
| 117 |
if ACount < ASize then |
| 118 |
begin |
| 119 |
ASize:= ACount; |
| 120 |
end; |
| 121 |
ARead:= Read(ABuffer[0], ASize); |
| 122 |
if ARead > 0 then |
| 123 |
begin |
| 124 |
Dec(ACount, ARead); |
| 125 |
Inc(Result, ARead); |
| 126 |
ATarget.WriteBuffer(ABuffer[0], ARead); |
| 127 |
end; |
| 128 |
until (ARead < ASize) or (ACount = 0); |
| 129 |
end; |
| 130 |
|
| 131 |
function TInflateStream.Read(var Buffer; Count: LongInt): LongInt; |
| 132 |
begin |
| 133 |
Result:= inherited Read(Buffer, Count); |
| 134 |
FHash:= crc32_16bytes(@Buffer, Result, FHash); |
| 135 |
if (Result < Count) and (Fstream.avail_in > 0) then |
| 136 |
begin |
| 137 |
FSource.Seek(-Fstream.avail_in, soCurrent); |
| 138 |
Fstream.avail_in:= 0; |
| 139 |
end; |
| 140 |
end; |
| 141 |
|
| 142 |
{ TDeflateStream } |
| 143 |
|
| 144 |
constructor TDeflateStream.Create(ALevel: Integer; |
| 145 |
ADest: TStream); |
| 146 |
const |
| 147 |
BUF_SIZE = 16384; |
| 148 |
var |
| 149 |
AError: Integer; |
| 150 |
begin |
| 151 |
TOwnerStream(Self).Create(ADest); |
| 152 |
|
| 153 |
Fbuffer:= GetMem(BUF_SIZE); |
| 154 |
Fstream.next_out:= Fbuffer; |
| 155 |
Fstream.avail_out:= BUF_SIZE; |
| 156 |
|
| 157 |
AError:= deflateInit2(Fstream, ALevel, Z_DEFLATED, -MAX_WBITS, DEF_MEM_LEVEL, 0); |
| 158 |
|
| 159 |
if AError <> Z_OK then |
| 160 |
raise Ecompressionerror.Create(zerror(AError)); |
| 161 |
end; |
| 162 |
|
| 163 |
function TDeflateStream.Write(const Buffer; Count: Longint): Longint; |
| 164 |
begin |
| 165 |
FHash:= crc32_16bytes(@Buffer, Count, FHash); |
| 166 |
Result:= inherited Write(Buffer, Count); |
| 167 |
if Assigned(FOnProgressStep) then |
| 168 |
begin |
| 169 |
FOnProgressStep(raw_written * 100 div FSize); |
| 170 |
end; |
| 171 |
end; |
| 172 |
|
| 173 |
function TDeflateStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 174 |
begin |
| 175 |
if (Offset = 0) and (Origin = soCurrent) then |
| 176 |
Result:= raw_written |
| 177 |
else begin |
| 178 |
Result:= inherited Seek(Offset, Origin); |
| 179 |
end; |
| 180 |
end; |
| 181 |
|
| 182 |
end. |
| 183 |
|