Class: Omnizip::Buffer::MemoryArchive
- Inherits:
-
Object
- Object
- Omnizip::Buffer::MemoryArchive
- Defined in:
- lib/omnizip/buffer/memory_archive.rb
Overview
Wrapper for in-memory archive operations
Provides unified interface for adding entries to and reading entries from archives stored in memory (StringIO). Works with both OutputStream (for creating) and InputStream (for reading).
Defined Under Namespace
Classes: Entry
Instance Attribute Summary collapse
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
-
#add(name, data, **options) ⇒ self
Add file from memory (write mode only).
-
#add_data(name, **options) { ... } ⇒ self
Add data with block (write mode only).
-
#each_entry {|entry| ... } ⇒ void
Iterate entries (read mode only).
-
#extract_all_to_memory ⇒ Hash<String, String>
Extract all entries to memory (read mode only).
-
#initialize(stream, format) ⇒ MemoryArchive
constructor
Initialize memory archive wrapper.
-
#to_s ⇒ String
Get underlying buffer as string (write mode only).
Constructor Details
#initialize(stream, format) ⇒ MemoryArchive
Initialize memory archive wrapper
33 34 35 36 37 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 33 def initialize(stream, format) @stream = stream @format = format @entries_cache = nil end |
Instance Attribute Details
#format ⇒ Object (readonly)
Returns the value of attribute format.
26 27 28 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 26 def format @format end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
26 27 28 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 26 def stream @stream end |
Instance Method Details
#add(name, data, **options) ⇒ self
Add file from memory (write mode only)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 57 def add(name, data, **) ensure_write_mode! case stream when Omnizip::Zip::OutputStream stream.put_next_entry(name, **) stream.write(data) unless name.end_with?("/") else raise NotImplementedError, "Unsupported stream type: #{stream.class}" end self end |
#add_data(name, **options) { ... } ⇒ self
Add data with block (write mode only)
82 83 84 85 86 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 82 def add_data(name, **) ensure_write_mode! data = yield add(name, data, **) end |
#each_entry {|entry| ... } ⇒ void
This method returns an undefined value.
Iterate entries (read mode only)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 101 def each_entry ensure_read_mode! case stream when Omnizip::Zip::InputStream while (zip_entry = stream.get_next_entry) entry = Entry.new(zip_entry, stream) yield(entry) end else raise NotImplementedError, "Unsupported stream type: #{stream.class}" end end |
#extract_all_to_memory ⇒ Hash<String, String>
Extract all entries to memory (read mode only)
123 124 125 126 127 128 129 130 131 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 123 def extract_all_to_memory ensure_read_mode! result = {} each_entry do |entry| result[entry.name] = entry.read unless entry.directory? end result end |
#to_s ⇒ String
Get underlying buffer as string (write mode only)
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/omnizip/buffer/memory_archive.rb', line 142 def to_s ensure_write_mode! case stream when Omnizip::Zip::OutputStream # OutputStream wraps the IO, we need to get the underlying buffer # This is only safe after close unless stream.closed? raise "Archive must be closed before accessing data" end # The buffer was passed in during creation, but we don't have # direct access. This method should be called on the StringIO # returned by Buffer.create instead. raise NotImplementedError, "Use Buffer.create return value instead" else raise "Cannot get string from read mode archive" end end |