Allocate less Arrays and Strings when writing a zip

This commit is contained in:
Sam Rawlins 2014-02-03 12:15:08 -08:00
parent ec81c30382
commit a68a36c759
3 changed files with 10 additions and 8 deletions

View File

@ -77,7 +77,7 @@ module Zip
private private
def to_key(entry) def to_key(entry)
entry.to_s.sub(/\/$/, '') entry.to_s.chomp('/')
end end
end end
end end

View File

@ -60,17 +60,19 @@ module Zip
# place Unknown last, so "extra" data that is missing the proper signature/size # place Unknown last, so "extra" data that is missing the proper signature/size
# does not prevent known fields from being read back in # does not prevent known fields from being read back in
def ordered_values def ordered_values
self.keys.sort_by { |k| k == 'Unknown' ? 1 : 0 }.map { |k| self[k] } result = []
self.each { |k,v| k == 'Unknown' ? result.push(v) : result.unshift(v) }
result
end end
def to_local_bin def to_local_bin
ordered_values.map { |v| v.to_local_bin.force_encoding('BINARY') }.join ordered_values.map! { |v| v.to_local_bin.force_encoding('BINARY') }.join
end end
alias :to_s :to_local_bin alias :to_s :to_local_bin
def to_c_dir_bin def to_c_dir_bin
ordered_values.map { |v| v.to_c_dir_bin.force_encoding('BINARY') }.join ordered_values.map! { |v| v.to_c_dir_bin.force_encoding('BINARY') }.join
end end
def c_dir_size def c_dir_size

View File

@ -7,7 +7,7 @@ module Zip
end end
def self.name def self.name
self.to_s.split("::")[-1] @name ||= self.to_s.split("::")[-1]
end end
# return field [size, content] or false # return field [size, content] or false
@ -32,12 +32,12 @@ module Zip
def to_local_bin def to_local_bin
s = pack_for_local s = pack_for_local
self.class.const_get(:HEADER_ID) + [s.bytesize].pack("v") + s self.class.const_get(:HEADER_ID) + [s.bytesize].pack("v") << s
end end
def to_c_dir_bin def to_c_dir_bin
s = pack_for_c_dir s = pack_for_c_dir
self.class.const_get(:HEADER_ID) + [s.bytesize].pack("v") + s self.class.const_get(:HEADER_ID) + [s.bytesize].pack("v") << s
end end
end end
end end