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/