Class: Zip::Entry
Overview
Zip::Entry represents an entry in a Zip archive.
Direct Known Subclasses
Constant Summary collapse
- STORED =
Constant used to specify that the entry is stored (i.e., not compressed).
::Zip::COMPRESSION_METHOD_STORE
- DEFLATED =
Constant used to specify that the entry is deflated (i.e., compressed).
::Zip::COMPRESSION_METHOD_DEFLATE
- EFS =
Language encoding flag (EFS) bit
0b100000000000- COMPRESSION_LEVEL_SUPERFAST_GPFLAG =
Compression level flags (used as part of the gp flags).
0b110- COMPRESSION_LEVEL_FAST_GPFLAG =
:nodoc:
0b100- COMPRESSION_LEVEL_MAX_GPFLAG =
:nodoc:
0b010
Instance Attribute Summary collapse
-
#comment ⇒ Object
:nodoc:.
-
#compressed_size ⇒ Object
:nodoc:.
-
#compression_level ⇒ Object
readonly
:nodoc:.
-
#crc ⇒ Object
Returns the value of attribute crc.
-
#external_file_attributes ⇒ Object
Returns the value of attribute external_file_attributes.
-
#extra ⇒ Object
readonly
:nodoc:.
-
#filepath ⇒ Object
readonly
:nodoc:.
-
#follow_symlinks ⇒ Object
:nodoc:.
-
#fstype ⇒ Object
Returns the value of attribute fstype.
-
#gp_flags ⇒ Object
Returns the value of attribute gp_flags.
-
#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
:nodoc:.
-
#restore_ownership ⇒ Object
:nodoc:.
-
#restore_permissions ⇒ Object
:nodoc:.
-
#restore_times ⇒ Object
:nodoc:.
-
#size ⇒ Object
The uncompressed size of the entry.
-
#unix_gid ⇒ Object
:nodoc:.
-
#unix_perms ⇒ Object
:nodoc:.
-
#unix_uid ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.read_c_dir_entry(io) ⇒ Object
:nodoc:.
-
.read_local_entry(io) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
:nodoc:.
-
#==(other) ⇒ Object
:nodoc:.
-
#absolute_time? ⇒ Boolean
Does this entry return time fields with accurate timezone information?.
-
#aes? ⇒ Boolean
Is this entry encrypted with AES encryption?.
-
#atime ⇒ Object
Get the last access time of this entry, if available.
-
#atime=(value) ⇒ Object
Set the last access time of this entry.
-
#calculate_local_header_size ⇒ Object
:nodoc:.
-
#cdir_header_size ⇒ Object
:nodoc:.
-
#check_c_dir_entry_comment_size ⇒ Object
:nodoc:.
-
#check_c_dir_entry_signature ⇒ Object
:nodoc:.
-
#check_c_dir_entry_static_header_length(buf) ⇒ Object
:nodoc:.
-
#check_name(name) ⇒ Object
:nodoc:.
-
#clean_up ⇒ Object
:nodoc:.
-
#comment_size ⇒ Object
:nodoc:.
-
#compression_method ⇒ Object
Return the compression method for this entry.
-
#compression_method=(method) ⇒ Object
Set the compression method for this entry.
-
#ctime ⇒ Object
Get the creation time of this entry, if available.
-
#ctime=(value) ⇒ Object
Set the creation time of this entry.
-
#encrypted? ⇒ Boolean
Is this entry encrypted?.
-
#extra_size ⇒ Object
:nodoc:.
-
#extract(entry_path = @name, destination_directory: '.', create_parent_directories: false, &block) ⇒ Object
Extracts this entry to a file at
entry_path, withdestination_directoryas the base location in the filesystem. -
#file_stat(path) ⇒ Object
:nodoc:.
-
#file_type_is?(type) ⇒ Boolean
:nodoc:.
-
#ftype ⇒ Object
:nodoc:.
-
#gather_fileinfo_from_srcpath(src_path) ⇒ Object
:nodoc:.
-
#get_extra_attributes_from_path(path) ⇒ Object
:nodoc:.
-
#get_input_stream(decrypter: nil, &block) ⇒ Object
Returns an IO like object for the given ZipEntry.
-
#get_raw_input_stream(&block) ⇒ Object
:nodoc:.
-
#incomplete? ⇒ Boolean
:nodoc:.
-
#initialize(zipfile = '', name = '', comment: '', size: nil, compressed_size: 0, crc: 0, compression_method: DEFLATED, compression_level: ::Zip.default_compression, time: ::Zip::DOSTime.now, extra: ::Zip::ExtraField.new) ⇒ Entry
constructor
Create a new Zip::Entry.
-
#local_entry_offset ⇒ Object
:nodoc:.
-
#name_is_directory? ⇒ Boolean
:nodoc:.
-
#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
:nodoc:.
-
#next_header_offset ⇒ Object
:nodoc:.
-
#pack_c_dir_entry ⇒ Object
:nodoc:.
-
#pack_local_entry ⇒ Object
:nodoc:.
-
#parent_as_string ⇒ Object
:nodoc:.
-
#prep_aes_extra(vendor_version, strength) ⇒ Object
Called by
AESEncrypter#prepare_entrybefore the local header is first written, so the extra field is in place before its size is measured. -
#read_c_dir_entry(io) ⇒ Object
:nodoc:.
-
#read_extra_field(buf, local: false) ⇒ Object
:nodoc:.
-
#read_local_entry(io) ⇒ Object
:nodoc:.
-
#set_default_vars_values ⇒ Object
:nodoc:.
-
#set_extra_attributes_on_path(dest_path) ⇒ Object
rubocop:enable Style/GuardClause.
-
#set_ftype_from_c_dir_entry ⇒ Object
:nodoc:.
-
#set_unix_attributes_on_path(dest_path) ⇒ Object
rubocop:disable Style/GuardClause.
-
#time(component: :mtime) ⇒ Object
(also: #mtime)
Get a timestamp component of this entry.
-
#time=(value, component: :mtime) ⇒ Object
(also: #mtime=)
Set a timestamp component of this entry.
-
#to_s ⇒ Object
:nodoc:.
-
#unpack_c_dir_entry(buf) ⇒ Object
:nodoc:.
-
#unpack_local_entry(buf) ⇒ Object
:nodoc:.
-
#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, suppress_extra_fields: false) ⇒ Object
:nodoc:.
-
#write_local_entry(io, suppress_extra_fields: false, rewrite: false) ⇒ Object
:nodoc:.
-
#write_to_zip_output_stream(zip_output_stream) ⇒ Object
:nodoc:.
-
#zip64? ⇒ Boolean
Does this entry use the ZIP64 extensions?.
Methods included from Dirtyable
Constructor Details
#initialize(zipfile = '', name = '', comment: '', size: nil, compressed_size: 0, crc: 0, compression_method: DEFLATED, compression_level: ::Zip.default_compression, time: ::Zip::DOSTime.now, extra: ::Zip::ExtraField.new) ⇒ Entry
Create a new Zip::Entry.
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/zip/entry.rb', line 76 def initialize( zipfile = '', name = '', comment: '', size: nil, compressed_size: 0, crc: 0, compression_method: DEFLATED, compression_level: ::Zip.default_compression, time: ::Zip::DOSTime.now, extra: ::Zip::ExtraField.new ) super() @name = name check_name(@name) set_default_vars_values @fstype = ::Zip::RUNNING_ON_WINDOWS ? ::Zip::FSTYPE_FAT : ::Zip::FSTYPE_UNIX @zipfile = zipfile @comment = comment || '' @compression_method = compression_method || DEFLATED @compression_level = compression_level || ::Zip.default_compression @compressed_size = compressed_size || 0 @crc = crc || 0 @size = size @time = case time when ::Zip::DOSTime time when Time ::Zip::DOSTime.from_time(time) else ::Zip::DOSTime.now end @extra = extra.kind_of?(ExtraField) ? extra : ExtraField.new(extra.to_s) set_compression_level_flags end |
Instance Attribute Details
#comment ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def comment @comment end |
#compressed_size ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def compressed_size @compressed_size end |
#compression_level ⇒ Object (readonly)
:nodoc:
34 35 36 |
# File 'lib/zip/entry.rb', line 34 def compression_level @compression_level end |
#crc ⇒ Object
Returns the value of attribute crc.
31 32 33 |
# File 'lib/zip/entry.rb', line 31 def crc @crc end |
#external_file_attributes ⇒ Object
Returns the value of attribute external_file_attributes.
31 32 33 |
# File 'lib/zip/entry.rb', line 31 def external_file_attributes @external_file_attributes end |
#extra ⇒ Object (readonly)
:nodoc:
34 35 36 |
# File 'lib/zip/entry.rb', line 34 def extra @extra end |
#filepath ⇒ Object (readonly)
:nodoc:
34 35 36 |
# File 'lib/zip/entry.rb', line 34 def filepath @filepath end |
#follow_symlinks ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def follow_symlinks @follow_symlinks end |
#fstype ⇒ Object
Returns the value of attribute fstype.
31 32 33 |
# File 'lib/zip/entry.rb', line 31 def fstype @fstype end |
#gp_flags ⇒ Object
Returns the value of attribute gp_flags.
31 32 33 |
# File 'lib/zip/entry.rb', line 31 def gp_flags @gp_flags end |
#internal_file_attributes ⇒ Object
Returns the value of attribute internal_file_attributes.
31 32 33 |
# File 'lib/zip/entry.rb', line 31 def internal_file_attributes @internal_file_attributes end |
#local_header_offset ⇒ Object
Returns the value of attribute local_header_offset.
31 32 33 |
# File 'lib/zip/entry.rb', line 31 def local_header_offset @local_header_offset end |
#name ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def name @name end |
#restore_ownership ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def restore_ownership @restore_ownership end |
#restore_permissions ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def @restore_permissions end |
#restore_times ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def restore_times @restore_times end |
#size ⇒ Object
The uncompressed size of the entry.
121 122 123 |
# File 'lib/zip/entry.rb', line 121 def size @size || 0 end |
#unix_gid ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def unix_gid @unix_gid end |
#unix_perms ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def unix_perms @unix_perms end |
#unix_uid ⇒ Object
:nodoc:
27 28 29 |
# File 'lib/zip/entry.rb', line 27 def unix_uid @unix_uid end |
Class Method Details
.read_c_dir_entry(io) ⇒ Object
:nodoc:
337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'lib/zip/entry.rb', line 337 def read_c_dir_entry(io) # :nodoc: 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
:nodoc:
350 351 352 353 354 355 356 357 358 |
# File 'lib/zip/entry.rb', line 350 def read_local_entry(io) # :nodoc: entry = new(io) entry.read_local_entry(io) entry rescue SplitArchiveError raise rescue Error nil end |
Instance Method Details
#<=>(other) ⇒ Object
:nodoc:
662 663 664 |
# File 'lib/zip/entry.rb', line 662 def <=>(other) # :nodoc: to_s <=> other.to_s end |
#==(other) ⇒ Object
:nodoc:
653 654 655 656 657 658 659 660 |
# File 'lib/zip/entry.rb', line 653 def ==(other) # :nodoc: return false unless other.class == self.class # Compares contents of local entry and exposed fields %w[compression_method crc compressed_size size name extra filepath time].all? do |k| other.__send__(k.to_sym) == __send__(k.to_sym) end end |
#absolute_time? ⇒ Boolean
Does this entry return time fields with accurate timezone information?
181 182 183 |
# File 'lib/zip/entry.rb', line 181 def absolute_time? @extra.member?(:universaltime) || @extra.member?(:ntfs) end |
#aes? ⇒ Boolean
Is this entry encrypted with AES encryption?
207 208 209 |
# File 'lib/zip/entry.rb', line 207 def aes? !@extra[:aes].nil? end |
#atime ⇒ Object
Get the last access time of this entry, if available.
144 145 146 |
# File 'lib/zip/entry.rb', line 144 def atime time(component: :atime) end |
#atime=(value) ⇒ Object
Set the last access time of this entry.
171 172 173 |
# File 'lib/zip/entry.rb', line 171 def atime=(value) send(:time=, value, component: :atime) end |
#calculate_local_header_size ⇒ Object
:nodoc:
274 275 276 |
# File 'lib/zip/entry.rb', line 274 def calculate_local_header_size # :nodoc: LOCAL_ENTRY_STATIC_HEADER_LENGTH + name_size + extra_size end |
#cdir_header_size ⇒ Object
:nodoc:
290 291 292 293 |
# File 'lib/zip/entry.rb', line 290 def cdir_header_size # :nodoc: CDIR_ENTRY_STATIC_HEADER_LENGTH + name_size + (@extra ? @extra.c_dir_size : 0) + comment_size end |
#check_c_dir_entry_comment_size ⇒ Object
:nodoc:
518 519 520 521 522 |
# File 'lib/zip/entry.rb', line 518 def check_c_dir_entry_comment_size # :nodoc: return if @comment && @comment.bytesize == @comment_length raise ::Zip::Error, 'Truncated cdir zip entry header' end |
#check_c_dir_entry_signature ⇒ Object
:nodoc:
512 513 514 515 516 |
# File 'lib/zip/entry.rb', line 512 def check_c_dir_entry_signature # :nodoc: 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
:nodoc:
506 507 508 509 510 |
# File 'lib/zip/entry.rb', line 506 def check_c_dir_entry_static_header_length(buf) # :nodoc: return unless buf.nil? || 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
:nodoc:
70 71 72 73 |
# File 'lib/zip/entry.rb', line 70 def check_name(name) # :nodoc: raise EntryNameError, name if name.start_with?('/') raise EntryNameError if name.length > 65_535 end |
#clean_up ⇒ Object
:nodoc:
757 758 759 |
# File 'lib/zip/entry.rb', line 757 def clean_up # :nodoc: @dirty = false # Any changes are written at this point. end |
#comment_size ⇒ Object
:nodoc:
270 271 272 |
# File 'lib/zip/entry.rb', line 270 def comment_size # :nodoc: @comment ? @comment.bytesize : 0 end |
#compression_method ⇒ Object
Return the compression method for this entry.
Returns STORED if the entry is a directory or if the compression level is 0.
189 190 191 192 193 |
# File 'lib/zip/entry.rb', line 189 def compression_method return STORED if ftype == :directory || @compression_level == 0 @compression_method end |
#compression_method=(method) ⇒ Object
Set the compression method for this entry.
196 197 198 199 |
# File 'lib/zip/entry.rb', line 196 def compression_method=(method) @dirty = true @compression_method = (ftype == :directory ? STORED : method) end |
#ctime ⇒ Object
Get the creation time of this entry, if available.
149 150 151 |
# File 'lib/zip/entry.rb', line 149 def ctime time(component: :ctime) end |
#ctime=(value) ⇒ Object
Set the creation time of this entry.
176 177 178 |
# File 'lib/zip/entry.rb', line 176 def ctime=(value) send(:time=, value, component: :ctime) end |
#encrypted? ⇒ Boolean
Is this entry encrypted?
112 113 114 |
# File 'lib/zip/entry.rb', line 112 def encrypted? gp_flags & 1 == 1 end |
#extra_size ⇒ Object
:nodoc:
266 267 268 |
# File 'lib/zip/entry.rb', line 266 def extra_size # :nodoc: @extra ? @extra.local_size : 0 end |
#extract(entry_path = @name, destination_directory: '.', create_parent_directories: false, &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.
create_parent_directories, if true, creates any missing intermediate
directories for a file entry before writing it. This defaults to
false: a missing intermediate directory usually means an earlier
entry (e.g. a symlink) was deliberately skipped as unsafe, and letting
a later entry silently succeed by re-creating that path as a plain
directory would mask that. Bulk operations such as extract_all,
which have no such earlier-entry to skip past, opt into this.
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 |
# File 'lib/zip/entry.rb', line 312 def extract(entry_path = @name, destination_directory: '.', create_parent_directories: false, &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 + ::File::SEPARATOR) || dest_dir == ::File::SEPARATOR 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? ::FileUtils.mkdir_p(::File.dirname(extract_path)) if create_parent_directories && file? __send__(:"create_#{ftype}", extract_path, &block) self end |
#file_stat(path) ⇒ Object
:nodoc:
554 555 556 557 558 559 560 |
# File 'lib/zip/entry.rb', line 554 def file_stat(path) # :nodoc: if @follow_symlinks ::File.stat(path) else ::File.lstat(path) end end |
#file_type_is?(type) ⇒ Boolean
:nodoc:
226 227 228 |
# File 'lib/zip/entry.rb', line 226 def file_type_is?(type) # :nodoc: ftype == type end |
#ftype ⇒ Object
:nodoc:
230 231 232 |
# File 'lib/zip/entry.rb', line 230 def ftype # :nodoc: @ftype ||= name_is_directory? ? :directory : :file end |
#gather_fileinfo_from_srcpath(src_path) ⇒ Object
:nodoc:
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 |
# File 'lib/zip/entry.rb', line 701 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 '#{@name}' indicates a 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 '#{@name}' indicates a 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 @size = stat.size get_extra_attributes_from_path(@filepath) end |
#get_extra_attributes_from_path(path) ⇒ Object
:nodoc:
562 563 564 565 566 567 568 569 570 |
# File 'lib/zip/entry.rb', line 562 def get_extra_attributes_from_path(path) # :nodoc: stat = file_stat(path) @time = DOSTime.from_time(stat.mtime) return if ::Zip::RUNNING_ON_WINDOWS @unix_uid = stat.uid @unix_gid = stat.gid @unix_perms = stat.mode & 0o7777 end |
#get_input_stream(decrypter: nil, &block) ⇒ Object
Returns an IO like object for the given ZipEntry. Optional decrypter can be provided for an encrypted entry Warning: may behave weird with symlinks.
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 |
# File 'lib/zip/entry.rb', line 669 def get_input_stream(decrypter: nil, &block) if ftype == :directory yield ::Zip::NullInputStream if block ::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 stringio else raise "unknown @file_type #{ftype}" end else zis = ::Zip::InputStream.new(@zipfile, offset: local_header_offset, decrypter: decrypter) zis.instance_variable_set(:@complete_entry, self) zis.get_next_entry if block begin yield(zis) ensure zis.close end else zis end end end |
#get_raw_input_stream(&block) ⇒ Object
:nodoc:
749 750 751 752 753 754 755 |
# File 'lib/zip/entry.rb', line 749 def get_raw_input_stream(&block) # :nodoc: if @zipfile.respond_to?(:seek) && @zipfile.respond_to?(:read) yield @zipfile else ::File.open(@zipfile, 'rb', &block) end end |
#incomplete? ⇒ Boolean
:nodoc:
116 117 118 |
# File 'lib/zip/entry.rb', line 116 def incomplete? # :nodoc: (gp_flags & 8 == 8) && (crc == 0 || size == 0 || compressed_size == 0) end |
#local_entry_offset ⇒ Object
:nodoc:
258 259 260 |
# File 'lib/zip/entry.rb', line 258 def local_entry_offset # :nodoc: local_header_offset + @local_header_size end |
#name_is_directory? ⇒ Boolean
:nodoc:
241 242 243 |
# File 'lib/zip/entry.rb', line 241 def name_is_directory? # :nodoc: @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.
248 249 250 251 252 253 254 255 256 |
# File 'lib/zip/entry.rb', line 248 def name_safe? # :nodoc: cleanpath = Pathname.new(@name).cleanpath return false unless cleanpath.relative? root = ::File::SEPARATOR naive = Regexp.escape(::File.join(root, cleanpath.to_s)) # Allow for Windows drive mappings at the root. ::File.absolute_path(cleanpath.to_s, root).match?(/([A-Z]:)?#{naive}/i) end |
#name_size ⇒ Object
:nodoc:
262 263 264 |
# File 'lib/zip/entry.rb', line 262 def name_size # :nodoc: @name ? @name.bytesize : 0 end |
#next_header_offset ⇒ Object
:nodoc:
295 296 297 |
# File 'lib/zip/entry.rb', line 295 def next_header_offset # :nodoc: local_entry_offset + compressed_size end |
#pack_c_dir_entry ⇒ Object
:nodoc:
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 |
# File 'lib/zip/entry.rb', line 599 def pack_c_dir_entry # :nodoc: zip64 = @extra[:zip64] [ @header_signature, @version, # version of encoding software @fstype, # filesystem type @version_needed_to_extract, # @versionNeededToExtract @gp_flags, # @gp_flags stored_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 || 0), 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 ].pack('VCCvvvvvVVVvvvvvVV') end |
#pack_local_entry ⇒ Object
:nodoc:
420 421 422 423 424 425 426 427 428 429 430 431 432 433 |
# File 'lib/zip/entry.rb', line 420 def pack_local_entry # :nodoc: zip64 = @extra[:zip64] [::Zip::LOCAL_ENTRY_SIGNATURE, @version_needed_to_extract, # version needed to extract @gp_flags, # @gp_flags stored_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 || 0), name_size, @extra ? @extra.local_size : 0].pack('VvvvvvVVVvv') end |
#parent_as_string ⇒ Object
:nodoc:
743 744 745 746 747 |
# File 'lib/zip/entry.rb', line 743 def parent_as_string # :nodoc: entry_name = name.chomp('/') slash_index = entry_name.rindex('/') slash_index ? entry_name.slice(0, slash_index + 1) : nil end |
#prep_aes_extra(vendor_version, strength) ⇒ Object
Called by AESEncrypter#prepare_entry before the local header is first
written, so the extra field is in place before its size is measured.
Directories have no content to encrypt, so they are left untouched.
214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/zip/entry.rb', line 214 def prep_aes_extra(vendor_version, strength) # :nodoc: return if directory? @version_needed_to_extract = [@version_needed_to_extract, Zip::VERSION_NEEDED_TO_EXTRACT_AES].max aes = @extra[:aes] || @extra.create(:aes) aes.vendor_version = vendor_version aes.vendor_id = 'AE' aes.encryption_strength = strength aes.compression_method = compression_method end |
#read_c_dir_entry(io) ⇒ Object
:nodoc:
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 |
# File 'lib/zip/entry.rb', line 532 def read_c_dir_entry(io) # :nodoc: @dirty = false # No changes at this point. 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 @name.tr!('\\', '/') # Normalise filepath separators after encoding set. read_extra_field(io.read(@extra_length)) @comment = io.read(@comment_length) check_c_dir_entry_comment_size set_ftype_from_c_dir_entry parse_zip64_extra(false) parse_aes_extra end |
#read_extra_field(buf, local: false) ⇒ Object
:nodoc:
524 525 526 527 528 529 530 |
# File 'lib/zip/entry.rb', line 524 def read_extra_field(buf, local: false) # :nodoc: if @extra.kind_of?(::Zip::ExtraField) @extra.merge(buf, local: local) if buf else @extra = ::Zip::ExtraField.new(buf, local: local) end end |
#read_local_entry(io) ⇒ Object
:nodoc:
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 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/zip/entry.rb', line 376 def read_local_entry(io) # :nodoc: @dirty = false # No changes at this point. current_offset = io.tell read_local_header_fields(io) if @header_signature == SPLIT_FILE_SIGNATURE raise SplitArchiveError if current_offset.zero? # Rewind, skipping the data descriptor, then try to read the local header again. current_offset += 16 io.seek(current_offset) read_local_header_fields(io) end unless @header_signature == LOCAL_ENTRY_SIGNATURE raise Error, "Zip local header magic not found at location '#{current_offset}'" end @local_header_offset = current_offset 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 @name.tr!('\\', '/') # Normalise filepath separators after encoding set. # We need to do this here because `initialize` has so many side-effects. # :-( @ftype = name_is_directory? ? :directory : :file extra = io.read(@extra_length) if extra && extra.bytesize != @extra_length raise ::Zip::Error, 'Truncated local zip entry header' end read_extra_field(extra, local: true) parse_zip64_extra(true) parse_aes_extra @local_header_size = calculate_local_header_size end |
#set_default_vars_values ⇒ Object
:nodoc:
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/zip/entry.rb', line 42 def set_default_vars_values # :nodoc: @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 = DEFAULT_RESTORE_OPTIONS[:restore_times] @restore_permissions = DEFAULT_RESTORE_OPTIONS[:restore_permissions] @restore_ownership = DEFAULT_RESTORE_OPTIONS[:restore_ownership] # BUG: need an extra field to support uid/gid's @unix_uid = nil @unix_gid = nil @unix_perms = nil end |
#set_extra_attributes_on_path(dest_path) ⇒ Object
rubocop:enable Style/GuardClause
585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/zip/entry.rb', line 585 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 # 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 |
#set_ftype_from_c_dir_entry ⇒ Object
:nodoc:
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 |
# File 'lib/zip/entry.rb', line 476 def set_ftype_from_c_dir_entry # :nodoc: @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 be 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
rubocop:disable Style/GuardClause
573 574 575 576 577 578 579 580 581 582 |
# File 'lib/zip/entry.rb', line 573 def set_unix_attributes_on_path(dest_path) # :nodoc: # Ignore setuid/setgid bits by default. Honour if @restore_ownership. unix_perms_mask = (@restore_ownership ? 0o7777 : 0o1777) if @restore_permissions && @unix_perms ::FileUtils.chmod(@unix_perms & unix_perms_mask, dest_path) end if @restore_ownership && @unix_uid && @unix_gid && ::Process.egid == 0 ::FileUtils.chown(@unix_uid, @unix_gid, dest_path) end end |
#time(component: :mtime) ⇒ Object Also known as: mtime
Get a timestamp component of this entry.
Returns modification time by default.
128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/zip/entry.rb', line 128 def time(component: :mtime) time = if @extra[:universaltime] @extra[:universaltime].send(component) elsif @extra[:ntfs] @extra[:ntfs].send(component) end # Standard time field in central directory has local time # under archive creator. Then, we can't get timezone. time || (@time if component == :mtime) end |
#time=(value, component: :mtime) ⇒ Object Also known as: mtime=
Set a timestamp component of this entry.
Sets modification time by default.
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/zip/entry.rb', line 156 def time=(value, component: :mtime) @dirty = true unless @extra.member?(:universaltime) || @extra.member?(:ntfs) @extra.create(:universaltime) end value = DOSTime.from_time(value) comp = "#{component}=" unless component.to_s.end_with?('=') (@extra[:universaltime] || @extra[:ntfs]).send(comp, value) @time = value if component == :mtime end |
#to_s ⇒ Object
:nodoc:
332 333 334 |
# File 'lib/zip/entry.rb', line 332 def to_s # :nodoc: @name end |
#unpack_c_dir_entry(buf) ⇒ Object
:nodoc:
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 |
# File 'lib/zip/entry.rb', line 455 def unpack_c_dir_entry(buf) # :nodoc: @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 = buf.unpack('VCCvvvvvVVVvvvvvVV') end |
#unpack_local_entry(buf) ⇒ Object
:nodoc:
361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/zip/entry.rb', line 361 def unpack_local_entry(buf) # :nodoc: @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)
280 281 282 283 284 285 286 287 288 |
# File 'lib/zip/entry.rb', line 280 def verify_local_header_size! # :nodoc: return if @local_header_size.nil? new_size = calculate_local_header_size return unless @local_header_size != new_size raise Error, "Local header size changed (#{@local_header_size} -> #{new_size})" end |
#write_c_dir_entry(io, suppress_extra_fields: false) ⇒ Object
:nodoc:
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 |
# File 'lib/zip/entry.rb', line 623 def write_c_dir_entry(io, suppress_extra_fields: false) # :nodoc: prep_cdir_zip64_extra 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 @extra.suppress_fields!(suppress_extra_fields) if suppress_extra_fields io << pack_c_dir_entry io << @name io << (@extra ? @extra.to_c_dir_bin : '') io << @comment end |
#write_local_entry(io, suppress_extra_fields: false, rewrite: false) ⇒ Object
:nodoc:
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
# File 'lib/zip/entry.rb', line 435 def write_local_entry(io, suppress_extra_fields: false, rewrite: false) # :nodoc: prep_local_zip64_extra # If we are rewriting the local header, then we verify that we haven't changed # its size. At this point we have to keep extra fields if they are present. if rewrite verify_local_header_size! elsif suppress_extra_fields @extra.suppress_fields!(suppress_extra_fields) end @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:
730 731 732 733 734 735 736 737 738 739 740 741 |
# File 'lib/zip/entry.rb', line 730 def write_to_zip_output_stream(zip_output_stream) # :nodoc: if ftype == :directory zip_output_stream.put_next_entry(self) elsif @filepath zip_output_stream.put_next_entry(self) get_input_stream do |is| ::Zip::IOExtras.copy_stream(zip_output_stream, is) end else zip_output_stream.copy_raw_entry(self) end end |
#zip64? ⇒ Boolean
Does this entry use the ZIP64 extensions?
202 203 204 |
# File 'lib/zip/entry.rb', line 202 def zip64? !@extra[:zip64].nil? end |