Skip to content

Instantly share code, notes, and snippets.

@gouravtiwari
Created July 21, 2016 18:22
Show Gist options
  • Save gouravtiwari/37f3c753ca9912fd4c01bc8d39d3e49f to your computer and use it in GitHub Desktop.
Save gouravtiwari/37f3c753ca9912fd4c01bc8d39d3e49f to your computer and use it in GitHub Desktop.
Check for compressed string before decompressing
# 2.2.2 :037 > gzip = 'Hi and hello'
# => "Hi and hello"
# 2.2.2 :038 > gzip.bytes[0..1]
# => [72, 105]
# 2.2.2 :039 > gzip.bytes[0..1] == [31,139]
# => false
# 2.2.2 :040 > gzip = ActiveSupport::Gzip.compress('what is this compress me!')
# => "\x1F\x8B\b\x00\x19\x11\x91W\x00\x03+\xCFH,Q\xC8,V(\xC9\x00\x12\xC9\xF9\xB9\x05E\xA9\xC5\xC5\n\xB9\xA9\x8A\x00\x14\x84\x90'\x19\x00\x00\x00"
# 2.2.2 :041 > gzip.bytes[0..1]
# => [31, 139]
# 2.2.2 :042 > gzip.bytes[0..1] == [31,139]
# => true
# 2.2.2 :043 > gzip = ActiveSupport::Gzip.compress('compress me!')
# => "\x1F\x8B\b\x00*\x11\x91W\x00\x03K\xCE\xCF-(J-.V\xC8MU\x04\x00R>n\x83\f\x00\x00\x00"
# 2.2.2 :044 > gzip.bytes[0..1]
# => [31, 139]
# 2.2.2 :045 > gzip.bytes[0..1] == [31,139]
# => true
# 2.2.2 :046 >
require 'active_support/gzip'
gzip = 'Hi and hello'
p gzip.bytes[0..1]
p gzip.bytes[0..1] == [31,139]
gzip = ActiveSupport::Gzip.compress('what is this compress me!')
p gzip.bytes[0..1]
p gzip.bytes[0..1] == [31,139]
gzip = ActiveSupport::Gzip.compress('compress me!')
p gzip.bytes[0..1]
p gzip.bytes[0..1] == [31,139]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment