本人初学Delphi,很多东西不懂,请各位给出代码的时候完全一点我要实现在.net和delphi之间传数据,.net调用Zlib.dll压缩解压缩byte数组已经实现了,现在要在delphi中实现。Unit UCompressinterface
  Uses Classes,SysUtils,ZLib,ZLibConst;
type
    TBytes = array of Byte;
  function CompressBytes(ASrcBytes: TBytes): TBytes;
  function DecompressBytes(ASrcBytes: TBytes;): TBytes;implementationend.谁能给出这2个函数的实现代码,百分相送,最好实现在压缩后的数组前4或8个元素保存原始数据大小

解决方案 »

  1.   

    http://blog.csdn.net/yuehaiyang/archive/2007/08/28/1762498.aspx
      

  2.   

    我这里要求输入输出参数是array of byte,我也找过byte数组和TStream的互相转换代码,可惜用起来出错
      

  3.   

    类似这样就可以了,顺手写了一下 不知道有没有错误function CompressBytes(ASrcBytes: TBytes): TBytes;
    var
      ZlibCompressor: TCompressionStream;
      ZipCount: LongWord;
      DestStream:TMemoryStream;
    begin
      DestStream:=TMemoryStream.Create;
      ZipCount:=length(ASrcBytes);
      DestStream.Write(ZipCount, SizeOf(LongWord));
      ZlibCompressor:=TCompressionStream.Create(clDefault, DestStream);
      try
        ZlibCompressor.Write(ASrcBytes[0], length(ASrcBytes));
      finally
        FreeAndNil(ZlibCompressor);    ;
      end;
      DestStream.Seek(0, 0);
      SetLength(Result,DestStream.Size);
      DestStream.Read(Result[0],DestStream.Size);
      DestStream.Free;
    end;
    function DecompressBytes(ASrcBytes: TBytes): TBytes;
    var
      UnZipStream : TDecompressionStream;
      UnZipCount:LongWord;
      SourseStream:TMemoryStream;
    begin
      SourseStream:=TMemoryStream.Create;  SourseStream.Write(ASrcBytes[0],length(ASrcBytes));
      SourseStream.Seek(0,0);
      SourseStream.Read(UnZipCount,SizeOf(LongWord));  UnZipStream := TDecompressionStream.Create(SourseStream);
      try
        SetLength(Result,UnZipCount);
        UnZipStream.Read(Result[0],UnZipCount);
      finally
        UnZipStream.Free;
      end;
    end;