Class: Omnizip::Buffer::MemoryArchive

Inherits:
Object
  • Object
show all
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).

Examples:

Creating an archive

buffer = StringIO.new
Omnizip::Zip::OutputStream.open(buffer) do |zos|
  archive = MemoryArchive.new(zos, :zip)
  archive.add('file.txt', 'content')
end

Reading an archive

Omnizip::Zip::InputStream.open(buffer) do |zis|
  archive = MemoryArchive.new(zis, :zip)
  archive.each_entry do |entry|
    puts entry.name
  end
end

Defined Under Namespace

Classes: Entry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream, format) ⇒ MemoryArchive

Initialize memory archive wrapper

Parameters:



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

#formatObject (readonly)

Returns the value of attribute format.



26
27
28
# File 'lib/omnizip/buffer/memory_archive.rb', line 26

def format
  @format
end

#streamObject (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)

Examples:

Add multiple files

archive.add('file1.txt', 'content1')
       .add('file2.txt', 'content2')
       .add('dir/', '')  # Directory entry

Parameters:

  • name (String)

    Entry name (path within archive)

  • data (String)

    Entry content

  • options (Hash)

    Entry options

Options Hash (**options):

  • :time (Time)

    Modification time (default: now)

  • :comment (String)

    Entry comment

  • :compression (Symbol)

    Compression method (:store, :deflate)

  • :level (Integer)

    Compression level (1-9)

Returns:

  • (self)

    For method chaining

Raises:

  • (RuntimeError)

    If stream is not an OutputStream



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, **options)
  ensure_write_mode!

  case stream
  when Omnizip::Zip::OutputStream
    stream.put_next_entry(name, **options)
    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)

Examples:

Add with block

archive.add_data('file.txt') { File.read('source.txt') }

Parameters:

  • name (String)

    Entry name

  • options (Hash)

    Entry options

Yields:

  • Block that returns content

Yield Returns:

  • (String)

    Entry content

Returns:

  • (self)

    For method chaining



82
83
84
85
86
# File 'lib/omnizip/buffer/memory_archive.rb', line 82

def add_data(name, **options)
  ensure_write_mode!
  data = yield
  add(name, data, **options)
end

#each_entry {|entry| ... } ⇒ void

This method returns an undefined value.

Iterate entries (read mode only)

Examples:

Process all entries

archive.each_entry do |entry|
  puts "#{entry.name}: #{entry.size} bytes"
  content = entry.read unless entry.directory?
end

Yields:

  • (entry)

    Block called for each entry

Yield Parameters:

  • entry (Entry)

    Archive entry

Raises:

  • (RuntimeError)

    If stream is not an InputStream



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_memoryHash<String, String>

Extract all entries to memory (read mode only)

Examples:

Extract all

files = archive.extract_all_to_memory
files.each { |name, content| puts "#{name}: #{content.size}" }

Returns:

  • (Hash<String, String>)

    Filename => content mapping



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_sString

Get underlying buffer as string (write mode only)

Examples:

Get archive data

archive_data = archive.to_s
File.binwrite('output.zip', archive_data)

Returns:

  • (String)

    Complete archive as binary string

Raises:

  • (RuntimeError)

    If stream is not an OutputStream



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