Class: Zip::Entry
- Inherits:
-
Object
- Object
- Zip::Entry
- Defined in:
- lib/zip/entry.rb
Direct Known Subclasses
Constant Summary collapse
- STORED =
0
- DEFLATED =
8
- EFS =
Language encoding flag (EFS) bit
0b100000000000
Instance Attribute Summary collapse
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#compressed_size ⇒ Object
Returns the value of attribute compressed_size.
-
#compression_method ⇒ Object
Returns the value of attribute compression_method.
-
#crc ⇒ Object
Returns the value of attribute crc.
-
#dirty ⇒ Object
Returns the value of attribute dirty.
-
#external_file_attributes ⇒ Object
Returns the value of attribute external_file_attributes.
-
#extra ⇒ Object
Returns the value of attribute extra.
-
#filepath ⇒ Object
readonly
:nodoc:.
-
#follow_symlinks ⇒ Object
Returns the value of attribute follow_symlinks.
-
#fstype ⇒ Object
Returns the value of attribute fstype.
-
#ftype ⇒ Object
readonly
:nodoc:.
-
#gp_flags ⇒ Object
Returns the value of attribute gp_flags.
-
#header_signature ⇒ Object
Returns the value of attribute header_signature.
-
#internal_file_attributes ⇒ Object
Returns the value of attribute internal_file_attributes.
-
#local_header_offset ⇒ Object
Returns the value of attribute local_header_offset.
-
#name ⇒ Object
Returns the value of attribute name.
-
#restore_ownership ⇒ Object
Returns the value of attribute restore_ownership.
-
#restore_permissions ⇒ Object
Returns the value of attribute restore_permissions.
-
#restore_times ⇒ Object
Returns the value of attribute restore_times.
-
#size ⇒ Object
Returns the value of attribute size.
-
#unix_gid ⇒ Object
Returns the value of attribute unix_gid.
-
#unix_perms ⇒ Object
Returns the value of attribute unix_perms.
-
#unix_uid ⇒ Object
Returns the value of attribute unix_uid.
-
#zipfile ⇒ Object
Returns the value of attribute zipfile.
Class Method Summary collapse
-
.read_c_dir_entry(io) ⇒ Object
:nodoc:all.
- .read_local_entry(io) ⇒ Object
-
.read_zip_64_long(io) ⇒ Object
:nodoc:.
-
.read_zip_long(io) ⇒ Object
:nodoc:.
-
.read_zip_short(io) ⇒ Object
:nodoc:.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#calculate_local_header_size ⇒ Object
:nodoc:all.
-
#cdir_header_size ⇒ Object
:nodoc:all.
- #check_c_dir_entry_comment_size ⇒ Object
- #check_c_dir_entry_signature ⇒ Object
- #check_c_dir_entry_static_header_length(buf) ⇒ Object
- #check_name(name) ⇒ Object
- #clean_up ⇒ Object
- #comment_size ⇒ Object
-
#encrypted? ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
- #extra_size ⇒ Object
-
#extract(dest_path = nil, &block) ⇒ Object
Extracts entry to file dest_path (defaults to @name).
-
#extract_v3(entry_path = @name, destination_directory: '.', &block) ⇒ Object
Extracts this entry to a file at ‘entry_path`, with `destination_directory` as the base location in the filesystem.
-
#file_stat(path) ⇒ Object
:nodoc:.
- #file_type_is?(type) ⇒ Boolean
-
#gather_fileinfo_from_srcpath(src_path) ⇒ Object
:nodoc:.
-
#get_extra_attributes_from_path(path) ⇒ Object
:nodoc:.
-
#get_input_stream(&block) ⇒ Object
Returns an IO like object for the given ZipEntry.
- #get_raw_input_stream(&block) ⇒ Object
- #incomplete? ⇒ Boolean
-
#initialize(zipfile = nil, name = nil, *args) ⇒ Entry
constructor
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity.
-
#local_entry_offset ⇒ Object
:nodoc:all.
-
#name_is_directory? ⇒ Boolean
:nodoc:all.
-
#name_safe? ⇒ Boolean
Is the name a relative path, free of ‘..` patterns that could lead to path traversal attacks? This does NOT handle symlinks; if the path contains symlinks, this check is NOT enough to guarantee safety.
- #name_size ⇒ Object
-
#next_header_offset ⇒ Object
:nodoc:all.
- #pack_c_dir_entry ⇒ Object
- #pack_local_entry ⇒ Object
- #parent_as_string ⇒ Object
-
#read_c_dir_entry(io) ⇒ Object
:nodoc:all.
- #read_c_dir_extra_field(io) ⇒ Object
-
#read_local_entry(io) ⇒ Object
:nodoc:all.
- #set_default_vars_values ⇒ Object
-
#set_extra_attributes_on_path(dest_path) ⇒ Object
:nodoc:.
- #set_ftype_from_c_dir_entry ⇒ Object
- #set_unix_attributes_on_path(dest_path) ⇒ Object
- #time ⇒ Object (also: #mtime)
- #time=(value) ⇒ Object
- #to_s ⇒ Object
- #unpack_c_dir_entry(buf) ⇒ Object
- #unpack_local_entry(buf) ⇒ Object
-
#verify_local_header_size! ⇒ Object
check before rewriting an entry (after file sizes are known) that we didn’t change the header size (and thus clobber file data or something).
-
#write_c_dir_entry(io) ⇒ Object
:nodoc:all.
-
#write_local_entry(io, rewrite = false) ⇒ Object
:nodoc:all.
-
#write_to_zip_output_stream(zip_output_stream) ⇒ Object
:nodoc:all.
Constructor Details
#initialize(zipfile = nil, name = nil, *args) ⇒ Entry
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/zip/entry.rb', line 56 def initialize(zipfile = nil, name = nil, *args) name ||= '' check_name(name) set_default_vars_values @fstype = ::Zip::RUNNING_ON_WINDOWS ? ::Zip::FSTYPE_FAT : ::Zip::FSTYPE_UNIX @zipfile = zipfile || '' @name = name if (args_hash = args.first).kind_of?(::Hash) @comment = args_hash[:comment] || '' @extra = args_hash[:extra] || '' @compressed_size = args_hash[:compressed_size] || 0 @crc = args_hash[:crc] || 0 @compression_method = args_hash[:compression_method] || ::Zip::Entry::DEFLATED @size = args_hash[:size] || 0 @time = args_hash[:time] || ::Zip::DOSTime.now else Zip.warn_about_v3_api('Zip::Entry.new') unless args.empty? @comment = args[0] || '' @extra = args[1] || '' @compressed_size = args[2] || 0 @crc = args[3] || 0 @compression_method = args[4] || ::Zip::Entry::DEFLATED @size = args[5] || 0 @time = args[6] || ::Zip::DOSTime.now end @ftype = name_is_directory? ? :directory : :file @extra = ::Zip::ExtraField.new(@extra.to_s) unless @extra.kind_of?(::Zip::ExtraField) end |
Instance Attribute Details
#comment ⇒ Object
Returns the value of attribute comment.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def comment @comment end |
#compressed_size ⇒ Object
Returns the value of attribute compressed_size.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def compressed_size @compressed_size end |
#compression_method ⇒ Object
Returns the value of attribute compression_method.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def compression_method @compression_method end |
#crc ⇒ Object
Returns the value of attribute crc.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def crc @crc end |
#dirty ⇒ Object
Returns the value of attribute dirty.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def dirty @dirty end |
#external_file_attributes ⇒ Object
Returns the value of attribute external_file_attributes.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def external_file_attributes @external_file_attributes end |
#extra ⇒ Object
Returns the value of attribute extra.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def extra @extra end |
#filepath ⇒ Object (readonly)
:nodoc:
16 17 18 |
# File 'lib/zip/entry.rb', line 16 def filepath @filepath end |
#follow_symlinks ⇒ Object
Returns the value of attribute follow_symlinks.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def follow_symlinks @follow_symlinks end |
#fstype ⇒ Object
Returns the value of attribute fstype.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def fstype @fstype end |
#ftype ⇒ Object (readonly)
:nodoc:
16 17 18 |
# File 'lib/zip/entry.rb', line 16 def ftype @ftype end |
#gp_flags ⇒ Object
Returns the value of attribute gp_flags.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def gp_flags @gp_flags end |
#header_signature ⇒ Object
Returns the value of attribute header_signature.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def header_signature @header_signature end |
#internal_file_attributes ⇒ Object
Returns the value of attribute internal_file_attributes.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def internal_file_attributes @internal_file_attributes end |
#local_header_offset ⇒ Object
Returns the value of attribute local_header_offset.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def local_header_offset @local_header_offset end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def name @name end |
#restore_ownership ⇒ Object
Returns the value of attribute restore_ownership.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def restore_ownership @restore_ownership end |
#restore_permissions ⇒ Object
Returns the value of attribute restore_permissions.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def @restore_permissions end |
#restore_times ⇒ Object
Returns the value of attribute restore_times.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def restore_times @restore_times end |
#size ⇒ Object
Returns the value of attribute size.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def size @size end |
#unix_gid ⇒ Object
Returns the value of attribute unix_gid.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def unix_gid @unix_gid end |
#unix_perms ⇒ Object
Returns the value of attribute unix_perms.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def unix_perms @unix_perms end |
#unix_uid ⇒ Object
Returns the value of attribute unix_uid.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def unix_uid @unix_uid end |
#zipfile ⇒ Object
Returns the value of attribute zipfile.
9 10 11 |
# File 'lib/zip/entry.rb', line 9 def zipfile @zipfile end |
Class Method Details
.read_c_dir_entry(io) ⇒ Object
:nodoc:all
247 248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/zip/entry.rb', line 247 def read_c_dir_entry(io) #:nodoc:all path = if io.respond_to?(:path) io.path else io end entry = new(path) entry.read_c_dir_entry(io) entry rescue Error nil end |
.read_local_entry(io) ⇒ Object
260 261 262 263 264 265 266 |
# File 'lib/zip/entry.rb', line 260 def read_local_entry(io) entry = new(io) entry.read_local_entry(io) entry rescue Error nil end |
.read_zip_64_long(io) ⇒ Object
:nodoc:
243 244 245 |
# File 'lib/zip/entry.rb', line 243 def read_zip_64_long(io) # :nodoc: io.read(8).unpack1('Q<') end |
.read_zip_long(io) ⇒ Object
:nodoc:
239 240 241 |
# File 'lib/zip/entry.rb', line 239 def read_zip_long(io) # :nodoc: io.read(4).unpack1('V') end |
.read_zip_short(io) ⇒ Object
:nodoc:
235 236 237 |
# File 'lib/zip/entry.rb', line 235 def read_zip_short(io) # :nodoc: io.read(2).unpack1('v') end |
Instance Method Details
#<=>(other) ⇒ Object
551 552 553 |
# File 'lib/zip/entry.rb', line 551 def <=>(other) to_s <=> other.to_s end |
#==(other) ⇒ Object
541 542 543 544 545 546 547 548 549 |
# File 'lib/zip/entry.rb', line 541 def ==(other) return false unless other.class == self.class # Compares contents of local entry and exposed fields keys_equal = %w[compression_method crc compressed_size size name extra filepath].all? do |k| other.__send__(k.to_sym) == __send__(k.to_sym) end keys_equal && time == other.time end |
#calculate_local_header_size ⇒ Object
:nodoc:all
166 167 168 |
# File 'lib/zip/entry.rb', line 166 def calculate_local_header_size #:nodoc:all LOCAL_ENTRY_STATIC_HEADER_LENGTH + name_size + extra_size end |
#cdir_header_size ⇒ Object
:nodoc:all
179 180 181 182 |
# File 'lib/zip/entry.rb', line 179 def cdir_header_size #:nodoc:all CDIR_ENTRY_STATIC_HEADER_LENGTH + name_size + (@extra ? @extra.c_dir_size : 0) + comment_size end |
#check_c_dir_entry_comment_size ⇒ Object
415 416 417 418 419 |
# File 'lib/zip/entry.rb', line 415 def check_c_dir_entry_comment_size return if @comment && @comment.bytesize == @comment_length raise ::Zip::Error, 'Truncated cdir zip entry header' end |
#check_c_dir_entry_signature ⇒ Object
409 410 411 412 413 |
# File 'lib/zip/entry.rb', line 409 def check_c_dir_entry_signature return if header_signature == ::Zip::CENTRAL_DIRECTORY_ENTRY_SIGNATURE raise Error, "Zip local header magic not found at location '#{local_header_offset}'" end |
#check_c_dir_entry_static_header_length(buf) ⇒ Object
403 404 405 406 407 |
# File 'lib/zip/entry.rb', line 403 def check_c_dir_entry_static_header_length(buf) return if buf.bytesize == ::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH raise Error, 'Premature end of file. Not enough data for zip cdir entry header' end |
#check_name(name) ⇒ Object
49 50 51 52 53 |
# File 'lib/zip/entry.rb', line 49 def check_name(name) return unless name.start_with?('/') raise ::Zip::EntryNameError, "Illegal ZipEntry name '#{name}', name must not start with /" end |
#clean_up ⇒ Object
642 643 644 |
# File 'lib/zip/entry.rb', line 642 def clean_up # By default, do nothing end |
#comment_size ⇒ Object
162 163 164 |
# File 'lib/zip/entry.rb', line 162 def comment_size @comment ? @comment.bytesize : 0 end |
#encrypted? ⇒ Boolean
rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
91 92 93 |
# File 'lib/zip/entry.rb', line 91 def encrypted? gp_flags & 1 == 1 end |
#extra_size ⇒ Object
158 159 160 |
# File 'lib/zip/entry.rb', line 158 def extra_size @extra ? @extra.local_size : 0 end |
#extract(dest_path = nil, &block) ⇒ Object
Extracts entry to file dest_path (defaults to @name). NB: The caller is responsible for making sure dest_path is safe, if it is passed.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/zip/entry.rb', line 191 def extract(dest_path = nil, &block) Zip.warn_about_v3_api('Zip::Entry#extract') if dest_path.nil? && !name_safe? warn "WARNING: skipped '#{@name}' as unsafe." return self end dest_path ||= @name block ||= proc { ::Zip.on_exists_proc } raise "unknown file type #{inspect}" unless directory? || file? || symlink? __send__("create_#{@ftype}", dest_path, &block) self end |
#extract_v3(entry_path = @name, destination_directory: '.', &block) ⇒ Object
Extracts this entry to a file at ‘entry_path`, with `destination_directory` as the base location in the filesystem.
NB: The caller is responsible for making sure ‘destination_directory` is safe, if it is passed.
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/zip/entry.rb', line 213 def extract_v3(entry_path = @name, destination_directory: '.', &block) dest_dir = ::File.absolute_path(destination_directory || '.') extract_path = ::File.absolute_path(::File.join(dest_dir, entry_path)) unless extract_path.start_with?(dest_dir) warn "WARNING: skipped extracting '#{@name}' to '#{extract_path}' as unsafe." return self end block ||= proc { ::Zip.on_exists_proc } raise "unknown file type #{inspect}" unless directory? || file? || symlink? __send__(:"create_#{ftype}", extract_path, &block) self end |
#file_stat(path) ⇒ Object
:nodoc:
446 447 448 449 450 451 452 |
# File 'lib/zip/entry.rb', line 446 def file_stat(path) # :nodoc: if @follow_symlinks ::File.stat(path) else ::File.lstat(path) end end |
#file_type_is?(type) ⇒ Boolean
121 122 123 124 125 |
# File 'lib/zip/entry.rb', line 121 def file_type_is?(type) raise InternalError, "current filetype is unknown: #{inspect}" unless @ftype @ftype == type end |
#gather_fileinfo_from_srcpath(src_path) ⇒ Object
:nodoc:
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 |
# File 'lib/zip/entry.rb', line 589 def gather_fileinfo_from_srcpath(src_path) # :nodoc: stat = file_stat(src_path) @ftype = case stat.ftype when 'file' if name_is_directory? raise ArgumentError, "entry name '#{newEntry}' indicates directory entry, but " \ "'#{src_path}' is not a directory" end :file when 'directory' @name += '/' unless name_is_directory? :directory when 'link' if name_is_directory? raise ArgumentError, "entry name '#{newEntry}' indicates directory entry, but " \ "'#{src_path}' is not a directory" end :symlink else raise "unknown file type: #{src_path.inspect} #{stat.inspect}" end @filepath = src_path get_extra_attributes_from_path(@filepath) end |
#get_extra_attributes_from_path(path) ⇒ Object
:nodoc:
454 455 456 457 458 459 460 461 462 |
# File 'lib/zip/entry.rb', line 454 def get_extra_attributes_from_path(path) # :nodoc: return if Zip::RUNNING_ON_WINDOWS stat = file_stat(path) @unix_uid = stat.uid @unix_gid = stat.gid @unix_perms = stat.mode & 0o7777 @time = ::Zip::DOSTime.from_time(stat.mtime) end |
#get_input_stream(&block) ⇒ Object
Returns an IO like object for the given ZipEntry. Warning: may behave weird with symlinks.
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/zip/entry.rb', line 557 def get_input_stream(&block) if @ftype == :directory yield ::Zip::NullInputStream if block_given? ::Zip::NullInputStream elsif @filepath case @ftype when :file ::File.open(@filepath, 'rb', &block) when :symlink linkpath = ::File.readlink(@filepath) stringio = ::StringIO.new(linkpath) yield(stringio) if block_given? stringio else raise "unknown @file_type #{@ftype}" end else zis = ::Zip::InputStream.new(@zipfile, offset: local_header_offset) zis.instance_variable_set(:@complete_entry, self) zis.get_next_entry if block_given? begin yield(zis) ensure zis.close end else zis end end end |
#get_raw_input_stream(&block) ⇒ Object
634 635 636 637 638 639 640 |
# File 'lib/zip/entry.rb', line 634 def get_raw_input_stream(&block) if @zipfile.respond_to?(:seek) && @zipfile.respond_to?(:read) yield @zipfile else ::File.open(@zipfile, 'rb', &block) end end |
#incomplete? ⇒ Boolean
95 96 97 |
# File 'lib/zip/entry.rb', line 95 def incomplete? gp_flags & 8 == 8 end |
#local_entry_offset ⇒ Object
:nodoc:all
150 151 152 |
# File 'lib/zip/entry.rb', line 150 def local_entry_offset #:nodoc:all local_header_offset + @local_header_size end |
#name_is_directory? ⇒ Boolean
:nodoc:all
134 135 136 |
# File 'lib/zip/entry.rb', line 134 def name_is_directory? #:nodoc:all @name.end_with?('/') end |
#name_safe? ⇒ Boolean
Is the name a relative path, free of ‘..` patterns that could lead to path traversal attacks? This does NOT handle symlinks; if the path contains symlinks, this check is NOT enough to guarantee safety.
141 142 143 144 145 146 147 148 |
# File 'lib/zip/entry.rb', line 141 def name_safe? cleanpath = Pathname.new(@name).cleanpath return false unless cleanpath.relative? root = ::File::SEPARATOR = ::File.join(root, cleanpath.to_s) ::File.absolute_path(cleanpath.to_s, root) == end |
#name_size ⇒ Object
154 155 156 |
# File 'lib/zip/entry.rb', line 154 def name_size @name ? @name.bytesize : 0 end |
#next_header_offset ⇒ Object
:nodoc:all
184 185 186 |
# File 'lib/zip/entry.rb', line 184 def next_header_offset #:nodoc:all local_entry_offset + compressed_size + data_descriptor_size end |
#pack_c_dir_entry ⇒ Object
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/zip/entry.rb', line 486 def pack_c_dir_entry zip64 = @extra['Zip64'] [ @header_signature, @version, # version of encoding software @fstype, # filesystem type @version_needed_to_extract, # @versionNeededToExtract @gp_flags, # @gp_flags @compression_method, @time.to_binary_dos_time, # @last_mod_time @time.to_binary_dos_date, # @last_mod_date @crc, zip64 && zip64.compressed_size ? 0xFFFFFFFF : @compressed_size, zip64 && zip64.original_size ? 0xFFFFFFFF : @size, name_size, @extra ? @extra.c_dir_size : 0, comment_size, zip64 && zip64.disk_start_number ? 0xFFFF : 0, # disk number start @internal_file_attributes, # file type (binary=0, text=1) @external_file_attributes, # native filesystem attributes zip64 && zip64.relative_header_offset ? 0xFFFFFFFF : @local_header_offset, @name, @extra, @comment ].pack('VCCvvvvvVVVvvvvvVV') end |
#pack_local_entry ⇒ Object
323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/zip/entry.rb', line 323 def pack_local_entry zip64 = @extra['Zip64'] [::Zip::LOCAL_ENTRY_SIGNATURE, @version_needed_to_extract, # version needed to extract @gp_flags, # @gp_flags @compression_method, @time.to_binary_dos_time, # @last_mod_time @time.to_binary_dos_date, # @last_mod_date @crc, zip64 && zip64.compressed_size ? 0xFFFFFFFF : @compressed_size, zip64 && zip64.original_size ? 0xFFFFFFFF : @size, name_size, @extra ? @extra.local_size : 0].pack('VvvvvvVVVvv') end |
#parent_as_string ⇒ Object
628 629 630 631 632 |
# File 'lib/zip/entry.rb', line 628 def parent_as_string entry_name = name.chomp('/') slash_index = entry_name.rindex('/') slash_index ? entry_name.slice(0, slash_index + 1) : nil end |
#read_c_dir_entry(io) ⇒ Object
:nodoc:all
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 |
# File 'lib/zip/entry.rb', line 429 def read_c_dir_entry(io) #:nodoc:all static_sized_fields_buf = io.read(::Zip::CDIR_ENTRY_STATIC_HEADER_LENGTH) check_c_dir_entry_static_header_length(static_sized_fields_buf) unpack_c_dir_entry(static_sized_fields_buf) check_c_dir_entry_signature set_time(@last_mod_date, @last_mod_time) @name = io.read(@name_length) if ::Zip.force_entry_names_encoding @name.force_encoding(::Zip.force_entry_names_encoding) end read_c_dir_extra_field(io) @comment = io.read(@comment_length) check_c_dir_entry_comment_size set_ftype_from_c_dir_entry parse_zip64_extra(false) end |
#read_c_dir_extra_field(io) ⇒ Object
421 422 423 424 425 426 427 |
# File 'lib/zip/entry.rb', line 421 def read_c_dir_extra_field(io) if @extra.kind_of?(::Zip::ExtraField) @extra.merge(io.read(@extra_length)) else @extra = ::Zip::ExtraField.new(io.read(@extra_length)) end end |
#read_local_entry(io) ⇒ Object
:nodoc:all
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
# File 'lib/zip/entry.rb', line 284 def read_local_entry(io) #:nodoc:all @local_header_offset = io.tell static_sized_fields_buf = io.read(::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH) || '' unless static_sized_fields_buf.bytesize == ::Zip::LOCAL_ENTRY_STATIC_HEADER_LENGTH raise Error, 'Premature end of file. Not enough data for zip entry local header' end unpack_local_entry(static_sized_fields_buf) unless @header_signature == ::Zip::LOCAL_ENTRY_SIGNATURE raise ::Zip::Error, "Zip local header magic not found at location '#{local_header_offset}'" end set_time(@last_mod_date, @last_mod_time) @name = io.read(@name_length) extra = io.read(@extra_length) @name.tr!('\\', '/') if ::Zip.force_entry_names_encoding @name.force_encoding(::Zip.force_entry_names_encoding) end if extra && extra.bytesize != @extra_length raise ::Zip::Error, 'Truncated local zip entry header' end if @extra.kind_of?(::Zip::ExtraField) @extra.merge(extra) if extra else @extra = ::Zip::ExtraField.new(extra) end parse_zip64_extra(true) @local_header_size = calculate_local_header_size end |
#set_default_vars_values ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/zip/entry.rb', line 18 def set_default_vars_values @local_header_offset = 0 @local_header_size = nil # not known until local entry is created or read @internal_file_attributes = 1 @external_file_attributes = 0 @header_signature = ::Zip::CENTRAL_DIRECTORY_ENTRY_SIGNATURE @version_needed_to_extract = VERSION_NEEDED_TO_EXTRACT @version = VERSION_MADE_BY @ftype = nil # unspecified or unknown @filepath = nil @gp_flags = 0 if ::Zip.unicode_names @gp_flags |= EFS @version = 63 end @follow_symlinks = false @restore_times = false @restore_permissions = false @restore_ownership = false # BUG: need an extra field to support uid/gid's @unix_uid = nil @unix_gid = nil @unix_perms = nil # @posix_acl = nil # @ntfs_acl = nil @dirty = false end |
#set_extra_attributes_on_path(dest_path) ⇒ Object
:nodoc:
477 478 479 480 481 482 483 484 |
# File 'lib/zip/entry.rb', line 477 def set_extra_attributes_on_path(dest_path) # :nodoc: return unless file? || directory? case @fstype when ::Zip::FSTYPE_UNIX set_unix_attributes_on_path(dest_path) end end |
#set_ftype_from_c_dir_entry ⇒ Object
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 |
# File 'lib/zip/entry.rb', line 374 def set_ftype_from_c_dir_entry @ftype = case @fstype when ::Zip::FSTYPE_UNIX @unix_perms = (@external_file_attributes >> 16) & 0o7777 case (@external_file_attributes >> 28) when ::Zip::FILE_TYPE_DIR :directory when ::Zip::FILE_TYPE_FILE :file when ::Zip::FILE_TYPE_SYMLINK :symlink else # best case guess for whether it is a file or not # Otherwise this would be set to unknown and that entry would never be able to extracted if name_is_directory? :directory else :file end end else if name_is_directory? :directory else :file end end end |
#set_unix_attributes_on_path(dest_path) ⇒ Object
464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/zip/entry.rb', line 464 def set_unix_attributes_on_path(dest_path) # ignore setuid/setgid bits by default. honor if @restore_ownership unix_perms_mask = 0o1777 unix_perms_mask = 0o7777 if @restore_ownership ::FileUtils.chmod(@unix_perms & unix_perms_mask, dest_path) if @restore_permissions && @unix_perms ::FileUtils.chown(@unix_uid, @unix_gid, dest_path) if @restore_ownership && @unix_uid && @unix_gid && ::Process.egid == 0 # Restore the timestamp on a file. This will either have come from the # original source file that was copied into the archive, or from the # creation date of the archive if there was no original source file. ::FileUtils.touch(dest_path, mtime: time) if @restore_times end |
#time ⇒ Object Also known as: mtime
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/zip/entry.rb', line 99 def time if @extra['UniversalTime'] @extra['UniversalTime'].mtime elsif @extra['NTFS'] @extra['NTFS'].mtime else # Standard time field in central directory has local time # under archive creator. Then, we can't get timezone. @time end end |
#time=(value) ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/zip/entry.rb', line 113 def time=(value) unless @extra.member?('UniversalTime') || @extra.member?('NTFS') @extra.create('UniversalTime') end (@extra['UniversalTime'] || @extra['NTFS']).mtime = value @time = value end |
#to_s ⇒ Object
230 231 232 |
# File 'lib/zip/entry.rb', line 230 def to_s @name end |
#unpack_c_dir_entry(buf) ⇒ Object
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/zip/entry.rb', line 350 def unpack_c_dir_entry(buf) @header_signature, @version, # version of encoding software @fstype, # filesystem type @version_needed_to_extract, @gp_flags, @compression_method, @last_mod_time, @last_mod_date, @crc, @compressed_size, @size, @name_length, @extra_length, @comment_length, _, # diskNumberStart @internal_file_attributes, @external_file_attributes, @local_header_offset, @name, @extra, @comment = buf.unpack('VCCvvvvvVVVvvvvvVV') end |
#unpack_local_entry(buf) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
# File 'lib/zip/entry.rb', line 269 def unpack_local_entry(buf) @header_signature, @version, @fstype, @gp_flags, @compression_method, @last_mod_time, @last_mod_date, @crc, @compressed_size, @size, @name_length, @extra_length = buf.unpack('VCCvvvvVVVvv') end |
#verify_local_header_size! ⇒ Object
check before rewriting an entry (after file sizes are known) that we didn’t change the header size (and thus clobber file data or something)
172 173 174 175 176 177 |
# File 'lib/zip/entry.rb', line 172 def verify_local_header_size! return if @local_header_size.nil? new_size = calculate_local_header_size raise Error, "local header size changed (#{@local_header_size} -> #{new_size})" if @local_header_size != new_size end |
#write_c_dir_entry(io) ⇒ Object
:nodoc:all
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 |
# File 'lib/zip/entry.rb', line 513 def write_c_dir_entry(io) #:nodoc:all prep_zip64_extra(false) case @fstype when ::Zip::FSTYPE_UNIX ft = case @ftype when :file @unix_perms ||= 0o644 ::Zip::FILE_TYPE_FILE when :directory @unix_perms ||= 0o755 ::Zip::FILE_TYPE_DIR when :symlink @unix_perms ||= 0o755 ::Zip::FILE_TYPE_SYMLINK end unless ft.nil? @external_file_attributes = (ft << 12 | (@unix_perms & 0o7777)) << 16 end end io << pack_c_dir_entry io << @name io << (@extra ? @extra.to_c_dir_bin : '') io << @comment end |
#write_local_entry(io, rewrite = false) ⇒ Object
:nodoc:all
338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/zip/entry.rb', line 338 def write_local_entry(io, rewrite = false) #:nodoc:all prep_zip64_extra(true) verify_local_header_size! if rewrite @local_header_offset = io.tell io << pack_local_entry io << @name io << @extra.to_local_bin if @extra @local_header_size = io.tell - @local_header_offset end |
#write_to_zip_output_stream(zip_output_stream) ⇒ Object
:nodoc:all
617 618 619 620 621 622 623 624 625 626 |
# File 'lib/zip/entry.rb', line 617 def write_to_zip_output_stream(zip_output_stream) #:nodoc:all if @ftype == :directory zip_output_stream.put_next_entry(self, nil, nil, ::Zip::Entry::STORED) elsif @filepath zip_output_stream.put_next_entry(self, nil, nil, compression_method || ::Zip::Entry::DEFLATED) get_input_stream { |is| ::Zip::IOExtras.copy_stream(zip_output_stream, is) } else zip_output_stream.copy_raw_entry(self) end end |