ZIPライブラリ クラスを修正しています。内部的には、ほぼすべての ZIP 実装でDEFLATE
圧縮 (RFC1951)が使用されています。
DEFLATE
問題は、Delphi では圧縮ライブラリにアクセスできないことです。しかし、私たちがたくさん持っているのは、ZLIB
圧縮コード (RFC1950)です。それは Delphi にも同梱されており、他にも 6 つほどの実装が浮かんでいます。
内部的には、ZLIB は圧縮にも DEFLATE を使用します。だから私は誰もがやったことをしたい - DEFLATE圧縮機能のために Delphi zlibライブラリを使用してください。
問題は、ZLIB が 2 バイトのプレフィックスと 4 バイトのトレーラーをDEFLATEDデータに追加することです。
[CMF] 1 byte
[FLG] 1 byte
[...deflate compressed data...]
[Adler-32 checksum] 4 bytes
だから私が必要とするのは、データを圧縮するために標準のTCompressionStream
(またはTZCompressionStream
、またはTZCompressionStreamEx
使用しているソースコードに応じて)ストリームを使用する方法です:
procedure CompressDataToTargetStream(sourceStream: TStream; targetStream: TStream);
var
compressor: TCompressionStream;
begin
compressor := TCompressionStream.Create(clDefault, targetStream); //clDefault = CompressionLevel
try
compressor.CopyFrom(sourceStream, sourceStream.Length)
finally
compressor.Free;
end;
end;
これは機能しますが、先頭の 2 バイトと末尾の 4 バイトが書き出されます。私はそれらを取り除く必要があります。
だから私は書いたTByteEaterStream
:
TByteEaterStream = class(TStream)
public
constructor Create(TargetStream: TStream;
LeadingBytesToEat, TrailingBytesToEat: Integer);
end;
例えば
procedure CompressDataToTargetStream(sourceStream: TStream; targetStream: TStream);
var
byteEaterStream: TByteEaterStream;
compressor: TCompressionStream;
begin
byteEaterStream := TByteEaterStream.Create(targetStream, 2, 4); //2 leading bytes, 4 trailing bytes
try
compressor := TCompressionStream.Create(clDefault, byteEaterStream); //clDefault = CompressionLevel
try
compressor.CopyFrom(sourceStream, sourceStream.Length)
finally
compressor.Free;
end;
finally
byteEaterStream.Free;
end;
end;
このストリームは write メソッドをオーバーライドします。2
最初のバイトを食べるのは簡単です。4
トリックは、末尾のバイトを食べることでした。
eater ストリームには 4 バイトの配列があり、i は常にバッファ内のすべての書き込みの最後の 4 バイトを保持します。EaterStreamが破棄されると、末尾の 4 バイトが破棄されます。
問題は、このバッファを介して数百万回の書き込みをシャッフルすると、パフォーマンスが低下することです。アップストリームでの一般的な使用法は次のとおりです。
for each of a million data rows
stream.Write(s, Length(s)); //30-90 character string
上流のユーザーが「終わりが近づいている」ことを示す必要は絶対にありません。もっと速くしたいだけです。
質問
流れるバイトのストリームを見て、最後の 4 バイトをホールドバックする最良の方法は何ですか? 書き込みが最後になる瞬間がわからない場合。
私が修正しているコードは、圧縮されたバージョン全体を に書き込んTStringStream
でから、内部の DEFLATE データを取得するために 900MB - 6 バイトのみを取得しました。
cs := TStringStream.Create('');
....write compressed data to cs
S := Copy(CS.DataString, 3, Length(CS.DataString) - 6);
それを除いて、ユーザーはメモリ不足になります。最初は a に書き込むように変更しましたTFileStream
が、同じトリックを実行できました。
しかし、私はより良い解決策が欲しいです。ストリーム ソリューション。中間ストレージなしで、圧縮された最終ストリームにデータを入れたい。
私の実装
それが何の役にも立たないというわけではありません。トリミングを行うために適応ストリームを使用するシステムを必ずしも求めているわけではないからです
TByteEaterStream = class(TStream)
private
FTargetStream: TStream;
FTargetStreamOwnership: TStreamOwnership;
FLeadingBytesToEat: Integer;
FTrailingBytesToEat: Integer;
FLeadingBytesRemaining: Integer;
FBuffer: array of Byte;
FValidBufferLength: Integer;
function GetBufferValidLength: Integer;
public
constructor Create(TargetStream: TStream; LeadingBytesToEat, TrailingBytesToEat: Integer; StreamOwnership: TStreamOwnership=soReference);
destructor Destroy; override;
class procedure SelfTest;
procedure Flush;
function Read(var Buffer; Count: Longint): Longint; override;
function Write(const Buffer; Count: Longint): Longint; override;
function Seek(Offset: Longint; Origin: Word): Longint; override;
end;
{ TByteEaterStream }
constructor TByteEaterStream.Create(TargetStream: TStream; LeadingBytesToEat, TrailingBytesToEat: Integer; StreamOwnership: TStreamOwnership=soReference);
begin
inherited Create;
//User requested state
FTargetStream := TargetStream;
FTargetStreamOwnership := StreamOwnership;
FLeadingBytesToEat := LeadingBytesToEat;
FTrailingBytesToEat := TrailingBytesToEat;
//internal housekeeping
FLeadingBytesRemaining := FLeadingBytesToEat;
SetLength(FBuffer, FTrailingBytesToEat);
FValidBufferLength := 0;
end;
destructor TByteEaterStream.Destroy;
begin
if FTargetStreamOwnership = soOwned then
FTargetStream.Free;
FTargetStream := nil;
inherited;
end;
procedure TByteEaterStream.Flush;
begin
if FValidBufferLength > 0 then
begin
FTargetStream.Write(FBuffer[0], FValidBufferLength);
FValidBufferLength := 0;
end;
end;
function TByteEaterStream.Write(const Buffer; Count: Integer): Longint;
var
newStart: Pointer;
totalCount: Integer;
addIndex: Integer;
bufferValidLength: Integer;
bytesToWrite: Integer;
begin
Result := Count;
if Count = 0 then
Exit;
if FLeadingBytesRemaining > 0 then
begin
newStart := Addr(Buffer);
Inc(Cardinal(newStart));
Dec(Count);
Dec(FLeadingBytesRemaining);
Result := Self.Write(newStart^, Count)+1; //tell the upstream guy that we wrote it
Exit;
end;
if FTrailingBytesToEat > 0 then
begin
if (Count < FTrailingBytesToEat) then
begin
//There's less bytes incoming than an entire buffer
//But the buffer might overfloweth
totalCount := FValidBufferLength+Count;
//If it could all fit in the buffer, then let it
if (totalCount <= FTrailingBytesToEat) then
begin
Move(Buffer, FBuffer[FValidBufferLength], Count);
FValidBufferLength := totalCount;
end
else
begin
//We're going to overflow the buffer.
//Purge from the buffer the amount that would get pushed
FTargetStream.Write(FBuffer[0], totalCount-FTrailingBytesToEat);
//Shuffle the buffer down (overlapped move)
bufferValidLength := bufferValidLength - (totalCount-FTrailingBytesToEat);
Move(FBuffer[totalCount-FTrailingBytesToEat], FBuffer[0], bufferValidLength);
addIndex := bufferValidLength ; //where we will add the data to
Move(Buffer, FBuffer[addIndex], Count);
end;
end
else if (Count = FTrailingBytesToEat) then
begin
//The incoming bytes exactly fill the buffer. Flush what we have and eat the incoming amounts
Flush;
Move(Buffer, FBuffer[0], FTrailingBytesToEat);
FValidBufferLength := FTrailingBytesToEat;
Result := FTrailingBytesToEat; //we "wrote" n bytes
end
else
begin
//Count is greater than trailing buffer eat size
Flush;
//Write the data that definitely not to be eaten
bytesToWrite := Count-FTrailingBytesToEat;
FTargetStream.Write(Buffer, bytesToWrite);
//Buffer the remainder
newStart := Addr(Buffer);
Inc(Cardinal(newStart), bytesToWrite);
Move(newStart^, FBuffer[0], FTrailingBytesToEat);
FValidBufferLength := 4;
end;
end;
end;
function TByteEaterStream.Seek(Offset: Integer; Origin: Word): Longint;
begin
//what does it mean if they want to seek around when i'm supposed to be eating data?
//i don't know; so results are, by definition, undefined. Don't use at your own risk
Result := FTargetStream.Seek(Offset, Origin);
end;
function TByteEaterStream.Read(var Buffer; Count: Integer): Longint;
begin
//what does it mean if they want to read back bytes when i'm supposed to be eating data?
//i don't know; so results are, by definition, undefined. Don't use at your own risk
Result := FTargetStream.Read({var}Buffer, Count);
end;
class procedure TByteEaterStream.SelfTest;
procedure CheckEquals(Expected, Actual: string; Message: string);
begin
if Actual <> Expected then
raise Exception.CreateFmt('TByteEaterStream self-test failed. Expected "%s", but was "%s". Message: %s', [Expected, Actual, Message]);
end;
procedure Test(const InputString: string; ExpectedString: string);
var
s: TStringStream;
eater: TByteEaterStream;
begin
s := TStringStream.Create('');
try
eater := TByteEaterStream.Create(s, 2, 4, soReference);
try
eater.Write(InputString[1], Length(InputString));
finally
eater.Free;
end;
CheckEquals(ExpectedString, s.DataString, InputString);
finally
s.Free;
end;
end;
begin
Test('1', '');
Test('11', '');
Test('113', '');
Test('1133', '');
Test('11333', '');
Test('113333', '');
Test('11H3333', 'H');
Test('11He3333', 'He');
Test('11Hel3333', 'Hel');
Test('11Hell3333', 'Hell');
Test('11Hello3333', 'Hello');
Test('11Hello,3333', 'Hello,');
Test('11Hello, 3333', 'Hello, ');
Test('11Hello, W3333', 'Hello, W');
Test('11Hello, Wo3333', 'Hello, Wo');
Test('11Hello, Wor3333', 'Hello, Wor');
Test('11Hello, Worl3333', 'Hello, Worl');
Test('11Hello, World3333', 'Hello, World');
Test('11Hello, World!3333', 'Hello, World!');
end;