From 3ad19e08b710eabb64a932df2fb359a260f4ae7b Mon Sep 17 00:00:00 2001 From: Adam Vandenberg Date: Sun, 29 Apr 2012 11:45:46 -0700 Subject: Extract detection of compression types Separate out detecting compression types from uncompressing and staging. --- Library/Homebrew/extend/pathname.rb | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'Library/Homebrew/extend') diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 3dda828d5..515356861 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -240,6 +240,31 @@ class Pathname nil end + def compression_type + # Don't treat jars as compressed + return nil if self.extname == '.jar' + + # OS X installer package + return :pkg if self.extname == '.pkg' + + # get the first six bytes + magic_bytes = nil + File.open(self) { |f| magic_bytes = f.read(6) } + + # magic numbers stolen from /usr/share/file/magic/ + case magic_bytes + when /^PK\003\004/ then :zip + when /^\037\213/ then :gzip + when /^BZh/ then :bzip2 + when /^\037\235/ then :compress + when /^\xFD7zXZ\x00/ then :xz + when /^Rar!/ then :rar + else + # Assume it is not an archive + nil + end + end + def incremental_hash(hasher) incr_hash = hasher.new self.open('r') do |f| -- cgit v1.2.3