Class: Rex::Zip::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/rex/zip/archive.rb

Overview

This represents an entire archive.

Direct Known Subclasses

Jar

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(compmeth = CM_DEFLATE) ⇒ Archive

Returns a new instance of Archive.



18
19
20
21
# File 'lib/rex/zip/archive.rb', line 18

def initialize(compmeth=CM_DEFLATE)
  @compmeth = compmeth
  @entries = []
end

Instance Attribute Details

#entriesObject (readonly)

An array of the Entry objects stored in this Archive.



15
16
17
# File 'lib/rex/zip/archive.rb', line 15

def entries
  @entries
end

Instance Method Details

#add_file(fname, fdata = nil, xtra = nil, comment = nil) ⇒ Object

Create a new Entry and add it to the archive.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rex/zip/archive.rb', line 27

def add_file(fname, fdata=nil, xtra=nil, comment=nil)
  if (not fdata)
    begin
      st = File.stat(fname)
    rescue
      return nil
    end

    ts = st.mtime
    if (st.directory?)
      attrs = EFA_ISDIR
      fname += '/'
    else
      f = File.open(fname, 'rb')
      fdata = f.read(f.stat.size)
      f.close
    end
  end

  @entries << Entry.new(fname, fdata, @compmeth, ts, attrs, xtra, comment)
end

#inspectObject



102
103
104
# File 'lib/rex/zip/archive.rb', line 102

def inspect
  "#<#{self.class} entries = [#{@entries.map{|e| e.name}.join(",")}]>"
end

#packObject

Compress this archive and return the resulting zip file as a String.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rex/zip/archive.rb', line 68

def pack
  ret = ''

  # save the offests
  offsets = []

  # file 1 .. file n
  @entries.each { |ent|
    offsets << ret.length
    ret << ent.pack
  }

  # archive decryption header (unsupported)
  # archive extra data record (unsupported)

  # central directory
  cfd_offset = ret.length
  idx = 0
  @entries.each { |ent|
    cfd = CentralDir.new(ent, offsets[idx])
    ret << cfd.pack
    idx += 1
  }

  # zip64 end of central dir record (unsupported)
  # zip64 end of central dir locator (unsupported)

  # end of central directory record
  cur_offset = ret.length - cfd_offset
  ret << CentralDirEnd.new(@entries.length, cur_offset, cfd_offset, @comment).pack

  ret
end

#save_to(fname) ⇒ Object

Write the compressed file to fname.



58
59
60
61
62
# File 'lib/rex/zip/archive.rb', line 58

def save_to(fname)
  f = File.open(fname, 'wb')
  f.write(pack)
  f.close
end

#set_comment(comment) ⇒ Object



50
51
52
# File 'lib/rex/zip/archive.rb', line 50

def set_comment(comment)
  @comment = comment
end