Class: ZipTricks::Manifest

Inherits:
Struct
  • Object
show all
Defined in:
lib/zip_tricks/manifest.rb

Overview

Helps to estimate archive sizes

Defined Under Namespace

Classes: ZipSpan

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ioObject

Returns the value of attribute io

Returns:

  • (Object)

    the current value of io



2
3
4
# File 'lib/zip_tricks/manifest.rb', line 2

def io
  @io
end

#part_listObject

Returns the value of attribute part_list

Returns:

  • (Object)

    the current value of part_list



2
3
4
# File 'lib/zip_tricks/manifest.rb', line 2

def part_list
  @part_list
end

#zip_streamerObject

Returns the value of attribute zip_streamer

Returns:

  • (Object)

    the current value of zip_streamer



2
3
4
# File 'lib/zip_tricks/manifest.rb', line 2

def zip_streamer
  @zip_streamer
end

Class Method Details

.build {|Manifest| ... } ⇒ Array<ZipSpan>, Fixnum

Builds an array of spans within the ZIP file and computes the size of the resulting archive in bytes.

zip_spans, bytesize = Manifest.build do | b |
  b.add_stored_entry(name: "file.doc", size: 898291)
  b.add_compressed_entry(name: "family.tif", size: 89281911, compressed_size: 121908)
end
bytesize #=> ... (Fixnum or Bignum)
zip_spans[0] #=> Manifest::ZipSpan(part_type: :entry_header, byte_range_in_zip: 0..44, ...)
zip_spans[-1] #=> Manifest::ZipSpan(part_type: :central_directory, byte_range_in_zip: 776721..898921, ...)

Yields:

  • (Manifest)

    the manifest object you can add entries to

Returns:

  • (Array<ZipSpan>, Fixnum)

    an array of byte spans within the final ZIP, and the total size of the archive



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/zip_tricks/manifest.rb', line 20

def self.build
  output_io = ZipTricks::WriteAndTell.new(ZipTricks::NullWriter)
  part_list = []
  last_range_end = 0
  ZipTricks::Streamer.open(output_io) do | zip_streamer |
    manifest = new(zip_streamer, output_io, part_list)
    yield(manifest)
    last_range_end = part_list[-1].byte_range_in_zip.end
  end

  # Record the position of the central directory
  directory_location = (last_range_end + 1)..(output_io.tell - 1)
  part_list << ZipSpan.new(:central_directory, directory_location, :central_directory, nil)

  [part_list, output_io.tell]
end

Instance Method Details

#add_compressed_entry(name:, size_uncompressed:, size_compressed:, segment_info: nil) ⇒ Object

Add a fake entry to the archive, to see how big it is going to be in the end.

Parameters:

  • name (String)

    the name of the file (filenames are variable-width in the ZIP)

  • size_uncompressed (Fixnum)

    size of the uncompressed entry

  • size_compressed (Fixnum)

    size of the compressed entry

  • segment_info (Object) (defaults to: nil)

    if you need to save anything to retrieve later from the Manifest, pass it here (like the URL of the file)

Returns:

  • self



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/zip_tricks/manifest.rb', line 64

def add_compressed_entry(name:, size_uncompressed:, size_compressed:, segment_info: nil)
  register_part(:entry_header, name, segment_info) do
    zip_streamer.add_compressed_entry(name, size_uncompressed, C_fake_crc, size_compressed)
  end

  register_part(:entry_body, name, segment_info) do
    zip_streamer.simulate_write(size_compressed)
  end

  self
end

#add_stored_entry(name:, size_uncompressed:, segment_info: nil) ⇒ Object

Add a fake entry to the archive, to see how big it is going to be in the end.

Parameters:

  • name (String)

    the name of the file (filenames are variable-width in the ZIP)

  • size_uncompressed (Fixnum)

    size of the uncompressed entry

  • segment_info (Object) (defaults to: nil)

    if you need to save anything to retrieve later from the Manifest, pass it here (like the URL of the file)

Returns:

  • self



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/zip_tricks/manifest.rb', line 44

def add_stored_entry(name:, size_uncompressed:, segment_info: nil)
  register_part(:entry_header, name, segment_info) do
    zip_streamer.add_stored_entry(name, size_uncompressed, C_fake_crc)
  end

  register_part(:entry_body, name, segment_info) do
    zip_streamer.simulate_write(size_uncompressed)
  end

  self
end