Reputation: 2299
I have a string, but I don't know the type of encoding.
Here's what the raw data looks like:
{
"securityProxyResponseEnvelope":{
"resultCode":"OK",
"apiResponse":"{zlibe}9mtdE350h9rd4h7wlFX3AkeCtNsb40FFaEZAl/CfcNNKrhGXawhgAs2rI4rnEgIwLpgJkKl+qkB0kzJ6+ZFmmz12Pl9/9MPdA1unUKL5OdHcWmAZim3ZjDXFGCW4zruCS/IOSiU1qVKAF5qIbocB4+2rAF7zH18SRtmXM8YW3eYs5w1NPjmYkM31W8x7QvrKkzFscH3kqDwmYn0I2gNNOtfwuKjWd5snunyqxPopZHNX3CBdW/pj4+N0tJXjAoHorCe8Ypmjxnvh3zthkLTbiBLgeULH1hGvVtkI0C9PGMyt/92upVW6qHxqCYoO/LTJK1tq6OpBnMRBNZDDntSRkrzp+1RpvzbBxFtwQ9jh45eSthbG5hq+D2oJkW5zrGi6TM8eG4ztCqRoO9dEvz2JbQsDCTPz70+C6iPYdkvOyqji18ysLjBbGcHw1j45YItcurVxp0FChxXrnHZwu6m430xKEp7ONxvgEZurt3T8qAjrkrbHfd8jRjDydUXYsMoa",
"session":"n3qp6jzHwZkXWSMW3VBF:jitqBjBmlZbrgcEgY7Od",
"parameters":{
}
}
}
I want to decompress the string in data['securityProxyResponseEnvelope']['apiResponse']
.
Here's what I'm doing:
@clear_string_from_data = '9mtdE350h9rd4h7wlFX3AkeCtNsb40FFaEZAl/CfcNNKrhGXawhgAs2rI4rnEgIwLpgJkKl+qkB0kzJ6+ZFmmz12Pl9/9MPdA1unUKL5OdHcWmAZim3ZjDXFGCW4zruCS/IOSiU1qVKAF5qIbocB4+2rAF7zH18SRtmXM8YW3eYs5w1NPjmYkM31W8x7QvrKkzFscH3kqDwmYn0I2gNNOtfwuKjWd5snunyqxPopZHNX3CBdW/pj4+N0tJXjAoHorCe8Ypmjxnvh3zthkLTbiBLgeULH1hGvVtkI0C9PGMyt/92upVW6qHxqCYoO/LTJK1tq6OpBnMRBNZDDntSRkrzp+1RpvzbBxFtwQ9jh45eSthbG5hq+D2oJkW5zrGi6TM8eG4ztCqRoO9dEvz2JbQsDCTPz70+C6iPYdkvOyqji18ysLjBbGcHw1j45YItcurVxp0FChxXrnHZwu6m430xKEp7ONxvgEZurt3T8qAjrkrbHfd8jRjDydUXYsMoa'
@decoded = Base64.decode64(@clear_string_from_data)
@inflated = Zlib::Inflate.inflate(@decoded)
But this returns
#=> Zlib::DataError: incorrect header check
What's causing this and what could I try next to decompress the data?
Upvotes: 5
Views: 7700
Reputation: 52268
This is a very unsatisfying answer (as it doesn't give an explanation of the cause). But I was using rails 6.1 and hadn't updated gems for a long time (at least a couple of years). I did so and the Zlib problem went away. I'm really sorry it's such a non-technical answer, but I put it here because it might be the most practical solution (it was in my case).
Upvotes: 0
Reputation: 745
Zlib::GzipReader is designed specifically for decompressing data that has been compressed with the Gzip compression format. Gzip is a widely used compression format for files and streams. If your data is Gzip-compressed, this is the appropriate choice.
result = Zlib::GzipReader.new(StringIO.new(response.to_s)).read
Zlib::Inflate inflate is a more general-purpose decompression method. It can handle various compression formats supported by the Zlib library, including Gzip, Deflate, and others. If your data is compressed using a format other than Gzip, this method allows you to handle it.
result = Zlib::Inflate.inflate(response)
Upvotes: 0
Reputation: 3092
I was getting this when trying to call inflate on a data that hadn't been deflated by Zlib. In my case it was for a unit test and I sent in a plain string and simply forgot to call .deflate on it first.
In your case, if you do this instead you don't get the error:
@decoded = Zlib::Deflate.deflate(@clear_string_from_data)
@inflated = Zlib::Inflate.inflate(@decoded)
Upvotes: 2
Reputation: 112349
What's causing it is that it is not zlib data. You should ask whoever is producing that raw data.
Upvotes: 1