Class: Nispack

Inherits:
Object
  • Object
show all
Defined in:
lib/nispack.rb

Constant Summary collapse

VERSION =
"1.0.0"
SIGNATURE =
"NISPACK\0\0\0\0\0".force_encoding("binary")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, file, entries) ⇒ Nispack

Returns a new instance of Nispack.



13
14
15
16
17
# File 'lib/nispack.rb', line 13

def initialize(name, file, entries)
   @name = name
   @file = file
   @entries = entries
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



11
12
13
# File 'lib/nispack.rb', line 11

def entries
  @entries
end

#fileObject (readonly)

Returns the value of attribute file.



10
11
12
# File 'lib/nispack.rb', line 10

def file
  @file
end

#filenameObject (readonly)

Returns the value of attribute filename.



9
10
11
# File 'lib/nispack.rb', line 9

def filename
  @filename
end

Class Method Details

.create_from_directory(options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/nispack.rb', line 75

def self.create_from_directory(options)
   dir = options[:dir] || Dir.pwd
   name = options[:name] || File.basename(Dir.pwd) + ".DAT"
   files = Dir.entries(dir).select{|f| File.file?(File.join(dir, f))}
   out = File.new(name, "wb+")
   out.write(SIGNATURE)
   out.write([files.size].pack("l"))
   start = 16 + 44 * files.size
   files.each do |f|
      out.write([f].pack("a32"))
      out.write([start].pack("l"))
      size = File.size(File.join(dir, f))
      out.write([size].pack("l"))
      out.write([0].pack("l"))
      start += size
   end
   files.each do |f|
      out.write(IO.binread(File.join(dir, f)))
   end
   out.close
end

.open(filename) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nispack.rb', line 19

def self.open(filename)
   name = File.basename(filename)
   file = File.new(filename, "rb")
   if file.read(SIGNATURE.bytesize) != SIGNATURE
      raise "#{filename} is not a Nispack: wrong signature."
   end
   n_entries = file.read(4).unpack('l')[0]
   entries = Array.new(n_entries)
   entries.map! do |entry|
      entry = Hash.new
      entry[:name] = file.read(32).strip
      entry[:start] = file.read(4).unpack('l')[0]
      entry[:size] = file.read(4).unpack('l')[0]
      file.pos += 4
      entry
   end
   file.pos = 0
   nispack = self.new(name, file, entries)
   if block_given?
      yield nispack
      nispack.close
   else
      return nispack
   end
end

Instance Method Details

#closeObject



45
46
47
# File 'lib/nispack.rb', line 45

def close
   @file.close
end

#extract(name) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/nispack.rb', line 57

def extract(name)
   entry = @entries.select{|x| x[:name] == name}.first
   if entry.nil?
      raise "Can't extract #{name}: not in the Nispack archive"
   end
   extract_entry(entry)
end

#extract_all(dir = File.basename(@name).chomp(File.extname(@name))) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/nispack.rb', line 49

def extract_all(dir = File.basename(@name).chomp(File.extname(@name)))
   old_dir = Dir.pwd
   FileUtils.mkdir_p(dir)
   FileUtils.cd(dir)
   @entries.each {|entry| extract_entry(entry)}
   FileUtils.cd(old_dir)
end