Refactoring

This commit is contained in:
Alexander Simonov 2012-02-14 00:03:34 +02:00
parent f9b9a30b6f
commit 7205983957
4 changed files with 41 additions and 35 deletions

View File

@ -26,23 +26,27 @@ module Zip
end end
def write_e_o_c_d(io, offset) #:nodoc: def write_e_o_c_d(io, offset) #:nodoc:
io << tmp = [
[END_OF_CENTRAL_DIRECTORY_SIGNATURE, END_OF_CENTRAL_DIRECTORY_SIGNATURE,
0 , # @numberOfThisDisk 0 , # @numberOfThisDisk
0 , # @numberOfDiskWithStartOfCDir 0 , # @numberOfDiskWithStartOfCDir
@entrySet? @entrySet.size : 0 , @entrySet? @entrySet.size : 0 ,
@entrySet? @entrySet.size : 0 , @entrySet? @entrySet.size : 0 ,
cdir_size , cdir_size ,
offset , offset ,
@comment ? @comment.length : 0 ].pack('VvvvvVVv') @comment ? @comment.length : 0
io << @comment ]
io << tmp.pack('VvvvvVVv')
io << @comment
end end
private :write_e_o_c_d private :write_e_o_c_d
def cdir_size #:nodoc: def cdir_size #:nodoc:
# does not include eocd # does not include eocd
@entrySet.inject(0) { |value, entry| entry.cdir_header_size + value } @entrySet.inject(0) do |value, entry|
entry.cdir_header_size + value
end
end end
private :cdir_size private :cdir_size
@ -68,7 +72,8 @@ module Zip
end end
@entrySet = ZipEntrySet.new @entrySet = ZipEntrySet.new
@size.times do @size.times do
@entrySet << ZipEntry.read_c_dir_entry(io) tmp = ZipEntry.read_c_dir_entry(io)
@entrySet << tmp
end end
end end
@ -100,7 +105,7 @@ module Zip
sigIndex = buf.rindex([END_OF_CENTRAL_DIRECTORY_SIGNATURE].pack('V')) sigIndex = buf.rindex([END_OF_CENTRAL_DIRECTORY_SIGNATURE].pack('V'))
raise ZipError, "Zip end of central directory signature not found" unless sigIndex raise ZipError, "Zip end of central directory signature not found" unless sigIndex
buf = buf.slice!((sigIndex+4)...(buf.size)) buf = buf.slice!((sigIndex + 4)...(buf.bytesize))
def buf.read(count) def buf.read(count)
slice!(0, count) slice!(0, count)

View File

@ -60,7 +60,7 @@ module Zip
attr_accessor :atime, :ctime, :mtime, :flag attr_accessor :atime, :ctime, :mtime, :flag
def merge(binstr) def merge(binstr)
binstr == "" and return return if binstr.empty?
size, content = initial_parse(binstr) size, content = initial_parse(binstr)
size or return size or return
@flag, mtime, atime, ctime = content.unpack("CVVV") @flag, mtime, atime, ctime = content.unpack("CVVV")
@ -103,10 +103,10 @@ module Zip
attr_accessor :uid, :gid attr_accessor :uid, :gid
def merge(binstr) def merge(binstr)
binstr == "" and return return if binstr.empty?
size, content = initial_parse(binstr) size, content = initial_parse(binstr)
# size: 0 for central direcotry. 4 for local header # size: 0 for central directory. 4 for local header
return if(! size || size == 0) return if(!size || size == 0)
uid, gid = content.unpack("vv") uid, gid = content.unpack("vv")
@uid ||= uid @uid ||= uid
@gid ||= gid @gid ||= gid
@ -132,17 +132,17 @@ module Zip
end end
def merge(binstr) def merge(binstr)
binstr == "" and return return if binstr.empty?
i = 0 i = 0
while i < binstr.bytesize while i < binstr.bytesize
id = binstr[i,2] id = binstr[i,2]
len = binstr[i+2,2].to_s.unpack("v")[0] len = binstr[i + 2,2].to_s.unpack("v")[0]
if id && ID_MAP.member?(id) if id && ID_MAP.member?(id)
field_name = ID_MAP[id].name field_name = ID_MAP[id].name
if self.member?(field_name) if self.member?(field_name)
self[field_name].mergea(binstr[i, len+4]) self[field_name].mergea(binstr[i, len + 4])
else else
field_obj = ID_MAP[id].new(binstr[i, len+4]) field_obj = ID_MAP[id].new(binstr[i, len + 4])
self[field_name] = field_obj self[field_name] = field_obj
end end
elsif id elsif id
@ -158,7 +158,7 @@ module Zip
self["Unknown"] << binstr[i..-1] self["Unknown"] << binstr[i..-1]
break break
end end
self["Unknown"] << binstr[i, len+4] self["Unknown"] << binstr[i, len + 4]
end end
i += len + 4 i += len + 4
end end
@ -180,9 +180,9 @@ module Zip
def to_local_bin def to_local_bin
s = "" s = ""
each { |k, v| each do |k, v|
s << v.to_local_bin s << v.to_local_bin
} end
s s
end end
alias :to_s :to_local_bin alias :to_s :to_local_bin

View File

@ -61,16 +61,18 @@ module Zip
super() super()
@name = fileName @name = fileName
@comment = "" @comment = ""
if (::File.exists?(fileName)) && !buffer case
::File.open(name, "rb") { |f| read_from_stream(f) } when ::File.exists?(fileName) && !buffer
elsif (create) ::File.open(name, "rb") do |f|
@entrySet = ZipEntrySet.new read_from_stream(f)
else end
raise ZipError, "File #{fileName} not found" when create
@entrySet = ZipEntrySet.new
else
raise ZipError, "File #{fileName} not found"
end end
@create = create @create = create
@storedEntries = @entrySet.dup @storedEntries = @entrySet.dup
@restore_ownership = false @restore_ownership = false
@restore_permissions = false @restore_permissions = false
@restore_times = true @restore_times = true

View File

@ -74,7 +74,7 @@ module Zip
update_local_headers update_local_headers
write_central_directory write_central_directory
@closed = true @closed = true
return @outputStream @outputStream
end end
# Closes the current entry and opens a new for writing. # Closes the current entry and opens a new for writing.
@ -110,11 +110,11 @@ module Zip
end end
private private
def finalize_current_entry def finalize_current_entry
return unless @currentEntry return unless @currentEntry
finish finish
@currentEntry.compressed_size = @outputStream.tell - @currentEntry.localHeaderOffset - @currentEntry.compressed_size = @outputStream.tell - @currentEntry.localHeaderOffset - @currentEntry.calculate_local_header_size
@currentEntry.calculate_local_header_size
@currentEntry.size = @compressor.size @currentEntry.size = @compressor.size
@currentEntry.crc = @compressor.crc @currentEntry.crc = @compressor.crc
@currentEntry = nil @currentEntry = nil
@ -138,12 +138,11 @@ module Zip
end end
def update_local_headers def update_local_headers
pos = @outputStream.tell pos = @outputStream.pos
@entrySet.each { @entrySet.each do |entry|
|entry|
@outputStream.pos = entry.localHeaderOffset @outputStream.pos = entry.localHeaderOffset
entry.write_local_entry(@outputStream) entry.write_local_entry(@outputStream)
} end
@outputStream.pos = pos @outputStream.pos = pos
end end