Class: Zippo::ZipDirectory
- Inherits:
-
Object
- Object
- Zippo::ZipDirectory
- 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
-
#[](name) ⇒ ZipMember
The ZipMember with the specified name.
-
#[]=(name, data) ⇒ Object
Inserts data into the ZipFile.
-
#entries ⇒ Array
The members of the ZipFile.
-
#entries_hash ⇒ Hash
The hash of ZipMembers, the hash key is the member’s name.
-
#initialize(io = nil) ⇒ ZipDirectory
constructor
A new instance of ZipDirectory.
-
#insert(name, source) ⇒ Object
Inserts data into the ZipFile.
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.
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
30 31 32 |
# File 'lib/zippo/zip_directory.rb', line 30 def []=(name, data) insert(name, StringIO.new(data)) end |
#entries ⇒ Array
Returns the members of the ZipFile.
73 74 75 |
# File 'lib/zippo/zip_directory.rb', line 73 def entries entries_hash.values end |
#entries_hash ⇒ Hash
Returns 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)
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 |