Can Delphi JCL 7z Compression be used to compress / decompress a stream in memory without file operations?

I used TJcl7zCompressArchive / TJcl7zDecompressArchive to perform the archiving operation before.

Now I would like to compress / decompress streams in memory directly without file operations. However, having seen examples from JCL demos on the Internet, I cannot find a way to do this using this library. I found other tools for this, but the compression ratio does not look as good as 7zip.

Can someone give some guidance or code examples showing how to achieve this. Many thanks!

+7
source share
1 answer

I use the JCL wrapper to compress the GZIP stream - not sure if it will work simply using TJcl7ziCompresspArchive. To compress the 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 unzip a 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; 
+10
source

All Articles