class Archive::Zip::Entry::File
Archive::Zip::Entry::File
represents a file entry within a Zip
archive.
Attributes
The path to a file whose contents are to be used for uncompressed file data. This will be nil
if the file_data
attribute is set directly.
Public Class Methods
Creates a new file entry where zip_path is the path to the entry in the ZIP archive. The Archive::Zip::Codec::Deflate
codec with the default compression level set (NORMAL) is used by default for compression. raw_data, if specified, must be a readable, IO-like object containing possibly compressed/encrypted file data for the entry. It is intended to be used primarily by the Archive::Zip::Entry.parse
class method.
Archive::Zip::Entry::new
# File lib/archive/zip/entry.rb, line 1015 def initialize(zip_path, raw_data = nil) super(zip_path, raw_data) @file_path = nil @file_data = nil @compression_codec = Zip::Codec::Deflate.new end
Public Instance Methods
Extracts this entry.
options is a Hash optionally containing the following:
- :file_path
-
Specifies the path to which this entry will be extracted. Defaults to the zip path of this entry.
- :permissions
-
When set to
false
(the default), POSIX mode/permission bits will be ignored. Otherwise, they will be restored if possible. - :ownerships
-
When set to
false
(the default), user and group ownerships will be ignored. On most systems, only a superuser is able to change ownerships, so setting this option totrue
as a regular user may have no effect. - :times
-
When set to
false
(the default), last accessed and last modified times will be ignored. Otherwise, they will be restored if possible.
Raises Archive::Zip::EntryError
if the extracted file data appears corrupt.
# File lib/archive/zip/entry.rb, line 1122 def extract(options = {}) # Ensure that unspecified options have default values. file_path = options.has_key?(:file_path) ? options[:file_path].to_s : @zip_path restore_permissions = options.has_key?(:permissions) ? options[:permissions] : false restore_ownerships = options.has_key?(:ownerships) ? options[:ownerships] : false restore_times = options.has_key?(:times) ? options[:times] : false # Create the containing directory tree if necessary. parent_dir = ::File.dirname(file_path) FileUtils.mkdir_p(parent_dir) unless ::File.exist?(parent_dir) # Dump the file contents. ::File.open(file_path, 'wb') do |f| while buffer = file_data.read(4096) do f.write(buffer) end end # Verify that the extracted data is good. begin unless expected_data_descriptor.nil? then expected_data_descriptor.verify(file_data.data_descriptor) end rescue => e raise Zip::EntryError, "`#{zip_path}': #{e.message}" end # Restore the metadata. ::File.chmod(mode, file_path) if restore_permissions ::File.chown(uid, gid, file_path) if restore_ownerships ::File.utime(atime, mtime, file_path) if restore_times # Attempt to rewind the file data back to the beginning, but ignore # errors. begin file_data.rewind rescue # Ignore. end nil end
Returns true
.
# File lib/archive/zip/entry.rb, line 1028 def file? true end
Returns a readable, IO-like object containing uncompressed file data.
NOTE: It is the responsibility of the user of this attribute to ensure that the close method of the returned IO-like object is called when the object is no longer needed.
# File lib/archive/zip/entry.rb, line 1064 def file_data return @file_data unless @file_data.nil? || @file_data.closed? unless raw_data.nil? then raw_data.rewind @file_data = compression_codec.decompressor( encryption_codec.decryptor(raw_data, password) ) else if @file_path.nil? then simulated_raw_data = BinaryStringIO.new else simulated_raw_data = ::File.new(@file_path, 'rb') end # Ensure that the IO-like object can return a data descriptor so that # it's possible to verify extraction later if desired. @file_data = Zip::Codec::Store.new.decompressor(simulated_raw_data) end @file_data end
Sets the file_data
attribute of this object to file_data. file_data must be a readable, IO-like object.
NOTE: As a side effect, the file_path
and raw_data
attributes for this object will be set to nil
.
# File lib/archive/zip/entry.rb, line 1090 def file_data=(file_data) @file_path = nil self.raw_data = nil @file_data = file_data # Ensure that the IO-like object can return CRC32 and data size # information so that it's possible to verify extraction later if desired. unless @file_data.respond_to?(:data_descriptor) then @file_data = Zip::Codec::Store.new.decompressor(@file_data) end @file_data end
Sets the file_path
attribute to file_path which should be a String usable with File#new to open a file for reading which will provide the IO-like object for the file_data
attribute.
# File lib/archive/zip/entry.rb, line 1053 def file_path=(file_path) @file_data = nil @raw_data = nil @file_path = file_path end
Returns the file type of this entry as the symbol :file
.
# File lib/archive/zip/entry.rb, line 1023 def ftype :file end
Overridden in order to ensure that the proper mode bits are set for a file.
# File lib/archive/zip/entry.rb, line 1034 def mode=(mode) super(0100000 | (mode & 07777)) end
Sets the decryption password.
# File lib/archive/zip/entry.rb, line 1039 def password=(password) unless @raw_data.nil? then @file_data = nil end @password = password end
Private Instance Methods
Write the file data to io.
# File lib/archive/zip/entry.rb, line 1176 def dump_file_data(io) while buffer = file_data.read(4096) do io.write(buffer) end # Attempt to ensure that the file data will still be in a readable state # at the beginning of the data for the next user, but close it if possible # in order to conserve resources. if file_path.nil? then # When the file_path attribute is not set, the file_data method cannot # reinitialize the IO object it returns, so attempt to rewind the file # data back to the beginning, but ignore errors. begin file_data.rewind rescue # Ignore. end else # Since the file_path attribute is set, the file_data method will # reinitialize the IO object it returns if we close the object here. file_data.close end end