class ZipContainer::File

This class represents a ZipContainer file in PK Zip format. See the OCF and UCF specifications for more details.

This class provides most of the facilities of the Zip::File class in the rubyzip gem. Please also consult the rubyzip documentation alongside these pages.

There are code examples available with the source code of this library.

Public Class Methods

create(filename, mimetype) → document click to toggle source
create(filename, mimetype) {|document| ...}

Create a new ZipContainer file on disk with the specified mimetype.

# File lib/zip-container/file.rb, line 77
def self.create(filename, mimetype, &block)
  ::Zip::OutputStream.open(filename) do |stream|
    stream.put_next_entry(MIMETYPE_FILE, nil, nil, ::Zip::Entry::STORED)
    stream.write mimetype
  end

  # Now open the newly created container.
  c = new(filename)

  if block_given?
    begin
      yield c
    ensure
      c.close
    end
  end

  c
end
each_entry → Enumerator click to toggle source
each_entry {|entry| ...}

Iterate over the entries in the ZipContainer file. The entry objects returned by this method are Zip::Entry objects. Please see the rubyzip documentation for details.

# File lib/zip-container/file.rb, line 104
def self.each_entry(filename, &block)
  c = new(filename)

  if block_given?
    begin
      c.each(&block)
    ensure
      c.close
    end
  end

  c.each
end

Public Instance Methods

add(entry, src_path, &continue_on_exists_proc) click to toggle source

Convenience method for adding the contents of a file to the ZipContainer file. If asked to add a file with a reserved name, such as the special mimetype header file, this method will raise a ReservedNameClashError.

See the rubyzip documentation for details of the continue_on_exists_proc parameter.

# File lib/zip-container/file.rb, line 128
def add(entry, src_path, &continue_on_exists_proc)
  if reserved_entry?(entry) || managed_directory?(entry)
    raise ReservedNameClashError.new(entry.to_s)
  end

  @container.add(entry, src_path, &continue_on_exists_proc)
end
close()
Alias for: commit
comment → String click to toggle source

Returns the ZipContainer file comment, if it has one.

# File lib/zip-container/file.rb, line 380
    
comment = comment click to toggle source

Set the ZipContainer file comment to the new value.

# File lib/zip-container/file.rb, line 387
    
commit → boolean click to toggle source
close → boolean

Commits changes that have been made since the previous commit to the ZipContainer file. Returns true if anything was actually done, false otherwise.

# File lib/zip-container/file.rb, line 143
def commit
  return false unless commit_required?

  if on_disk?
    @container.commit
  end
end
Also aliased as: close
commit_required? → boolean click to toggle source

Returns true if any changes have been made to this ZipContainer file since the last commit, false otherwise.

# File lib/zip-container/file.rb, line 395
    
dir → Zip::ZipFsDir click to toggle source

Returns an object which can be used like ruby’s built in Dir (class) object, except that it works on the ZipContainer file on which this method is invoked.

See the rubyzip documentation for details.

# File lib/zip-container/file.rb, line 161
def dir
  @fs_dir
end
each → Enumerator click to toggle source
each {|entry| ...}

Iterate over the entries in the ZipContainer file. The entry objects returned by this method are Zip::Entry objects. Please see the rubyzip documentation for details.

# File lib/zip-container/file.rb, line 405
    
entries → Enumerable click to toggle source

Returns an Enumerable containing all the entries in the ZipContainer file The entry objects returned by this method are Zip::Entry objects. Please see the rubyzip documentation for details.

# File lib/zip-container/file.rb, line 414
    
extract(entry, dest_path, &on_exists_proc) click to toggle source

Extracts the specified entry of the ZipContainer file to dest_path.

See the rubyzip documentation for details of the on_exists_proc parameter.

# File lib/zip-container/file.rb, line 424
    
file → Zip::ZipFsFile click to toggle source

Returns an object which can be used like ruby’s built in File (class) object, except that it works on the ZipContainer file on which this method is invoked.

See the rubyzip documentation for details.

# File lib/zip-container/file.rb, line 173
def file
  @fs_file
end
find_entry(entry_name, options = {}) → Zip::Entry or nil click to toggle source

Searches for the entry with the specified name. Returns nil if no entry is found or if the specified entry is hidden for normal use. You can specify :include_hidden => true to include hidden entries in the search.

# File lib/zip-container/file.rb, line 184
def find_entry(entry_name, options = {})
  options = {:include_hidden => false}.merge(options)

  unless options[:include_hidden]
    return if hidden_entry?(entry_name)
  end

  @container.find_entry(entry_name)
end
get_entry(entry, options = {}) → Zip::Entry or nil click to toggle source

Searches for an entry like #find_entry, but throws Errno::ENOENT if no entry is found or if the specified entry is hidden for normal use. You can specify :include_hidden => true to include hidden entries in the search.

# File lib/zip-container/file.rb, line 201
def get_entry(entry, options = {})
  options = {:include_hidden => false}.merge(options)

  unless options[:include_hidden]
    raise Errno::ENOENT, entry if hidden_entry?(entry)
  end

  @container.get_entry(entry)
end
get_input_stream(entry) → stream click to toggle source
get_input_stream(entry) {|stream| ...}

Returns an input stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby’s built in File.open method.

# File lib/zip-container/file.rb, line 450
    
get_output_stream(entry, permission = nil) → stream click to toggle source
get_output_stream(entry, permission = nil) {|stream| ...}

Returns an output stream to the specified entry. If a block is passed the stream object is passed to the block and the stream is automatically closed afterwards just as with ruby’s built-in File.open method.

See the rubyzip documentation for details of the permission_int parameter.

# File lib/zip-container/file.rb, line 221
def get_output_stream(entry, permission = nil, &block)
  if reserved_entry?(entry) || managed_directory?(entry)
    raise ReservedNameClashError.new(entry.to_s)
  end

  @container.get_output_stream(entry, permission, &block)
end
glob(pattern) → Array click to toggle source
glob(pattern) { |entry| ... }
glob(pattern, *parameters) → Array
glob(pattern, *parameters) { |entry| ... }

Searches for entries given a glob. Hidden files are ignored by default.

The parameters that can be supplied are:

  • flags - A bitwise OR of the FNM_xxx parameters defined in File::Constants. The default value is ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH

  • options - :include_hidden => true will include hidden entries in the search.

# File lib/zip-container/file.rb, line 243
def glob(pattern, *params, &block)
  flags = ::File::FNM_PATHNAME | ::File::FNM_DOTMATCH
  options = { :include_hidden => false }

  params.each do |param|
    case param
    when Hash
      options = options.merge(param)
    else
      flags = param
    end
  end

  entries.map do |entry|
    next if !options[:include_hidden] && hidden_entry?(entry)
    next unless ::File.fnmatch(pattern, entry.name.chomp('/'), flags)
    yield(entry) if block_given?
    entry
  end.compact
end
in_memory? → boolean click to toggle source

Is this ZipContainer file memory resident as opposed to stored on disk?

# File lib/zip-container/file.rb, line 268
def in_memory?
  !@on_disk
end
mkdir(name, permission = 0755) click to toggle source

Creates a directory in the ZipContainer file. If asked to create a directory with a name reserved for use by a file this method will raise a ReservedNameClashError.

The new directory will be created with the supplied unix-style permissions. The default (0755) is owner read, write and list; group read and list; and world read and list.

# File lib/zip-container/file.rb, line 282
def mkdir(name, permission = 0755)
  if reserved_entry?(name) || managed_file?(name)
    raise ReservedNameClashError.new(name)
  end

  @container.mkdir(name, permission)
end
name → String click to toggle source

Returns the filename of this ZipContainer file.

# File lib/zip-container/file.rb, line 469
    
on_disk? → boolean click to toggle source

Is this ZipContainer file stored on disk as opposed to memory resident?

# File lib/zip-container/file.rb, line 294
def on_disk?
  @on_disk
end
read(entry) → String click to toggle source

Returns a string containing the contents of the specified entry.

# File lib/zip-container/file.rb, line 476
    
remove(entry) click to toggle source

Removes the specified entry from the ZipContainer file. If asked to remove any reserved files such as the special mimetype header file this method will do nothing.

# File lib/zip-container/file.rb, line 304
def remove(entry)
  return if reserved_entry?(entry)
  @container.remove(entry)
end
rename(entry, new_name, &continue_on_exists_proc) click to toggle source

Renames the specified entry in the ZipContainer file. If asked to rename any reserved files such as the special mimetype header file this method will do nothing. If asked to rename a file to one of the reserved names a ReservedNameClashError is raised.

See the rubyzip documentation for details of the continue_on_exists_proc parameter.

# File lib/zip-container/file.rb, line 319
def rename(entry, new_name, &continue_on_exists_proc)
  return if reserved_entry?(entry)
  raise ReservedNameClashError.new(new_name) if reserved_entry?(new_name)

  @container.rename(entry, new_name, &continue_on_exists_proc)
end
replace(entry, src_path) click to toggle source

Replaces the specified entry of the ZipContainer file with the contents of src_path (from the file system). If asked to replace any reserved files such as the special mimetype header file this method will do nothing.

# File lib/zip-container/file.rb, line 333
def replace(entry, src_path)
  return if reserved_entry?(entry)
  @container.replace(entry, src_path)
end
size → int click to toggle source

Returns the number of entries in the ZipContainer file.

# File lib/zip-container/file.rb, line 483
  
to_s → String click to toggle source

Return a textual summary of this ZipContainer file.

# File lib/zip-container/file.rb, line 342
def to_s
  @container.to_s + " - #{@mimetype}"
end