在将 TComponent 写入 TStream 时压缩它们

发布于 2024-07-21 13:08:40 字数 460 浏览 1 评论 0原文

我们使用的一些应用程序依赖于 TComponent 后代来轻松保存/加载其内部对象的状态,在 Write/ReadComponentResFile 的帮助下

function TSomeClass.SaveState: boolean;
begin
  ...
  try
    ...
    WriteComponentResFile(self.f_path, TComponent(self));
    result := true;
  except   
    result := false;
  end;
  ....
end;

现在我们想压缩这些数据,但由于某种原因我一直无法找到一个使用 JCL bzip2 流类编写类似函数的方法,关于不支持查找操作的一些事情

由于我不是 TStream 专家,我想知道实现这种压缩的最简单方法; 使用 TComponent 读/写?

谢谢

Some application we're using depends on TComponent descendants to easily save/load the state of its internal objects, with the help of Write/ReadComponentResFile

function TSomeClass.SaveState: boolean;
begin
  ...
  try
    ...
    WriteComponentResFile(self.f_path, TComponent(self));
    result := true;
  except   
    result := false;
  end;
  ....
end;

Now we would like to compress this data, but for some reason I've been unable to find a way to write similar function using the JCL bzip2 stream class, something about seek operation not being supported

As I am no TStream expert, I would like to know the easiest way for me to implement such a compression; working with TComponent read/write ?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生一梦 2024-07-28 13:08:40

TComponent 显然希望能够在它读取或写入的流中进行查找,而您通常无法使用压缩流来做到这一点 - 至少不能向后。 一旦您读取了一个字节,您就无法返回并再次读取它,因为您获得的值可能取决于首先导致该字节的整个流内容。 向后查找以重新读取一个字节可能意味着重新读取整个流。 对于写入来说,要返回并“修复”流的某些区域,更改一个字节将意味着需要重新压缩其后的所有内容。 所以你可以明白为什么压缩流不喜欢向后查找。

将数据写入TMemoryStream,然后将该流的内容复制到压缩流。 要读取,请将解压缩流复制到 TMemoryStream 中,然后从那里加载组件。 (从内存流中加载组件之前,不要忘记将 Position 属性设置回零。)

TComponent apparently wants to be able to seek in the stream it reads from or writes to, and you generally can't do that with compressed streams — at least not backward. Once you've read a byte, you can't go back and read it again because precisely what value you get may depend on the entire stream contents that led to that byte in the first place. Seeking backward to re-read one byte could mean re-reading the entire stream. And for writing, to go back and "fix up" some region of the stream, changing one byte would mean needing to recompress everything that came after it. So you can see why compressed streams don't like to seek backward.

Write your data to a TMemoryStream, and then copy that stream's contents to a compressed stream afterward. To read, copy the decompression stream into a TMemoryStream and then load your components from there. (Don't forget to set the Position property back to zero before loading the component out of the memory stream.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文