TCompressionStream使用数据初始化

Sti*_*ers 6 delphi zlib

SPDY协议指定使用预定义的数据块初始化名称/值数据的压缩:

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

(zlib压缩的工作方式是,对于"看起来"再次出现更多的字符串,它将使用更少的位,因此如果您使用通常的嫌疑人预加载压缩,那么在压缩之后您可能会得到更少的位当时.但现在我真正的问题:)

这可能来自ZLib单元的Delphi的TCompressionStream吗?

Zoë*_*son 6

你需要使用deflateSetDictionary.它在Delphi XE2的ZLib.pas中可用,但压缩流类不会公开该TZStreamRec字段来调用它.类助手可以访问关联类的私有字段,因此您可以通过添加一个来解决该限制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;
Run Code Online (Sandbox Code Playgroud)

在创建压缩流后立即使用SPDY字符串调用SetDictionary.