Skip to content

Instantly share code, notes, and snippets.

@hc5duke
Created July 30, 2012 06:00
Show Gist options
  • Save hc5duke/3205218 to your computer and use it in GitHub Desktop.
Save hc5duke/3205218 to your computer and use it in GitHub Desktop.
Parse GZipped results
#!/usr/bin/env ruby
require 'active_support'
gzips = {
:java => [31, -117, 8, 0, 0, 0, 0, 0, 0, 0, -77, 41, 74, 45, 44, 77, 45, 46, -79, -77, 41, -120, -49, 76, -79, 51, 50, 48, -78, -80, -47, 7, 51, 109, -118, -13, 75, -117, -110, 83, -29, 75, 42, 11, 82, -19, 12, 108, -12, -111, -71, 54, -6, 48, 109, 0, -21, -40, -25, -121, 64, 0, 0, 0],
:ruby => [31, -117, 8, 0, 32, 45, 19, 80, 0, 3, -77, 41, 74, 45, 44, 77, 45, 46, -79, -77, 41, -120, -49, 76, -79, 51, 50, 48, -78, -80, -47, 7, 51, 109, -118, -13, 75, -117, -110, 83, -29, 75, 42, 11, 82, -19, 12, 108, -12, -111, -71, 54, -6, 48, 109, 0, -21, -40, -25, -121, 64, 0, 0, 0],
}
def formatted_put(text, comment)
text = text.join(' ') if text.is_a? Array
text = text.to_s unless text.is_a? String
puts "#{text}#{' ' * (20 - text.length)} # #{comment}"
end
gzips.each do |lang, gz|
puts "*" * 30
puts lang
# magic header
arr = gz.shift(2)
raise "ERROR: magic header incorrect #{arr.inspect}" unless arr == [31, -117]
formatted_put(arr.join(' '), "magic header")
# compression method
arr = gz.shift
raise "ERROR: Invalid compression method #{arr}" unless (0..8).include?(arr)
method = case arr
when 0 then "store (copied)"
when 1 then "compress"
when 2 then "pack"
when 3 then "lzh"
when (4..7) then "reserved"
when 8 then "deflate"
end
formatted_put(arr, "Compression method: #{method}")
# flags
arr = gz.shift
formatted_put(arr, "Flag: something?")
arr = gz.shift(4)
time = (arr[3] << 24) + (arr[2] << 16) + (arr[1] << 8) + arr[0]
formatted_put(arr.join(' '), "File mod. time = #{Time.at(time).to_s}")
arr = gz.shift
formatted_put(arr, "Extra flags")
arr = gz.shift
os = case arr
when 0 then "FAT filesystem (MS-DOS, OS/2, NT/Win32)"
when 1 then "Amiga"
when 2 then "VMS (or OpenVMS)"
when 3 then "Unix"
when 4 then "VM/CMS"
when 5 then "Atari TOS"
when 6 then "HPFS filesystem (OS/2, NT)"
when 7 then "Macintosh"
when 8 then "Z-System"
when 9 then "CP/M"
when 10 then "TOPS-20"
when 11 then "NTFS filesystem (NT)"
when 12 then "QDOS"
when 13 then "Acorn RISCOS"
when 15 then "unknown"
end
formatted_put(arr, "OS: #{os}")
end
=begin
$ ./parse.rb
******************************
java
31 -117 # magic header
8 # Compression method: deflate
0 # Flag: something?
0 0 0 0 # File mod. time = 1969-12-31 16:00:00 -0800
0 # Extra flags
0 # OS: FAT filesystem (MS-DOS, OS/2, NT/Win32)
******************************
ruby
31 -117 # magic header
8 # Compression method: deflate
0 # Flag: something?
32 45 19 80 # File mod. time = 2012-07-27 17:06:56 -0700
0 # Extra flags
3 # OS: Unix
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment