2 # Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
4 # See LICENSE.txt for permissions.
9 require 'rubygems/package'
14 # The format class knows the guts of the RubyGem .gem file format
15 # and provides the capability to read gem files
18 attr_accessor :spec, :file_entries, :gem_path
19 extend Gem::UserInteraction
22 # Constructs an instance of a Format object, representing the gem's
25 # gem:: [String] The file name of the gem
27 def initialize(gem_path)
32 # Reads the named gem file and returns a Format object, representing
33 # the data from the gem file
35 # file_path:: [String] Path to the gem file
37 def self.from_file_by_path(file_path, security_policy = nil)
40 unless File.exist?(file_path)
41 raise Gem::Exception, "Cannot load gem at [#{file_path}] in #{Dir.pwd}"
44 # check for old version gem
45 if File.read(file_path, 20).include?("MD5SUM =")
46 require 'rubygems/old_format'
48 format = OldFormat.from_file_by_path(file_path)
50 open file_path, Gem.binary_mode do |io|
51 format = from_io io, file_path, security_policy
59 # Reads a gem from an io stream and returns a Format object, representing
60 # the data from the gem file
62 # io:: [IO] Stream from which to read the gem
64 def self.from_io(io, gem_path="(io)", security_policy = nil)
67 Package.open io, 'r', security_policy do |pkg|
68 format.spec = pkg.metadata
69 format.file_entries = []
72 size = entry.header.size
73 mode = entry.header.mode
75 format.file_entries << [{
76 "size" => size, "mode" => mode, "path" => entry.full_name,