class Xlsxrb::Ooxml::ZipWriter
Writes ZIP archives using only stdlib (zlib). Supports streaming: entries are written sequentially, central directory at close.
Sizes, offsets, and entry counts that do not fit the 32-bit (16-bit for counts) fields switch to ZIP64 records for that value only, so archives below those limits are written exactly as plain ZIP.
Constants
- DATA_DESCRIPTOR_FLAG
- MAX_UINT16
- MAX_UINT32
- UTF8_FLAG
- VERSION_DEFAULT
- VERSION_ZIP64
Attributes
untyped
@return [Integer] Deflate compression level (0..9).
Public Class Methods
(untyped io, ?compression_level: Integer | nil) → void
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 45 def initialize(io, compression_level: DEFAULT_COMPRESSION_LEVEL) level = compression_level.nil? ? DEFAULT_COMPRESSION_LEVEL : compression_level raise ArgumentError, "Invalid compression_level: #{compression_level.inspect}. Must be an Integer between 0 and 9 (or Zlib::DEFAULT_COMPRESSION)." unless level.is_a?(Integer) && (level == Zlib::DEFAULT_COMPRESSION || (0..9).cover?(level)) @io = io @compression_level = level @entries = [] @closed = false @bytes_written = 0 @seekable = determine_seekable # Values at or above this limit are written as ZIP64. 0xFFFFFFFF itself is the # ZIP64 marker, so it cannot be stored in a 32-bit field. @zip64_limit = MAX_UINT32 end
@param io [IO, StringIO] Destination IO stream. @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION).
nil selects {Xlsxrb::DEFAULT_COMPRESSION_LEVEL}.
(untyped target, ?compression_level: untyped) ?{ (?) → untyped } → untyped
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 23 def self.open(target, compression_level: DEFAULT_COMPRESSION_LEVEL, &block) io = target.is_a?(String) ? File.open(target, "wb") : target writer = new(io, compression_level: compression_level) if block begin yield writer ensure writer.close io.close if target.is_a?(String) end else writer end end
Public Instance Methods
(untyped path, untyped content) → untyped
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 69 def add_binary_entry(path, content) raise "ZipWriter is closed" if @closed content_bytes = content.b write_raw_entry(path, content_bytes) end
Add a file entry with binary content (no encoding conversion).
(untyped path, untyped content) → untyped
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 61 def add_entry(path, content) raise "ZipWriter is closed" if @closed content_bytes = content.encode("UTF-8").b write_raw_entry(path, content_bytes) end
Add a file entry with string content.
() → untyped
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 177 def close return if @closed finish_entry if @current_entry @closed = true cd_offset = @bytes_written cd_size = write_central_directory write_end_of_central_directory(cd_offset, cd_size) end
(String path, untyped source_io, data_offset: Integer, compressed_size: Integer, uncompressed_size: Integer, crc32: Integer, ?method: Integer) → void
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 152 def copy_raw_entry(path, source_io, data_offset:, compressed_size:, uncompressed_size:, crc32:, method: 8) raise "ZipWriter is closed" if @closed validate_entry_name!(path) gp_flag = entry_gp_flag(path) offset = @bytes_written write_local_header(path, crc32, compressed_size, uncompressed_size, gp_flag: gp_flag, method: method) if compressed_size.positive? source_io.seek(data_offset, IO::SEEK_SET) IO.copy_stream(source_io, @io, compressed_size) @bytes_written += compressed_size end @entries << { path: path, crc32: crc32, compressed_size: compressed_size, uncompressed_size: uncompressed_size, offset: offset, gp_flag: gp_flag, method: method } end
Copies an entry from an existing archive IO without decompressing or recompressing.
() → untyped
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 110 def finish_entry raise "No entry started" unless @has_current_entry @has_current_entry = false # Flush remaining deflate data remaining = @entry_deflater.finish @entry_compressed_size += remaining.bytesize write_bytes(remaining) @entry_deflater.close final_crc = @entry_crc & 0xFFFFFFFF zip64 = !@entry_seekable || zip64?(@entry_compressed_size) || zip64?(@entry_uncompressed_size) if @entry_seekable && !zip64 # Patch the local header if seekable patch_local_header { write_local_header(@entry_path, final_crc, @entry_compressed_size, @entry_uncompressed_size, gp_flag: @entry_gp_flag, count_bytes: false) } else # Unseekable output always ends a streamed entry with an 8-byte data descriptor. # On seekable output, an entry that outgrew 32-bit sizes falls back to one too, # because the local header has no room for a ZIP64 extra field. write_data_descriptor(final_crc, @entry_compressed_size, @entry_uncompressed_size, zip64: zip64) if @entry_seekable @entry_gp_flag |= DATA_DESCRIPTOR_FLAG patch_local_header { write_local_header(@entry_path, 0, 0, 0, gp_flag: @entry_gp_flag, count_bytes: false, version: VERSION_ZIP64) } end end @entries << { path: @entry_path, crc32: final_crc, compressed_size: @entry_compressed_size, uncompressed_size: @entry_uncompressed_size, offset: @entry_offset, gp_flag: @entry_gp_flag, method: 8, zip64_sizes: !@entry_seekable } end
(untyped path) → untyped
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 78 def start_entry(path) raise "ZipWriter is closed" if @closed validate_entry_name!(path) @entry_path = path @entry_offset = @bytes_written @entry_gp_flag = entry_gp_flag(path, @seekable ? 0 : DATA_DESCRIPTOR_FLAG) @entry_deflater = Zlib::Deflate.new(@compression_level, -Zlib::MAX_WBITS) @entry_crc = Zlib.crc32 @entry_uncompressed_size = 0 @entry_compressed_size = 0 @entry_seekable = @seekable @has_current_entry = true # Write local header (if unseekable, sizes/crc are 0 and bit 3 is set). Unseekable # output cannot patch the header once the size is known, and Excel reads the data # descriptor as 8-byte ZIP64 only when the local header states version 4.5, so such # entries are declared ZIP64 up front. write_local_header(path, 0, 0, 0, gp_flag: @entry_gp_flag, version: @seekable ? VERSION_DEFAULT : VERSION_ZIP64) end
Write a string directly into the current ZIP entry stream. Use start_entry / write_data / finish_entry for true streaming.
(untyped str) → untyped
Source
# File lib/xlsxrb/ooxml/zip_writer.rb, line 99 def write_data(str) raise "No entry started" unless @has_current_entry return if str.empty? @entry_crc = Zlib.crc32(str, @entry_crc) @entry_uncompressed_size += str.bytesize compressed = @entry_deflater.deflate(str) @entry_compressed_size += compressed.bytesize write_bytes(compressed) end