我想问一个关于Delphi或Pascal的Zlib Inflate / Deflate代码的例子,因为整个网络我都找不到它......无处不在它的C / C ++ ......
我浏览了所有可用的资源,并且能够找到它:
procedure ZDecompress(const inBuffer: Pointer; inSize: Integer;
out outBuffer: Pointer; out outSize: Integer; outEstimate: Integer);
var
zstream: TZStreamRec;
delta: Integer;
begin
FillChar(zstream, SizeOf(TZStreamRec), 0);
delta := (inSize + 255) and not 255;
if outEstimate = 0 then outSize := delta
else outSize := outEstimate;
GetMem(outBuffer, outSize);
try
zstream.next_in := inBuffer;
zstream.avail_in := inSize;
zstream.next_out := outBuffer;
zstream.avail_out := outSize;
ZDecompressCheck(InflateInit(zstream));
try
while ZDecompressCheck(inflate(zstream, Z_NO_FLUSH)) <> Z_STREAM_END do
begin
Inc(outSize, delta);
ReallocMem(outBuffer, outSize);
zstream.next_out := PChar(Integer(outBuffer) + zstream.total_out);
zstream.avail_out := delta;
end;
finally
ZDecompressCheck(inflateEnd(zstream));
end;
ReallocMem(outBuffer, zstream.total_out);
outSize := zstream.total_out;
except
FreeMem(outBuffer);
raise;
end;
end;
我假设此程序适用于Inflating。任何建议的代码&amp;由你掌握的程序是为了同一目的???
答案 0 :(得分:1)
使用,system.ZLib;
TWadItem = Class
private
_UncompressedData:TMemoryStream;
_CompressedData:TMemoryStream;
public
public
Procedure Pack;
procedure unpack;
...
procedure TWadItem.Pack;
var
lz:TZCompressionStream;
begin
if _CompressedData<>nil then exit;
if _UnCompressedData=nil then exit;
if _Uncompresseddata.Size<1 then exit;
_CompressedData:=TMemoryStream.Create;
_UnCompressedData.Seek(0,0);
lz:=TZCompressionStream.Create(TCompressionLevel.clMax,_CompressedData);
lz.CopyFrom(_UnCompressedData,_UnCOmpressedData.Size);
LZ.Free;
_UnCompressedData.free;
_UnCompressedData:=NIL;
end;
procedure TWadItem.unpack;
var
lz:TZDeCompressionStream;
buff:TBytes;
begin
if _UnCompressedData<>nil then exit;
if _CompressedData=nil then exit;
_UnCompressedData:=TMemoryStream.Create;
lz:=TZDeCompressionStream.Create(_CompressedData);
_unCompressedData.CopyFrom(lz,0);
LZ.Free;
_CompressedData.Seek(0,0);
_unCompressedData.Seek(0,0);
// _unCompressedData.SaveToFile(ExtractFilePath(ParamStr(0))+'Decompress.bmp');
_unCompressedData.Seek(0,0);
//freeandNIl(_CompressedData);
_CompressedData.Free;
_CompressedData:=NIL;
end;
constructor TWadItem.create;
begin
self._UncompressedData:=nil;
self._CompressedData:=nil;
end;
function TWadItem.CreateBitmap: TBitmap;
begin
result:=TBitmap.Create;
self.ReadToBitmap(result);
end;
function TWadItem.CurrentlyCompressed: Boolean;
begin
result:=false;
if self._CompressedData<>nil then result:=true;
if self._UncompressedData<>nil then result:=False;
end;
destructor TWadItem.destroy;
begin
try
_UncompressedData.Free;
except
on exception do begin end;
end;
try
_CompressedData.Free;
except on exception do begin end;
end;
end;
procedure TWadItem.AssignFromBitmap(FromBMP: TBitmap);
begin
UnPack;
if _UnCompressedData=nil then
begin
_UnCompressedData:=TMemoryStream.Create;
_UnCompressedData.Clear;
end;
_UncompressedData.SetSize(0);
_UnCompressedData.Seek(0,0);
FromBMP.SaveToStream(_UnCompressedData);
end;