Skip to content

Instantly share code, notes, and snippets.

@kana
Created February 15, 2013 14:33
Show Gist options
  • Save kana/4960734 to your computer and use it in GitHub Desktop.
Save kana/4960734 to your computer and use it in GitHub Desktop.
Unpack PackFile
def die(message)
puts message
exit
end
def validate(what, actual, expected)
die "Invalid file format: #{what} should be #{expected} but got #{actual}" if
actual != expected
end
def main()
validate 'Magic number', ARGF.read(8), 'PackFile'
validate 'Padding', ARGF.read(4), ' '
file_count = ARGF.read(4).unpack('L<')[0]
file_headers = []
(1..file_count).each do |n|
h = {}
h[:name] = ARGF.read(16).unpack('Z*')[0]
h[:offset] = ARGF.read(4).unpack('L<')[0]
h[:size] = ARGF.read(4).unpack('L<')[0]
validate "Padding ##{n}", ARGF.read(8), "\x00" * 8
file_headers << h
end
file_headers.each do |h|
puts "Extracting #{h[:name]} (offset #{h[:offset]} / size #{h[:size]})"
File.open(h[:name], 'wb') do |io|
io.write(ARGF.read(h[:size]))
end
end
end
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment