Parent

Included Modules

Archive::Zip::Entry::File

Archive::Zip::Entry::File represents a file entry within a Zip archive.

Attributes

file_path[R]

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

new(zip_path, raw_data = nil) click to toggle source

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.

# File lib/archive/zip/entry.rb, line 1014
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

extract(options = {}) click to toggle source

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 to true 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 1121
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
file?() click to toggle source

Returns true.

# File lib/archive/zip/entry.rb, line 1027
def file?
  true
end
file_data() click to toggle source

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 1063
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
file_data=(file_data) click to toggle source

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 1089
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
file_path=(file_path) click to toggle source

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 1052
def file_path=(file_path)
  @file_data = nil
  @raw_data = nil
  @file_path = file_path
end
ftype() click to toggle source

Returns the file type of this entry as the symbol :file.

# File lib/archive/zip/entry.rb, line 1022
def ftype
  :file
end
mode=(mode) click to toggle source

Overridden in order to ensure that the proper mode bits are set for a file.

# File lib/archive/zip/entry.rb, line 1033
def mode=(mode)
  super(0100000 | (mode & 07777))
end
password=(password) click to toggle source

Sets the decryption password.

# File lib/archive/zip/entry.rb, line 1038
def password=(password)
  unless @raw_data.nil? then
    @file_data = nil
  end
  @password = password
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.