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.



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

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

Instance Attribute Details

#entriesObject (readonly)

An array of the Entry objects stored in this Archive.



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

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.



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

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



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

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.



67
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
# File 'lib/rex/zip/archive.rb', line 67

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.



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

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

#set_comment(comment) ⇒ Object



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

def set_comment(comment)
	@comment = comment
end