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.

Examples:

Create archive in memory

zip_data = Omnizip::Buffer.create(:zip) do |archive|
  archive.add('readme.txt', 'Hello World')
  archive.add('data.json', '{"key": "value"}')
end
# => Returns StringIO with complete ZIP archive

Extract from memory

contents = Omnizip::Buffer.extract_to_memory(zip_data)
# => {"readme.txt" => "Hello World", "data.json" => '{"key": "value"}'}

From Hash

archive_data = {
  'file1.txt' => 'content1',
  'file2.txt' => 'content2'
}
zip_buffer = Omnizip::Buffer.create_from_hash(archive_data, :zip)

Defined Under Namespace

Classes: MemoryArchive, MemoryExtractor

Class Method Summary collapse

Class Method Details

.create(format = :zip, **options) {|archive| ... } ⇒ StringIO

Create archive in memory

rubocop:disable Naming/BlockForwarding, Style/ArgumentsForwarding -- Ruby 3.0 compatibility

Examples:

Create ZIP in memory

buffer = Omnizip::Buffer.create(:zip) do |archive|
  archive.add('file.txt', 'content')
  archive.add('dir/file2.txt', 'more content')
end
File.binwrite('output.zip', buffer.string)

Parameters:

  • format (Symbol) (defaults to: :zip)

    Archive format (:zip, :seven_zip)

  • options (Hash)

    Format-specific options

Yields:

  • (archive)

    Block to populate archive

Yield Parameters:

Returns:

  • (StringIO)

    Complete archive in memory, rewound to start



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, **options, &block)
  buffer = StringIO.new(String.new(encoding: Encoding::BINARY))

  case format
  when :zip
    create_zip(buffer, options, &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

Examples:

Create from Hash

data = {'file1.txt' => 'content1', 'file2.txt' => 'content2'}
zip = Omnizip::Buffer.create_from_hash(data, :zip)

Parameters:

  • hash (Hash<String, String>)

    Filename => content mapping

  • format (Symbol) (defaults to: :zip)

    Archive format

  • options (Hash)

    Format-specific options

Returns:

  • (StringIO)

    Complete archive in memory



120
121
122
123
124
125
126
# File 'lib/omnizip/buffer.rb', line 120

def create_from_hash(hash, format = :zip, **options)
  create(format, **options) 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

Examples:

Extract to Hash

files = Omnizip::Buffer.extract_to_memory(zip_data)
files.each do |name, content|
  puts "#{name}: #{content.bytesize} bytes"
end

Parameters:

  • data (String, StringIO)

    Archive data

  • format (Symbol, nil) (defaults to: nil)

    Archive format (auto-detected if nil)

Returns:

  • (Hash<String, String>)

    Filename => content mapping



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

Examples:

Read entries

Omnizip::Buffer.open(zip_data) do |archive|
  archive.each_entry do |entry|
    puts "#{entry.name}: #{entry.size} bytes"
  end
end

Parameters:

  • data (String, StringIO)

    Archive data

  • format (Symbol, nil) (defaults to: nil)

    Archive format (auto-detected if nil)

Yields:

  • (archive)

    Block to read from archive

Yield Parameters:

  • archive (MemoryArchive)

    Archive object to read entries from

Returns:

  • (MemoryArchive, Object)

    Archive object or block return value



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