sys.zlib.pas
unit sys.zlib; // cxg 2025-1-10 fit(fpc+delphi) interface uses {$IFDEF fpc} ZStream, {$else} zlib, {$endif} Variants, SysUtils, Classes; function CompressVariant(V: OleVariant): OleVariant; function CompressStream(val: TStream): TStream; function CompressBytes(val: TBytes): TBytes; function DeCompressVariant(V: OleVariant): OleVariant; function DeCompressStream(val: TStream): TStream; function DeCompressBytes(val: TBytes): TBytes; implementation procedure StreamToVariant(Stream: TStream; var V: OleVariant); var P: Pointer; begin V := VarArrayCreate([0, Stream.Size - 1], varByte); P := VarArrayLock(V); try Stream.Position := 0; Stream.Read(P^, Stream.Size); finally VarArrayUnlock(V); end; end; procedure VariantToStream(const V: OleVariant; Stream: TStream); var P: Pointer; begin Stream.Position := 0; Stream.Size := VarArrayHighBound(V, 1) - VarArrayLowBound(V, 1) + 1; P := VarArrayLock(V); try Stream.Write(P^, Stream.Size); finally VarArrayUnlock(V); end; Stream.Position := 0; end; function CompressBytes(val: TBytes): TBytes; var zipStream: TCompressionStream; Stream: TBytesStream; begin Stream := TBytesStream.Create(val); try zipStream := TCompressionStream.Create(cldefault, Stream); try SetLength(Result, zipStream.Size); zipStream.Position := 0; zipStream.Read(PByte(Result)^, zipStream.Size); finally zipStream.Free; end; finally Stream.Free; end; end; function CompressStream(val: TStream): TStream; begin Result := TCompressionStream.Create(cldefault, val); end; function CompressVariant(V: OleVariant): OleVariant; var zipStream: TCompressionStream; Stream: TStream; begin Stream := TMemoryStream.Create; try VariantToStream(V, Stream); zipStream := TCompressionStream.Create(cldefault, Stream); try StreamToVariant(zipStream, Result); finally zipStream.Free; end; finally Stream.Free; end; end; function DeCompressBytes(val: TBytes): TBytes; var Stream: TBytesStream; zipStream: TDecompressionStream; begin Stream := TBytesStream.Create(val); try zipStream := TDecompressionStream.Create(Stream); try SetLength(Result, zipStream.Size); zipStream.Position := 0; zipStream.Read(PByte(val)^, zipStream.Size); finally zipStream.Free; end; finally Stream.Free; end; end; function DeCompressStream(val: TStream): TStream; begin Result := TDecompressionStream.Create(val); end; function DeCompressVariant(V: OleVariant): OleVariant; var zipStream: TDecompressionStream; Stream: TStream; begin Stream := TMemoryStream.Create; try VariantToStream(V, Stream); zipStream := TDecompressionStream.Create(Stream); try StreamToVariant(zipStream, Result); finally zipStream.Free; end; finally Stream.Free; end; end; end.
标签:function,pas,end,val,Stream,zlib,zipStream,sys,TStream From: https://www.cnblogs.com/hnxxcxg/p/18664141