I'm trying to decode a gzipped string with zlib
and jruby
. Here is the minimal working example.
require 'stringio'
require 'zlib'
str = 'H4sIAAAAAAAA/y2NwQrDIBAFfyXstUbWNWrir5RSrEoQUi2JOZSQf6+EHt8wzDtgKd7VVPIG9n7AMwWwYhj1MBkkwtEwcN7vq/NfsAo5MnhFt6Y8g71WcDXW9I5ggVCYHqlH0xE12RJ1N5SIwGBpJ3UPTVOKa41IssGS5z+Vhhs1SdHo9okxXPXzcf4AY45Ve6EAAAA='
input = StringIO.new(str)
puts Zlib::GzipReader.new(input).read
And this is the output I get
/Users/duke/.rvm/rubies/jruby-1.7.23/bin/jruby --1.9 -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) /Users/duke/RubymineProjects/untitled/gzip_test.rb
Zlib::GzipFile::Error: not in gzip format
initialize at org/jruby/ext/zlib/JZlibRubyGzipReader.java:156
new at org/jruby/ext/zlib/JZlibRubyGzipReader.java:85
(root) at /Users/duke/RubymineProjects/untitled/gzip_test.rb:6
load at org/jruby/RubyKernel.java:1059
(root) at -e:1
Process finished with exit code 1
The gzipped string is valid. You can try it here http://www.txtwizard.net/compression
Your str
contains Base64 encoded data. However, since Zlib::GzipReader
doesn't decode the data on its own but expects the raw binary gzip data, it fails.
You can manually decode the data before creating your StringIO
object though:
require 'base64'
require 'stringio'
require 'zlib'
str = 'H4sIAAAAAAAA/y2NwQrDIBAFfyXstUbWNWrir5RSrEoQUi2JOZSQf6+EHt8wzDtgKd7VVPIG9n7AMwWwYhj1MBkkwtEwcN7vq/NfsAo5MnhFt6Y8g71WcDXW9I5ggVCYHqlH0xE12RJ1N5SIwGBpJ3UPTVOKa41IssGS5z+Vhhs1SdHo9okxXPXzcf4AY45Ve6EAAAA='
raw = Base64.decode64(str)
input = StringIO.new(raw)
puts Zlib::GzipReader.new(input).read
# => {"locations":[{"_id":1486497022087,"accuracy":50.0,"bearing":0.0,"datetime":"2017-02-07 22:50:22 +0300","latitude":55.660023,"longitude":37.759313,"speed":0.0}]}
The website you linked to also describes this behavior (emphasis mine):
This simple online text compression tool is compressing a plain text and decompressing compressed base64 string with gzip, bzip2 and deflate algorithms