Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-04

From deflate to gzip

5 messages · 4 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

I'm using gzip from Apache to compress my static files (like HTML, CSS, JS etc.), and it worked fine (http://imagebin.org/303024). But, to compress the CGI content, I'm using deflate:

var
  m: TMemoryStream;
begin
  if aeDeflate in BrookGetAcceptEncodingSet(ARequest.AcceptEncoding) then
  begin
    m := TMemoryStream.Create;
    try
      with TCompressionStream.Create(clMax, m, True) do
      try
        Write(AResponse.Content[1], Length(AResponse.Content));
      finally
        Free;
      end;
      m.Seek(0, 0);
      AResponse.SetCustomHeader(fieldContentEncoding, 'deflate');
      AResponse.ContentStream := m;
      AResponse.SendContent;
     finally
      m.Free;
    end;
  end;
end;

Now I need to use gzip instead of deflate, like:

      AResponse.SetCustomHeader(fieldContentEncoding, 'gzip');

So, how to use zstream to (or other) to compress my content stream in gzip mode? I need to create a temporary gzip file (using TGZFileStream class) and load it after from my stream (m), or I can do it directly in stream?

Thank you!

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Michael Van Canneyt
to silvioprog
silvioprog wrote 34 lines
Hello, I'm using gzip from Apache to compress my static files (like HTML, CSS, JS etc.), and it worked fine (http://imagebin.org/303024). But, to compress the CGI content, I'm using deflate: var   m: TMemoryStream; begin   if aeDeflate in BrookGetAcceptEncodingSet(ARequest.AcceptEncoding) then   begin     m := TMemoryStream.Create;     try       with TCompressionStream.Create(clMax, m, True) do       try         Write(AResponse.Content[1], Length(AResponse.Content));       finally         Free;       end;       m.Seek(0, 0);       AResponse.SetCustomHeader(fieldContentEncoding, 'deflate');       AResponse.ContentStream := m;       AResponse.SendContent;     finally       m.Free;     end;   end; end; Now I need to use gzip instead of deflate, like:       AResponse.SetCustomHeader(fieldContentEncoding, 'gzip'); So, how to use zstream to (or other) to compress my content stream in gzip mode? I need to create a temporary gzip file (using TGZFileStream class) and load it after from my stream (m), or I can do it directly in stream?

No, currently you need a file, unless you are willing to duplicate the tgzstream class using an in memory-approach. (that would be a welcome addition, BTW)

Michael.

Reply by email

Burdjia.com

Hello,

You can use libtar and zstream units, from the LCL. Class TTarArchive creates a .tar stream and TCompressionStream compress it using gzip algorithm. Combine them to create .tar.gz archives.

Both classes can work "on the fly" without saving information on disk.

Guillermo "Ñuño" Martínez

Show quoted text 40 lines
Hello, I'm using gzip from Apache to compress my static files (like HTML, CSS, JS etc.), and it worked fine (http://imagebin.org/303024). But, to compress the CGI content, I'm using deflate: var m: TMemoryStream; begin if aeDeflate in BrookGetAcceptEncodingSet(ARequest.AcceptEncoding) then begin m := TMemoryStream.Create; try with TCompressionStream.Create(clMax, m, True) do try Write(AResponse.Content[1], Length(AResponse.Content)); finally Free; end; m.Seek(0, 0); AResponse.SetCustomHeader(fieldContentEncoding, 'deflate'); AResponse.ContentStream := m; AResponse.SendContent; finally m.Free; end; end; end; Now I need to use gzip instead of deflate, like: AResponse.SetCustomHeader(fieldContentEncoding, 'gzip'); So, how to use zstream to (or other) to compress my content stream in gzip mode? I need to create a temporary gzip file (using TGZFileStream class) and load it after from my stream (m), or I can do it directly in stream? Thank you!

Reply by email

Daniel Gaspary
to Michael Van Canneyt: No, currently you need a file, unless you are willing to duplicate the tgzstream class…
Michael Van Canneyt wrote
No, currently you need a file, unless you are willing to duplicate the tgzstream class using an in memory-approach. (that would be a welcome addition, BTW)

TZipper has similar problem, you cannot write direct to the destination file.

Months a go I worked to modify the class to have a Stream writing directly to a zip file.

A complicated change, and I don't know if worth the effort because due to Zip format[1], I realize that only streams with support to "seek back" would be possible.

[1] The File Header is written before the compressed stream, and it must contain the compressed file size.

Reply by email