From: Pat Patterson Date: 2007-03-15T02:14:23+09:00 Subject: Re: Ruby Newbie Problems with deflate, base64... Thanks, Eden - that works great and is much cleaner than cutting the header and checksum off of the deflated data. Cheers, Pat eden li wrote: > You can prevent deflate from generating the header by passing in - > MAX_WBITS to the options for Deflate.new. The following method > emulates gzdeflate from php: > > def gzdeflate(s) > Zlib::Deflate.new(nil, -Zlib::MAX_WBITS).deflate(s, Zlib::FINISH) > end > > puts gzdeflate("Hello World").unpack('H*').first > # => f348cdc9c95708cf2fca490100 > > On Mar 14, 8:16 am, Pat Patterson wrote: > >> OK - so now I know what is happening... >> >> Zlib::Deflate.deflate implements ZLIB compression according to RFC 1950. >> ZLIB defines a 2 byte header containing a variety of flags and a 4 byte >> trailer containing an Adler-32 checksum. Just out of interest, the >> header that I'm seeing translates as 'compression method = deflate, >> windows size = 32k, no preset dictionary, default compression level', >> which makes perfect sense. Deflate compression itself is defined by RFC >> 1951. >> >> So - if you want 'raw' deflated data (which is called for in many >> situations), cutting off the leading 2 and trailing 4 bytes is exactly >> what you need to do. >> >> Cheers, >> >> Pat >> >> >> >> Pat Patterson wrote: >> >>> Thanks, Brian! That revealed an obvious bug in the code I was using to >>> examine the deflated data (should have been 0.upto(deflated.length-1)). >>> >>> So Base64 /is/ working correctly. Deflate prepends 2 bytes (seems to >>> be constant 0x789c for default deflate level) and appends 4 bytes >>> (rather than 1 and 5 as I thought) to the deflated data. When I cut >>> those off, I can get Ruby to work the same as PHP. >>> >>> Still - it would be nice if deflate worked the same as on Java, PHP, ... >>> >>> Cheers, >>> >>> Pat >>> >>> Brian Candler wrote: >>> >>>> On Tue, Mar 13, 2007 at 01:21:52PM +0900, Pat Patterson wrote: >>>> >>>>> While the Ruby >>>>> >>>>> deflated = Zlib::Deflate.deflate( "Hello world" ) >>>>> myhex = "" >>>>> 1.upto(deflated.length) { |i| myhex << "%02x" % deflated[i] } >>>>> puts myhex >>>>> >>>>> Shows >>>>> >>>>> 9cf348cdc9c95728cf2fca49010018ab043d00 >>>>> >>>>> (BTW - if anyone knows a more succinct way to hex encode a string in >>>>> Ruby, that would be useful) >>>>> >>>> str = "\001\377" >>>> puts str.unpack("H*") >>>> >> -- >> Pat Patterson - pat.patter...@sun.com >> Federation Architect, >> Sun Microsystems, Inc.http://blogs.sun.com/superpat >> > > > -- Pat Patterson - pat.patterson@sun.com Federation Architect, Sun Microsystems, Inc. http://blogs.sun.com/superpat