2012-06-29 05:18:28 +00:00
|
|
|
# -*- coding: binary -*-
|
2010-02-10 17:27:40 +00:00
|
|
|
|
|
|
|
module Rex
|
|
|
|
module Zip
|
|
|
|
|
|
|
|
#
|
|
|
|
# This represents an entire archive.
|
|
|
|
#
|
|
|
|
class Archive
|
2011-05-25 16:45:20 +00:00
|
|
|
|
|
|
|
# An array of the Entry objects stored in this Archive.
|
2010-08-19 17:08:29 +00:00
|
|
|
attr_reader :entries
|
2010-02-10 17:27:40 +00:00
|
|
|
|
2011-05-25 16:45:20 +00:00
|
|
|
|
2010-02-10 17:27:40 +00:00
|
|
|
def initialize(compmeth=CM_DEFLATE)
|
|
|
|
@compmeth = compmeth
|
|
|
|
@entries = []
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-05-25 16:45:20 +00:00
|
|
|
#
|
|
|
|
# Create a new Entry and add it to the archive.
|
|
|
|
#
|
2013-08-24 23:06:31 +00:00
|
|
|
# If fdata is set, the file is populated with that data
|
|
|
|
# from the calling method. If fdata is nil, then the
|
|
|
|
# fs is checked for the file.
|
|
|
|
#
|
2010-02-10 17:27:40 +00:00
|
|
|
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
|
2013-08-24 23:06:31 +00:00
|
|
|
fdata = ''
|
2013-08-25 11:21:37 +00:00
|
|
|
unless fname[-1,1] == '/'
|
|
|
|
fname += '/'
|
2013-08-24 23:17:26 +00:00
|
|
|
end
|
2010-02-10 17:27:40 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def set_comment(comment)
|
|
|
|
@comment = comment
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-05-25 16:45:20 +00:00
|
|
|
#
|
|
|
|
# Write the compressed file to +fname+.
|
|
|
|
#
|
2010-02-10 17:27:40 +00:00
|
|
|
def save_to(fname)
|
|
|
|
f = File.open(fname, 'wb')
|
|
|
|
f.write(pack)
|
|
|
|
f.close
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-05-25 16:45:20 +00:00
|
|
|
#
|
|
|
|
# Compress this archive and return the resulting zip file as a String.
|
|
|
|
#
|
2010-02-10 17:27:40 +00:00
|
|
|
def pack
|
2012-06-29 06:07:58 +00:00
|
|
|
ret = ''
|
2010-02-10 17:27:40 +00:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2010-11-10 23:04:01 +00:00
|
|
|
def inspect
|
|
|
|
"#<#{self.class} entries = [#{@entries.map{|e| e.name}.join(",")}]>"
|
|
|
|
end
|
|
|
|
|
2010-02-10 17:27:40 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|