Class: OPA::ZipIO::Writer
- Inherits:
-
Object
- Object
- OPA::ZipIO::Writer
- Defined in:
- lib/opa/zip_io.rb
Overview
Builds a ZIP archive in memory and returns the bytes.
Defined Under Namespace
Classes: Entry
Instance Method Summary collapse
- #add_entry(name, data) ⇒ Object
-
#initialize ⇒ Writer
constructor
A new instance of Writer.
- #to_bytes ⇒ Object
Constructor Details
#initialize ⇒ Writer
Returns a new instance of Writer.
15 16 17 |
# File 'lib/opa/zip_io.rb', line 15 def initialize @entries = [] end |
Instance Method Details
#add_entry(name, data) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/opa/zip_io.rb', line 19 def add_entry(name, data) data = data.b crc32 = Zlib.crc32(data) deflater = Zlib::Deflate.new(Zlib::DEFAULT_COMPRESSION, -Zlib::MAX_WBITS) compressed = deflater.deflate(data, Zlib::FINISH) deflater.close @entries << Entry.new(name, data, crc32, compressed, nil) end |
#to_bytes ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/opa/zip_io.rb', line 28 def to_bytes io = StringIO.new io.set_encoding(Encoding::BINARY) # Write local file headers + data @entries.each do |entry| entry.offset = io.pos write_local_header(io, entry) io.write(entry.compressed) end # Central directory cd_offset = io.pos @entries.each { |entry| write_cd_header(io, entry) } cd_size = io.pos - cd_offset # End of central directory write_eocd(io, @entries.size, cd_size, cd_offset) io.string end |