• 流的压缩与解压缩


    uses Zlib; 
    //压缩 
    procedure DoCompress(var CompressedStream: TMemoryStream); 
    var 
      SourceStream: TCompressionStream; 
      DestStream: TMemoryStream; 
      Count: int64; 
    Begin 
      CompressedStream.Position := 0; 
      Count := CompressedStream.Size; 
      if Count=0 then     //错误 
        Exit; 
      DestStream := TMemoryStream.Create; 
      SourceStream:=TCompressionStream.Create(clMax, DestStream); 
      Try 
        CompressedStream.SaveToStream(SourceStream); 
        SourceStream.Free; 
        CompressedStream.Clear; 
        CompressedStream.WriteBuffer(Count, SizeOf(Count)); 
        CompressedStream.CopyFrom(DestStream, 0); 
      finally 
        DestStream.Free; 
      end; 
      CompressedStream.Position := 0; 
    end; 

    //解压缩 
    procedure DoUncompress(var UnCompressedStream: TMemoryStream); 
    var 
      Count: int64; 
      SourceStream: TDecompressionStream; 
      Buffer: pchar; 
    begin 
      UnCompressedStream.Position:=0; 
      UnCompressedStream.ReadBuffer(Count, sizeof(Count)); 
      if count<0 then 
      begin 
        UnCompressedStream.Position:=0; 
        exit; 
      end; 
      SourceStream := TDecompressionStream.Create(UnCompressedStream); 
      Getmem(Buffer, Count); 
      SourceStream.Read(Buffer^, Count); 
      freeandnil(SourceStream); 
      UnCompressedStream.Clear; 
      UnCompressedStream.WriteBuffer(Buffer^,Count); 
      UnCompressedStream.Position:=0; 
      freemem(Buffer); 
    end; 

  • 相关阅读:
    Apache Common-IO 使用
    Apache Compress-使用
    使用JavaConfig方式-Spring 基础学习
    设计模式-策略模式
    logback mybatis 打印sql语句
    oracle 迭代查询
    使用 Dom4j 将 XML 转换为 MAP
    Java连接Access数据库的那些坑
    如何从Maven中央存储库下载?
    sublime构建各个编译环境
  • 原文地址:https://www.cnblogs.com/yych/p/1901408.html
Copyright © 2020-2023  润新知