Class: Xlsxrb::Ooxml::ZipGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxrb/ooxml/zip_generator.rb

Overview

Generates a simple ZIP file using only the standard library.

Instance Method Summary collapse

Constructor Details

#initialize(filepath) ⇒ ZipGenerator

Returns a new instance of ZipGenerator.



11
12
13
14
15
# File 'lib/xlsxrb/ooxml/zip_generator.rb', line 11

def initialize(filepath)
  @filepath = filepath
  @entries = []
  @file = nil
end

Instance Method Details

#add_entry(filepath, content) ⇒ Object

Adds an entry to the ZIP archive.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xlsxrb/ooxml/zip_generator.rb', line 18

def add_entry(filepath, content)
  compressed_data = compress(content)
  @entries << {
    path: filepath,
    content: content,
    compressed_data: compressed_data,
    crc32: crc32(content),
    uncompressed_size: content.bytesize,
    compressed_size: compressed_data.bytesize,
    mtime: Time.now
  }
end

#generateObject

Generates the ZIP file.



32
33
34
35
36
37
38
# File 'lib/xlsxrb/ooxml/zip_generator.rb', line 32

def generate
  File.open(@filepath, "wb") do |f|
    @file = f
    write_entries
    write_central_directory
  end
end