Module: Omnizip::Buffer
- Defined in:
- lib/omnizip/buffer.rb,
lib/omnizip/buffer/memory_archive.rb,
lib/omnizip/buffer/memory_extractor.rb
Overview
In-memory archive operations without filesystem I/O
This module provides methods for creating and reading archives entirely in memory using StringIO, enabling web applications, testing, and API responses without temporary files.
Defined Under Namespace
Classes: MemoryArchive, MemoryExtractor
Class Method Summary collapse
-
.create(format = :zip, **options) {|archive| ... } ⇒ StringIO
Create archive in memory.
-
.create_from_hash(hash, format = :zip, **options) ⇒ StringIO
Create archive from Hash of filename => content.
-
.extract_to_memory(data, format: nil) ⇒ Hash<String, String>
Extract all entries to memory.
-
.open(data, format: nil) {|archive| ... } ⇒ MemoryArchive, Object
Open archive from memory.
Class Method Details
.create(format = :zip, **options) {|archive| ... } ⇒ StringIO
Create archive in memory
rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/omnizip/buffer.rb', line 50 def create(format = :zip, **, &block) buffer = StringIO.new(String.new(encoding: Encoding::BINARY)) case format when :zip create_zip(buffer, , &block) when :seven_zip, :"7z" raise NotImplementedError, "7z format support coming in Phase 2" else raise ArgumentError, "Unsupported format: #{format}" end buffer.tap(&:rewind) end |
.create_from_hash(hash, format = :zip, **options) ⇒ StringIO
Create archive from Hash of filename => content
120 121 122 123 124 125 126 |
# File 'lib/omnizip/buffer.rb', line 120 def create_from_hash(hash, format = :zip, **) create(format, **) do |archive| hash.each do |name, content| archive.add(name, content) end end end |
.extract_to_memory(data, format: nil) ⇒ Hash<String, String>
Extract all entries to memory
105 106 107 108 |
# File 'lib/omnizip/buffer.rb', line 105 def extract_to_memory(data, format: nil) extractor = Buffer::MemoryExtractor.new(data, format: format) extractor.extract_all end |
.open(data, format: nil) {|archive| ... } ⇒ MemoryArchive, Object
Open archive from memory
80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/omnizip/buffer.rb', line 80 def open(data, format: nil, &block) buffer = data.is_a?(StringIO) ? data : StringIO.new(data.b) format ||= detect_format(buffer) case format when :zip open_zip(buffer, &block) when :seven_zip, :"7z" raise NotImplementedError, "7z format support coming in Phase 2" else raise ArgumentError, "Unsupported format: #{format}" end end |