Sign in Join

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

Mailing lists / fpc-pascal / 2015-10

How to duplicate PHP's gzcompress() function in FPC?

7 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

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

Graeme Geldenhuys

Hi,

I need to compress a file the same way as PHP's gzcompress() function:

http://php.net/manual/en/function.gzcompress.php

From that URL it uses ZLIB, and that is apparently different to GZIP and

DEFLATE, because PHP also has a gzencode() and gzdeflate() functions respectively.

It seems the key point, from want I understand in the URL text, is that ZLIB compresses the data and has no Header Information, where GZIP does similar, but has Header Information.

I'm still unclear as to the the differences between ZLIB and DEFLATE - I always assumed them to be the same thing.

Anyway, so what is the equivalent to gzcompress in FPC? I see in the FCL there is a zlib.pp unit, but in the code comments in takes about wrapping the compressed data with gzip. So I'm not sure if this is then equivalent to PHP's gzcompress().

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

Dmitry Boyarintsev
to Graeme Geldenhuys

Not sure about, if there's an equivalent function, but the difference with Gzip and Deflate is the following.
Gzip contains an information about the original file + deflate (compressed) stream of data.
Default is just stream of data.
For example ZIP files are using Deflate, but not using Gzip headers (providing their owns).

The difference is mentioned in deflateInit2() functions, where a user can specify windowBits. If the value is negative, to Gzip header would be generated.

I presume the gzcompress is passing negative value to the function, while gzencode passes a positive value to the function.

thanks,
Dmitry

On Wed, Oct 7, 2015 at 11:26 AM, Graeme Geldenhuys <

Show quoted text 36 lines
wrote: Hi, I need to compress a file the same way as PHP's gzcompress() function: http://php.net/manual/en/function.gzcompress.php From that URL it uses ZLIB, and that is apparently different to GZIP and DEFLATE, because PHP also has a gzencode() and gzdeflate() functions respectively. It seems the key point, from want I understand in the URL text, is that ZLIB compresses the data and has no Header Information, where GZIP does similar, but has Header Information. I'm still unclear as to the the differences between ZLIB and DEFLATE - I always assumed them to be the same thing. Anyway, so what is the equivalent to gzcompress in FPC? I see in the FCL there is a zlib.pp unit, but in the code comments in takes about wrapping the compressed data with gzip. So I'm not sure if this is then equivalent to PHP's gzcompress(). Regards, - Graeme - -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

Graeme Geldenhuys
to Dmitry Boyarintsev
Dmitry Boyarintsev wrote 6 lines
The difference is mentioned in deflateInit2() functions, where a user can specify windowBits. If the value is negative, to Gzip header would be generated. I presume the gzcompress is passing negative value to the function, while gzencode passes a positive value to the function.

I'm actually experimenting that that (or similar) right now.... I already use TCompressionStream included with FPC, and I see its constructor has an option ASkipHeaders which defaults to False. I'm now setting that parameter to True and will soon see if that works for my code or not.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

silvioprog
to Graeme Geldenhuys: I need to compress a file the same way as PHP's gzcompress() function:…

On Wed, Oct 7, 2015 at 12:26 PM, Graeme Geldenhuys <

Show quoted text 9 lines
wrote: Hi, I need to compress a file the same way as PHP's gzcompress() function: http://php.net/manual/en/function.gzcompress.php-pascal <http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal>

[...]

About compress a string with GZ, this one[1] show how to compress streams, that can be used to compress strings too, via something like TStringStream. =)

I believe that this example is compatible with 2.6.4+.

[1] http://www.gocher.me/GZIP

Silvio Clécio

Reply by email

Graeme Geldenhuys
to Graeme Geldenhuys: That didn't work for my experiment. :-/ - Graeme - fpGUI Toolkit - a cross-platform GUI…
Graeme Geldenhuys wrote
> > constructor has an option ASkipHeaders which defaults to False. I'm now > > setting that parameter to True and will soon see if that works for my > > code or not. That didn't work for my experiment. :-/

Correction, I made a silly mistake in my code. I've managed to get it to work with TCompressionStream and with ASkipHeaders = False (the default).

Thanks for everybody's feedback on this.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ My public PGP key: http://tinyurl.com/graeme-pgp

Reply by email

silvioprog
to Graeme Geldenhuys

On Wed, Oct 7, 2015 at 1:42 PM, Graeme Geldenhuys <

wrote:

[...]

Correction, I made a silly mistake in my code. I've managed to get it to work with TCompressionStream and with ASkipHeaders = False (the default).

The link that I sent has a nice explanation (and tests) about GZIP header:

"GZip:
2 bytes $1f $8b (IDentification)
1 byte $08 Compression Method = deflate
1 byte $00 FLaGs bit 0 FTEXT - indicates file is ASCII text (can be safely ignored) bit 1 FHCRC - there is a CRC16 for the header immediately following the header bit 2 FEXTRA - extra fields are present bit 3 FNAME - the zero-terminated filename is present. encoding; ISO-8859-1. bit 4 FCOMMENT - a zero-terminated file comment is present. encoding: ISO-8859-1 bit 5-7 reserved
4 bytes $00000000 Modification TIME = no time stamp is available" More ... <http://www.gocher.me/GZIP>

Silvio Clécio

Reply by email