Sha256: f37ae45b01749195bea85d3ccb3dd4f980c63fd12ec37275dcb7c50ba794b493

Contents?: true

Size: 1.65 KB

Versions: 16

Compression:

Stored size: 1.65 KB

Contents

##
# $Id: entry.rb 11173 2010-11-30 03:52:46Z egypt $
##

module Rex
module Zip

class Entry

	attr_accessor :name, :flags, :info, :xtra, :comment, :attrs
	attr_reader :data

	def initialize(fname, data, compmeth, timestamp=nil, attrs=nil, xtra=nil, comment=nil)
		@name = fname
		@data = data
		@xtra = xtra
		@xtra ||= ''
		@comment = comment
		@comment ||= ''
		@attrs = attrs
		@attrs ||= 0

		# XXX: sanitize timestmap (assume now)
		timestamp ||= Time.now
		@flags = CompFlags.new(0, compmeth, timestamp)

		if (@data)
			compress
		else
			@data = ''
			@info = CompInfo.new(0, 0, 0)
		end
		@compdata ||= ''
	end

	def data=(val)
		@data = val
		compress
	end

	def compress
		@crc = Zlib.crc32(@data, 0)
		case @flags.compmeth

		when CM_STORE
			@compdata = @data

		when CM_DEFLATE
			z = Zlib::Deflate.new(Zlib::BEST_COMPRESSION)
			@compdata = z.deflate(@data, Zlib::FINISH)
			z.close
			@compdata = @compdata[2, @compdata.length-6]

		else
			raise 'Unsupported compression method: %u' % @flags.compmeth
		end

		# if compressing doesn't help, just store it
		if (@compdata.length > @data.length)
			@compdata = @data
			@flags.compmeth = CM_STORE
		end

		@info = CompInfo.new(@crc, @compdata.length, @data.length)
	end


	def relative_path
		if (@name[0,1] == '/')
			return @name[1,@name.length]
		end
		@name
	end


	def pack
		ret = ''

		#  - lfh 1
		lfh = LocalFileHdr.new(self)
		ret << lfh.pack

		#  - data 1
		if (@compdata)
			ret << @compdata
		end

		if (@gpbf & GPBF_USE_DATADESC)
			#  - data desc 1
			dd = DataDesc.new(@info)
			ret << dd.pack
		end

		ret
	end

	def inspect
		"#<#{self.class} name:#{name}, data:#{@data.length} bytes>"
	end

end

end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
librex-0.0.32 lib/rex/zip/entry.rb
librex-0.0.31 lib/rex/zip/entry.rb
librex-0.0.30 lib/rex/zip/entry.rb
librex-0.0.29 lib/rex/zip/entry.rb
librex-0.0.28 lib/rex/zip/entry.rb
librex-0.0.27 lib/rex/zip/entry.rb
librex-0.0.26 lib/rex/zip/entry.rb
librex-0.0.25 lib/rex/zip/entry.rb
librex-0.0.23 lib/rex/zip/entry.rb
librex-0.0.21 lib/rex/zip/entry.rb
librex-0.0.19 lib/rex/zip/entry.rb
librex-0.0.17 lib/rex/zip/entry.rb
librex-0.0.13 lib/rex/zip/entry.rb
librex-0.0.12 lib/rex/zip/entry.rb
librex-0.0.7 lib/rex/zip/entry.rb
librex-0.0.6 lib/rex/zip/entry.rb