Refactoring
This commit is contained in:
parent
f9b9a30b6f
commit
7205983957
|
@ -26,23 +26,27 @@ module Zip
|
|||
end
|
||||
|
||||
def write_e_o_c_d(io, offset) #:nodoc:
|
||||
io <<
|
||||
[END_OF_CENTRAL_DIRECTORY_SIGNATURE,
|
||||
tmp = [
|
||||
END_OF_CENTRAL_DIRECTORY_SIGNATURE,
|
||||
0 , # @numberOfThisDisk
|
||||
0 , # @numberOfDiskWithStartOfCDir
|
||||
@entrySet? @entrySet.size : 0 ,
|
||||
@entrySet? @entrySet.size : 0 ,
|
||||
cdir_size ,
|
||||
@entrySet? @entrySet.size : 0 ,
|
||||
@entrySet? @entrySet.size : 0 ,
|
||||
cdir_size ,
|
||||
offset ,
|
||||
@comment ? @comment.length : 0 ].pack('VvvvvVVv')
|
||||
io << @comment
|
||||
@comment ? @comment.length : 0
|
||||
]
|
||||
io << tmp.pack('VvvvvVVv')
|
||||
io << @comment
|
||||
end
|
||||
|
||||
private :write_e_o_c_d
|
||||
|
||||
def cdir_size #:nodoc:
|
||||
# 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
|
||||
|
||||
private :cdir_size
|
||||
|
@ -68,7 +72,8 @@ module Zip
|
|||
end
|
||||
@entrySet = ZipEntrySet.new
|
||||
@size.times do
|
||||
@entrySet << ZipEntry.read_c_dir_entry(io)
|
||||
tmp = ZipEntry.read_c_dir_entry(io)
|
||||
@entrySet << tmp
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -100,7 +105,7 @@ module Zip
|
|||
|
||||
sigIndex = buf.rindex([END_OF_CENTRAL_DIRECTORY_SIGNATURE].pack('V'))
|
||||
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)
|
||||
slice!(0, count)
|
||||
|
|
|
@ -60,7 +60,7 @@ module Zip
|
|||
attr_accessor :atime, :ctime, :mtime, :flag
|
||||
|
||||
def merge(binstr)
|
||||
binstr == "" and return
|
||||
return if binstr.empty?
|
||||
size, content = initial_parse(binstr)
|
||||
size or return
|
||||
@flag, mtime, atime, ctime = content.unpack("CVVV")
|
||||
|
@ -103,10 +103,10 @@ module Zip
|
|||
attr_accessor :uid, :gid
|
||||
|
||||
def merge(binstr)
|
||||
binstr == "" and return
|
||||
return if binstr.empty?
|
||||
size, content = initial_parse(binstr)
|
||||
# size: 0 for central direcotry. 4 for local header
|
||||
return if(! size || size == 0)
|
||||
# size: 0 for central directory. 4 for local header
|
||||
return if(!size || size == 0)
|
||||
uid, gid = content.unpack("vv")
|
||||
@uid ||= uid
|
||||
@gid ||= gid
|
||||
|
@ -132,17 +132,17 @@ module Zip
|
|||
end
|
||||
|
||||
def merge(binstr)
|
||||
binstr == "" and return
|
||||
return if binstr.empty?
|
||||
i = 0
|
||||
while i < binstr.bytesize
|
||||
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)
|
||||
field_name = ID_MAP[id].name
|
||||
if self.member?(field_name)
|
||||
self[field_name].mergea(binstr[i, len+4])
|
||||
self[field_name].mergea(binstr[i, len + 4])
|
||||
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
|
||||
end
|
||||
elsif id
|
||||
|
@ -158,7 +158,7 @@ module Zip
|
|||
self["Unknown"] << binstr[i..-1]
|
||||
break
|
||||
end
|
||||
self["Unknown"] << binstr[i, len+4]
|
||||
self["Unknown"] << binstr[i, len + 4]
|
||||
end
|
||||
i += len + 4
|
||||
end
|
||||
|
@ -180,9 +180,9 @@ module Zip
|
|||
|
||||
def to_local_bin
|
||||
s = ""
|
||||
each { |k, v|
|
||||
each do |k, v|
|
||||
s << v.to_local_bin
|
||||
}
|
||||
end
|
||||
s
|
||||
end
|
||||
alias :to_s :to_local_bin
|
||||
|
|
|
@ -61,16 +61,18 @@ module Zip
|
|||
super()
|
||||
@name = fileName
|
||||
@comment = ""
|
||||
if (::File.exists?(fileName)) && !buffer
|
||||
::File.open(name, "rb") { |f| read_from_stream(f) }
|
||||
elsif (create)
|
||||
@entrySet = ZipEntrySet.new
|
||||
else
|
||||
raise ZipError, "File #{fileName} not found"
|
||||
case
|
||||
when ::File.exists?(fileName) && !buffer
|
||||
::File.open(name, "rb") do |f|
|
||||
read_from_stream(f)
|
||||
end
|
||||
when create
|
||||
@entrySet = ZipEntrySet.new
|
||||
else
|
||||
raise ZipError, "File #{fileName} not found"
|
||||
end
|
||||
@create = create
|
||||
@storedEntries = @entrySet.dup
|
||||
|
||||
@restore_ownership = false
|
||||
@restore_permissions = false
|
||||
@restore_times = true
|
||||
|
|
|
@ -74,7 +74,7 @@ module Zip
|
|||
update_local_headers
|
||||
write_central_directory
|
||||
@closed = true
|
||||
return @outputStream
|
||||
@outputStream
|
||||
end
|
||||
|
||||
# Closes the current entry and opens a new for writing.
|
||||
|
@ -110,11 +110,11 @@ module Zip
|
|||
end
|
||||
|
||||
private
|
||||
|
||||
def finalize_current_entry
|
||||
return unless @currentEntry
|
||||
finish
|
||||
@currentEntry.compressed_size = @outputStream.tell - @currentEntry.localHeaderOffset -
|
||||
@currentEntry.calculate_local_header_size
|
||||
@currentEntry.compressed_size = @outputStream.tell - @currentEntry.localHeaderOffset - @currentEntry.calculate_local_header_size
|
||||
@currentEntry.size = @compressor.size
|
||||
@currentEntry.crc = @compressor.crc
|
||||
@currentEntry = nil
|
||||
|
@ -138,12 +138,11 @@ module Zip
|
|||
end
|
||||
|
||||
def update_local_headers
|
||||
pos = @outputStream.tell
|
||||
@entrySet.each {
|
||||
|entry|
|
||||
pos = @outputStream.pos
|
||||
@entrySet.each do |entry|
|
||||
@outputStream.pos = entry.localHeaderOffset
|
||||
entry.write_local_entry(@outputStream)
|
||||
}
|
||||
end
|
||||
@outputStream.pos = pos
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue