Class: Zippo::ZipDirectory

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/zippo/zip_directory.rb

Overview

The ZipDirectory is responsible for managing the set of ZipMembers belonging to a ZipFile.

Instance Method Summary collapse

Constructor Details

#initialize(io = nil) ⇒ ZipDirectory

Returns a new instance of ZipDirectory.



16
17
18
# File 'lib/zippo/zip_directory.rb', line 16

def initialize(io = nil)
  @io = io
end

Instance Method Details

#[](name) ⇒ ZipMember

Returns the ZipMember with the specified name.

Parameters:

  • name (String)

    the name of the ZipMember

Returns:

  • (ZipMember)

    the ZipMember with the specified name



22
23
24
# File 'lib/zippo/zip_directory.rb', line 22

def [](name)
  entries_hash[name]
end

#[]=(name, data) ⇒ Object

Inserts data into the ZipFile

Parameters:

  • name (String)

    the name of the member to insert

  • data (String)

    the data to insert



30
31
32
# File 'lib/zippo/zip_directory.rb', line 30

def []=(name, data)
  insert(name, StringIO.new(data))
end

#entriesArray

Returns the members of the ZipFile.

Returns:

  • (Array)

    the members of the ZipFile



73
74
75
# File 'lib/zippo/zip_directory.rb', line 73

def entries
  entries_hash.values
end

#entries_hashHash

Returns the hash of ZipMembers, the hash key is the member’s name.

Returns:

  • (Hash)

    the hash of ZipMembers, the hash key is the member’s name



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/zippo/zip_directory.rb', line 60

def entries_hash
  @entries_hash ||=
    if @io
      CentralDirectoryReader.new(@io)
        .cd_file_headers.each_with_object({}) do |header, hash|
          hash[header.name] = ZipMember.new @io, header
        end
    else
      {}
    end
end

#insert(name, source) ⇒ Object

Inserts data into the ZipFile

  • when the source is a ZipMember, the already-compressed data may be re-used when writing out

  • when the source is a String, it is interpreted as a filename, and will be used as the source of the data

  • otherwise, source is assumed to an already opened IO object

Source can be any of

  • an IO object

  • a string (path to file)

  • another ZipMember (allowing direct stream copy)

Parameters:

  • name (String)

    the name of the member to insert

  • source

    where to read the data from



49
50
51
52
53
54
55
56
# File 'lib/zippo/zip_directory.rb', line 49

def insert(name, source)
  set name,
      case source
      when ZipMember then source.with_name name
      when String then IOZipMember.new name, File.open(source, 'r:BINARY')
      else IOZipMember.new name, source
      end
end