delphi - TCompressionStream 用数据初始化

标签 delphi zlib

SPDY 协议(protocol)指定使用预定义的数据 block 来初始化名称/值数据的压缩:

http://mbelshe.github.com/SPDY-Specification/draft-mbelshe-spdy-00.xml#rfc.section.2.6.9.1

(zlib 压缩的工作方式是,对于“似乎”重复出现次数较多的字符串,它会使用更少的位,因此,如果您使用通常的嫌疑人预加载压缩,那么您最终可能会得到更少的位更多时间是在压缩之后。但现在是我真正的问题:)

使用 ZLib 单元中的 Delphi 的 TCompressionStream 可以实现这一点吗?

最佳答案

您需要使用deflateSetDictionary 。它在 Delphi XE2 的 ZLib.pas 中可用,但压缩流类不会公开调用它的 TZStreamRec 字段。类助手可以访问关联类的私有(private)字段,因此您可以通过向 TCustomZStream 添加一个来解决该限制(将其添加到 TZCompressionStream 不起作用)。

type
  TCustomZStreamHelper = class helper for TCustomZStream
    function SetDictionary(dictionary: PByte; dictLength: Cardinal): Integer;
  end;

function TCustomZStreamHelper.SetDictionary(dictionary: PByte;
  dictLength: Cardinal): Integer;
begin
  if Self is TZCompressionStream then
    Result := deflateSetDictionary(Self.FZStream, dictionary, dictLength)
  else if Self is TZDecompressionStream then
    Result := inflateSetDictionary(Self.FZStream, dictionary, dictLength)
  else raise Exception.Create('Invalid class type');
end;

只需在创建压缩流后立即使用 SPDY 字符串调用 SetDictionary 即可。

关于delphi - TCompressionStream 用数据初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011442/

相关文章:

c++ - zlib gzopen() 返回压缩文件流。它会解压缩文件吗?

java - 使用 java Deflater/Inflater 和字典有什么问题

delphi - DropDownCount 不适用于 OwnerDrawFixed 样式

delphi - 无需 iOS 硬件即可创建 iOS 软件

Delphi:如何以相反的顺序删除子类?

Delphi XE8 Firemonkey TListView - 如何以编程方式设置背景颜色

winapi - 将 REG_BINARY 读取到字符串

c - iowin32.h 中的 OF 宏

c - 跳过不兼容的 zlib dll

对文件中的随机访问有良好支持的压缩格式?