Delphi JCL 7zCompression可以用于压缩/解压缩内存流而不需要文件操作吗?

发布于 2024-11-28 16:34:37 字数 226 浏览 1 评论 0原文

我之前使用过TJcl7zCompressArchive / TJcl7zDecompressArchive 进行Archive 操作。

现在我想直接压缩/解压缩内存中的流,而不进行文件操作。但是,当看到在网络中搜索的 JCL 演示中的示例时,我找不到使用该库来执行此操作的方法。我确实找到了其他工具来做到这一点,但压缩率似乎不如 7zip。

任何人都可以给出一些指示或示例代码来展示如何实现这一目标。多谢!

I had used TJcl7zCompressArchive / TJcl7zDecompressArchive to do Archive operation before.

Now I would like to compress / decompress in-memory streams directly without file operation. However, when seeing the examples from JCL demos searching in the web, I cannot find a way to do so using that lib. I did find other tools to do that but the compression ratio seems not as good as 7zip.

Can anyone give some directions or sample code showing how to achieve this. Thanks a lot!

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

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

发布评论

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

评论(1

酒儿 2024-12-05 16:34:37

我使用 JCL 包装器来压缩 GZIP 流 - 不确定它是否可以简单地使用 TJcl7ziCompresspArchive。要压缩流,我使用以下命令:

procedure _CompressStreamGZIP( const ASourceStream, ADestinationStream: TStream );
var
  LArchive : TJclCompressArchive;
begin
  ADestinationStream.Position := 0;
  ASourceStream.Position := 0;
  LArchive := TJclGZipCompressArchive.Create( ADestinationStream, 0, False );

  try
    LArchive.AddFile( '..\Stream.0', ASourceStream, false );
    LArchive.Compress();
  finally
    if ( Assigned( LArchive ) ) then FreeAndNil( LArchive );
  end;
end;

要解压缩流:

procedure _DecompressStreamGZIP( const ASourceStream, ADestinationStream : TStream );
var
  LArchive : TJclDecompressArchive;
begin
  ADestinationStream.Position := 0;
  ASourceStream.Position := 0;
  LArchive := TJclGZipDecompressArchive.Create( ASourceStream, 0, false );

  try
    LArchive.ListFiles();
    LArchive.Items[0].Stream := ADestinationStream;
    LArchive.Items[0].OwnsStream := false;
    LArchive.Items[0].Selected := True;
    LArchive.ExtractSelected();
  finally
    if ( Assigned( LArchive ) ) then FreeAndNil( LArchive );
  end;
end;

I use the JCL wrapper to compress a GZIP stream - not sure if it would work simply using a TJcl7ziCompresspArchive. To compress a stream I use the following:

procedure _CompressStreamGZIP( const ASourceStream, ADestinationStream: TStream );
var
  LArchive : TJclCompressArchive;
begin
  ADestinationStream.Position := 0;
  ASourceStream.Position := 0;
  LArchive := TJclGZipCompressArchive.Create( ADestinationStream, 0, False );

  try
    LArchive.AddFile( '..\Stream.0', ASourceStream, false );
    LArchive.Compress();
  finally
    if ( Assigned( LArchive ) ) then FreeAndNil( LArchive );
  end;
end;

To decompress the stream:

procedure _DecompressStreamGZIP( const ASourceStream, ADestinationStream : TStream );
var
  LArchive : TJclDecompressArchive;
begin
  ADestinationStream.Position := 0;
  ASourceStream.Position := 0;
  LArchive := TJclGZipDecompressArchive.Create( ASourceStream, 0, false );

  try
    LArchive.ListFiles();
    LArchive.Items[0].Stream := ADestinationStream;
    LArchive.Items[0].OwnsStream := false;
    LArchive.Items[0].Selected := True;
    LArchive.ExtractSelected();
  finally
    if ( Assigned( LArchive ) ) then FreeAndNil( LArchive );
  end;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文