Module: Gsplat::IO::ZipArchive

Defined in:
lib/gsplat/io/zip_archive.rb

Overview

Minimal single-disk ZIP reader/writer for NPZ archives.

Constant Summary collapse

LOCAL_SIGNATURE =
0x04034B50
CENTRAL_SIGNATURE =
0x02014B50
END_SIGNATURE =
0x06054B50
UTF8_FLAG =
0x0800
METHODS =
{ stored: 0, deflate: 8 }.freeze
LOCAL_TEMPLATE =
"VvvvvvVVVvv"
CENTRAL_TEMPLATE =
"VvvvvvvVVVvvvvvVV"
END_TEMPLATE =
"VvvvvVVv"

Class Method Summary collapse

Class Method Details

.decode(archive) ⇒ Hash{String=>String}

Decodes named binary entries from a ZIP archive.

Parameters:

  • archive (String)

    ZIP bytes

Returns:

  • (Hash{String=>String})


50
51
52
53
54
55
56
57
58
59
# File 'lib/gsplat/io/zip_archive.rb', line 50

def decode(archive)
  archive = archive.b
  entry_count, central_offset = (archive)
  cursor = central_offset

  entry_count.times.each_with_object({}) do |_, entries|
    , cursor = parse_central_entry(archive, cursor)
    entries[.fetch(:name)] = extract_entry(archive, )
  end
end

.encode(entries, compression: :deflate) ⇒ String

Encodes named binary entries as a ZIP archive.

Parameters:

  • entries (Hash{String=>String})
  • compression (Symbol) (defaults to: :deflate)

    :stored or :deflate

Returns:

  • (String)

    ZIP bytes



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gsplat/io/zip_archive.rb', line 25

def encode(entries, compression: :deflate)
  method = METHODS.fetch(compression) do
    raise ArgumentError, "unknown compression #{compression.inspect}; expected stored or deflate"
  end
  body = +"".b
  central = +"".b

  entries.each do |name, data|
    name = name.to_s.b
    data = data.b
    compressed = method.zero? ? data : deflate(data)
     = (name, data, compressed, method, body.bytesize)
    body << local_header() << name << compressed
    central << central_header() << name
  end

  central_offset = body.bytesize
  body << central
  body << end_record(entries.length, central.bytesize, central_offset)
end