xlsxrb 0.1.17 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +256 -1
- data/README.md +111 -28
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +439 -139
- data/lib/xlsxrb/chart_builder.rb +540 -193
- data/lib/xlsxrb/dsl_helpers.rb +178 -73
- data/lib/xlsxrb/elements/cell.rb +197 -130
- data/lib/xlsxrb/elements/column.rb +88 -7
- data/lib/xlsxrb/elements/coordinate_access.rb +86 -59
- data/lib/xlsxrb/elements/image.rb +18 -19
- data/lib/xlsxrb/elements/metadata.rb +2379 -0
- data/lib/xlsxrb/elements/row.rb +84 -118
- data/lib/xlsxrb/elements/styles.rb +140 -90
- data/lib/xlsxrb/elements/types.rb +164 -22
- data/lib/xlsxrb/elements/workbook.rb +122 -140
- data/lib/xlsxrb/elements/worksheet.rb +451 -226
- data/lib/xlsxrb/elements.rb +1 -0
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +16 -5
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +14 -9
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +296 -34
- data/lib/xlsxrb/ooxml/reader/listeners/style_listeners.rb +17 -3
- data/lib/xlsxrb/ooxml/reader.rb +52 -50
- data/lib/xlsxrb/ooxml/styles_parser.rb +36 -5
- data/lib/xlsxrb/ooxml/utils.rb +2 -2
- data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
- data/lib/xlsxrb/ooxml/workbook_writer.rb +242 -109
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +63 -31
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +119 -56
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +33 -13
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +75 -15
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +18 -8
- data/lib/xlsxrb/ooxml/writer.rb +124 -44
- data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
- data/lib/xlsxrb/ooxml/zip_generator.rb +2 -0
- data/lib/xlsxrb/ooxml/zip_reader.rb +112 -25
- data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
- data/lib/xlsxrb/stream_row.rb +56 -81
- data/lib/xlsxrb/stream_sheet.rb +542 -273
- data/lib/xlsxrb/stream_writer.rb +1536 -870
- data/lib/xlsxrb/style_builder.rb +667 -221
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +406 -143
- data/lib/xlsxrb/worksheet_builder.rb +1009 -900
- data/lib/xlsxrb.rb +1179 -1056
- data/sig/generated/xlsxrb/chart_builder.rbs +385 -159
- data/sig/generated/xlsxrb/dsl_helpers.rbs +81 -15
- data/sig/generated/xlsxrb/elements/cell.rbs +78 -70
- data/sig/generated/xlsxrb/elements/column.rbs +55 -3
- data/sig/generated/xlsxrb/elements/coordinate_access.rbs +35 -29
- data/sig/generated/xlsxrb/elements/image.rbs +7 -11
- data/sig/generated/xlsxrb/elements/metadata.rbs +1574 -0
- data/sig/generated/xlsxrb/elements/row.rbs +39 -42
- data/sig/generated/xlsxrb/elements/styles.rbs +66 -66
- data/sig/generated/xlsxrb/elements/types.rbs +27 -0
- data/sig/generated/xlsxrb/elements/workbook.rbs +46 -99
- data/sig/generated/xlsxrb/elements/worksheet.rbs +121 -96
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +61 -4
- data/sig/generated/xlsxrb/ooxml/reader.rbs +2 -2
- data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/utils.rbs +3 -3
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +13 -1
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +6 -4
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +7 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +22 -13
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
- data/sig/generated/xlsxrb/ooxml/zip_generator.rbs +2 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +24 -9
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
- data/sig/generated/xlsxrb/stream_row.rbs +32 -33
- data/sig/generated/xlsxrb/stream_sheet.rbs +233 -181
- data/sig/generated/xlsxrb/stream_writer.rbs +961 -667
- data/sig/generated/xlsxrb/style_builder.rbs +402 -128
- data/sig/generated/xlsxrb/workbook_builder.rbs +223 -71
- data/sig/generated/xlsxrb/worksheet_builder.rbs +579 -453
- data/sig/generated/xlsxrb.rbs +205 -144
- data/sig/rexml.rbs +8 -0
- metadata +5 -5
- data/lib/xlsxrb/utils.rb +0 -291
- data/sig/generated/xlsxrb/utils.rbs +0 -184
|
@@ -46,6 +46,9 @@ module Xlsxrb
|
|
|
46
46
|
@max_uncompressed_size = max_uncompressed_size || ZipReader::MAX_UNCOMPRESSED_SIZE
|
|
47
47
|
@encoding = encoding
|
|
48
48
|
@buffer = +"".b
|
|
49
|
+
# Read offset into @buffer. Consuming by offset instead of slice! avoids moving the
|
|
50
|
+
# rest of the inflated chunk (often ten times the requested length) on every read.
|
|
51
|
+
@buffer_pos = 0
|
|
49
52
|
@bytes_read_compressed = 0
|
|
50
53
|
@total_uncompressed = 0
|
|
51
54
|
@eof = false
|
|
@@ -61,8 +64,9 @@ module Xlsxrb
|
|
|
61
64
|
check_bom_encoding!
|
|
62
65
|
|
|
63
66
|
if length.nil?
|
|
64
|
-
result = @buffer.b
|
|
67
|
+
result = @buffer.byteslice(@buffer_pos, @buffer.bytesize - @buffer_pos).b
|
|
65
68
|
@buffer = +"".b
|
|
69
|
+
@buffer_pos = 0
|
|
66
70
|
while (chunk = fetch_chunk(DEFAULT_CHUNK_SIZE))
|
|
67
71
|
result << chunk
|
|
68
72
|
end
|
|
@@ -72,21 +76,26 @@ module Xlsxrb
|
|
|
72
76
|
|
|
73
77
|
return "".force_encoding(@encoding || Encoding::BINARY) if length.zero?
|
|
74
78
|
|
|
75
|
-
while @buffer.bytesize < length && !@eof
|
|
76
|
-
chunk = fetch_chunk([length - @buffer.bytesize, DEFAULT_CHUNK_SIZE].max)
|
|
79
|
+
while @buffer.bytesize - @buffer_pos < length && !@eof
|
|
80
|
+
chunk = fetch_chunk([length - (@buffer.bytesize - @buffer_pos), DEFAULT_CHUNK_SIZE].max)
|
|
77
81
|
break unless chunk
|
|
78
82
|
|
|
83
|
+
compact_buffer
|
|
79
84
|
@buffer << chunk
|
|
80
85
|
end
|
|
81
86
|
|
|
82
|
-
|
|
87
|
+
available = @buffer.bytesize - @buffer_pos
|
|
88
|
+
return nil if available.zero? && @eof
|
|
83
89
|
|
|
84
|
-
sliced = @buffer.
|
|
85
|
-
|
|
90
|
+
sliced = @buffer.byteslice(@buffer_pos, [length, available].min)
|
|
91
|
+
@buffer_pos += sliced.bytesize
|
|
92
|
+
compact_buffer if @buffer_pos == @buffer.bytesize
|
|
93
|
+
sliced.force_encoding(@encoding) if @encoding
|
|
86
94
|
sliced
|
|
87
95
|
end
|
|
88
96
|
|
|
89
|
-
#: (?Integer chunk_size)
|
|
97
|
+
#: (?Integer chunk_size) { (String) -> void } -> void
|
|
98
|
+
#: (?Integer chunk_size) -> Enumerator[String, void]
|
|
90
99
|
def each_chunk(chunk_size = DEFAULT_CHUNK_SIZE, &block)
|
|
91
100
|
return enum_for(:each_chunk, chunk_size) unless block
|
|
92
101
|
raise IOError, "closed stream" if @closed
|
|
@@ -111,6 +120,7 @@ module Xlsxrb
|
|
|
111
120
|
@inflater&.close
|
|
112
121
|
@inflater = nil
|
|
113
122
|
@buffer.clear
|
|
123
|
+
@buffer_pos = 0
|
|
114
124
|
nil
|
|
115
125
|
end
|
|
116
126
|
|
|
@@ -121,11 +131,23 @@ module Xlsxrb
|
|
|
121
131
|
|
|
122
132
|
#: () -> bool
|
|
123
133
|
def eof?
|
|
124
|
-
@eof && @buffer.
|
|
134
|
+
@eof && @buffer.bytesize == @buffer_pos
|
|
125
135
|
end
|
|
126
136
|
|
|
127
137
|
private
|
|
128
138
|
|
|
139
|
+
# Drops the consumed prefix of @buffer.
|
|
140
|
+
def compact_buffer
|
|
141
|
+
return if @buffer_pos.zero?
|
|
142
|
+
|
|
143
|
+
if @buffer_pos == @buffer.bytesize
|
|
144
|
+
@buffer.clear
|
|
145
|
+
else
|
|
146
|
+
@buffer = @buffer.byteslice(@buffer_pos, @buffer.bytesize - @buffer_pos)
|
|
147
|
+
end
|
|
148
|
+
@buffer_pos = 0
|
|
149
|
+
end
|
|
150
|
+
|
|
129
151
|
def fetch_chunk(chunk_size)
|
|
130
152
|
return nil if @eof || @closed
|
|
131
153
|
|
|
@@ -233,7 +255,7 @@ module Xlsxrb
|
|
|
233
255
|
else
|
|
234
256
|
source
|
|
235
257
|
end
|
|
236
|
-
reader = new(io, max_uncompressed_size: max_uncompressed_size, should_close_io: should_close)
|
|
258
|
+
reader = new(io, max_uncompressed_size: max_uncompressed_size, should_close_io: should_close, filepath: (source if should_close))
|
|
237
259
|
if block_given?
|
|
238
260
|
begin
|
|
239
261
|
yield reader
|
|
@@ -245,11 +267,12 @@ module Xlsxrb
|
|
|
245
267
|
end
|
|
246
268
|
end
|
|
247
269
|
|
|
248
|
-
#: (untyped io, ?max_uncompressed_size: Integer, ?should_close_io: bool) -> void
|
|
249
|
-
def initialize(io, max_uncompressed_size: MAX_UNCOMPRESSED_SIZE, should_close_io: false)
|
|
270
|
+
#: (untyped io, ?max_uncompressed_size: Integer, ?should_close_io: bool, ?filepath: String?) -> void
|
|
271
|
+
def initialize(io, max_uncompressed_size: MAX_UNCOMPRESSED_SIZE, should_close_io: false, filepath: nil)
|
|
250
272
|
@raw_io = io
|
|
251
273
|
@max_uncompressed_size = max_uncompressed_size
|
|
252
274
|
@should_close_io = should_close_io
|
|
275
|
+
@filepath = filepath
|
|
253
276
|
@tempfile = nil
|
|
254
277
|
@io = nil
|
|
255
278
|
@catalog = nil
|
|
@@ -279,13 +302,21 @@ module Xlsxrb
|
|
|
279
302
|
cached ||= @cached_entries[entry.name] if @cached_entries&.key?(entry.name)
|
|
280
303
|
return normalize_text_encoding(cached) if cached
|
|
281
304
|
|
|
282
|
-
|
|
283
|
-
|
|
305
|
+
raw = if @should_close_io && @raw_io.respond_to?(:closed?) && @raw_io.closed? && @filepath
|
|
306
|
+
File.open(@filepath, "rb") do |f|
|
|
307
|
+
f.seek(entry.data_offset, IO::SEEK_SET)
|
|
308
|
+
f.read(entry.compressed_size)
|
|
309
|
+
end
|
|
310
|
+
else
|
|
311
|
+
io.seek(entry.data_offset, IO::SEEK_SET)
|
|
312
|
+
io.read(entry.compressed_size)
|
|
313
|
+
end
|
|
284
314
|
decompress(raw, entry.method)
|
|
285
315
|
end
|
|
286
316
|
|
|
287
317
|
# Yields (entry_name, data_string) for each file in the archive.
|
|
288
|
-
#: ()
|
|
318
|
+
#: () { (String, String?) -> void } -> void
|
|
319
|
+
#: () -> Enumerator[[String, String?], void]
|
|
289
320
|
def each_entry(&block)
|
|
290
321
|
return enum_for(:each_entry) unless block
|
|
291
322
|
|
|
@@ -319,15 +350,45 @@ module Xlsxrb
|
|
|
319
350
|
end
|
|
320
351
|
|
|
321
352
|
# Yields inflated binary chunks directly from the compressed stream.
|
|
322
|
-
#: (String entry_name, ?chunk_size: Integer, ?encoding: (Encoding | String | false | nil))
|
|
353
|
+
#: (String entry_name, ?chunk_size: Integer, ?encoding: (Encoding | String | false | nil)) { (String) -> void } -> void
|
|
354
|
+
#: (String entry_name, ?chunk_size: Integer, ?encoding: (Encoding | String | false | nil)) -> Enumerator[String, void]
|
|
323
355
|
def each_entry_chunk(entry_name, chunk_size: 65_536, encoding: nil, &block)
|
|
324
356
|
return enum_for(:each_entry_chunk, entry_name, chunk_size: chunk_size, encoding: encoding) unless block
|
|
325
357
|
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
358
|
+
if @should_close_io && @raw_io.respond_to?(:closed?) && @raw_io.closed? && @filepath
|
|
359
|
+
File.open(@filepath, "rb") do |f|
|
|
360
|
+
entry = lookup_entry(entry_name)
|
|
361
|
+
raise ZipError, "Entry not found in archive: #{entry_name}" unless entry
|
|
362
|
+
|
|
363
|
+
resolved_encoding = if encoding == false
|
|
364
|
+
nil
|
|
365
|
+
elsif encoding
|
|
366
|
+
encoding.is_a?(String) ? Encoding.find(encoding) : encoding
|
|
367
|
+
elsif entry_name.end_with?(".xml", ".rels") || entry.name.end_with?(".xml", ".rels")
|
|
368
|
+
Encoding::UTF_8
|
|
369
|
+
end
|
|
370
|
+
|
|
371
|
+
stream = EntryStreamIO.new(
|
|
372
|
+
f,
|
|
373
|
+
entry.data_offset,
|
|
374
|
+
entry.compressed_size,
|
|
375
|
+
entry.method,
|
|
376
|
+
max_uncompressed_size: @max_uncompressed_size,
|
|
377
|
+
encoding: resolved_encoding
|
|
378
|
+
)
|
|
379
|
+
begin
|
|
380
|
+
stream.each_chunk(chunk_size, &block)
|
|
381
|
+
ensure
|
|
382
|
+
stream.close
|
|
383
|
+
end
|
|
384
|
+
end
|
|
385
|
+
else
|
|
386
|
+
stream = open_entry_io(entry_name, encoding: encoding)
|
|
387
|
+
begin
|
|
388
|
+
stream.each_chunk(chunk_size, &block)
|
|
389
|
+
ensure
|
|
390
|
+
stream.close
|
|
391
|
+
end
|
|
331
392
|
end
|
|
332
393
|
end
|
|
333
394
|
|
|
@@ -442,6 +503,7 @@ module Xlsxrb
|
|
|
442
503
|
return nil unless eocd_data && eocd_data.bytesize == 18
|
|
443
504
|
|
|
444
505
|
_disk_num, _cd_disk, _disk_entries, total_entries, _cd_size, cd_offset, _comment_len = eocd_data.unpack("vvvvVVv")
|
|
506
|
+
total_entries, cd_offset = read_zip64_end_of_central_directory(io, eocd_pos, total_entries, cd_offset) if total_entries == 0xFFFF || cd_offset == 0xFFFF_FFFF
|
|
445
507
|
return nil if cd_offset > file_size
|
|
446
508
|
|
|
447
509
|
io.seek(cd_offset, IO::SEEK_SET)
|
|
@@ -509,6 +571,7 @@ module Xlsxrb
|
|
|
509
571
|
header = io.read(26)
|
|
510
572
|
break unless header && header.bytesize == 26
|
|
511
573
|
|
|
574
|
+
version_needed = header[0, 2].unpack1("v")
|
|
512
575
|
gp_flag = header[2, 2].unpack1("v")
|
|
513
576
|
method = header[4, 2].unpack1("v")
|
|
514
577
|
crc = header[10, 4].unpack1("V")
|
|
@@ -528,7 +591,8 @@ module Xlsxrb
|
|
|
528
591
|
has_data_descriptor = gp_flag.anybits?(0x08)
|
|
529
592
|
|
|
530
593
|
if has_data_descriptor && compressed_size.zero?
|
|
531
|
-
|
|
594
|
+
# A ZIP64 entry (version needed 4.5) carries 8-byte sizes in its data descriptor.
|
|
595
|
+
entry_data, = find_data_descriptor_stream(io, method, zip64: version_needed >= 45)
|
|
532
596
|
@cached_entries ||= {}
|
|
533
597
|
@cached_entries[norm_name] = entry_data
|
|
534
598
|
@cached_entries[entry_name] = entry_data if entry_name != norm_name
|
|
@@ -562,15 +626,17 @@ module Xlsxrb
|
|
|
562
626
|
result
|
|
563
627
|
end
|
|
564
628
|
|
|
565
|
-
def find_data_descriptor_stream(io, method)
|
|
629
|
+
def find_data_descriptor_stream(io, method, zip64: false)
|
|
566
630
|
if method == 8
|
|
567
631
|
inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
|
568
632
|
result = +""
|
|
569
633
|
chunk_size = 4096
|
|
634
|
+
fed = 0
|
|
570
635
|
begin
|
|
571
636
|
while !inflater.finished? && (chunk = io.read(chunk_size))
|
|
572
637
|
break if chunk.empty?
|
|
573
638
|
|
|
639
|
+
fed += chunk.bytesize
|
|
574
640
|
inflated = inflater.inflate(chunk)
|
|
575
641
|
limit = @max_uncompressed_size || MAX_UNCOMPRESSED_SIZE
|
|
576
642
|
raise ZipBombError, "ZIP bomb detected: Uncompressed size exceeds #{limit} bytes" if result.bytesize + inflated.bytesize > limit
|
|
@@ -580,19 +646,22 @@ module Xlsxrb
|
|
|
580
646
|
rescue Zlib::BufError, Zlib::DataError
|
|
581
647
|
# Inflation ended
|
|
582
648
|
ensure
|
|
583
|
-
|
|
649
|
+
# Ruby's Zlib::Inflate drops input past the end of the deflate stream (avail_in
|
|
650
|
+
# is 0), so rewind by what was read beyond the compressed length (total_in).
|
|
651
|
+
remaining_bytes = fed - inflater.total_in
|
|
584
652
|
inflater.close
|
|
585
653
|
end
|
|
586
654
|
|
|
587
655
|
io.seek(-remaining_bytes, IO::SEEK_CUR) if io.respond_to?(:seek) && remaining_bytes.positive?
|
|
588
656
|
|
|
589
657
|
desc_sig = io.read(4)
|
|
658
|
+
sizes_len = zip64 ? 16 : 8
|
|
590
659
|
if desc_sig == [0x50, 0x4B, 0x07, 0x08].pack("C4")
|
|
591
|
-
io.read(
|
|
660
|
+
io.read(4 + sizes_len)
|
|
592
661
|
elsif desc_sig == [0x50, 0x4B, 0x03, 0x04].pack("C4") || desc_sig == [0x50, 0x4B, 0x01, 0x02].pack("C4")
|
|
593
662
|
io.seek(-4, IO::SEEK_CUR) if io.respond_to?(:seek)
|
|
594
663
|
elsif desc_sig
|
|
595
|
-
io.read(
|
|
664
|
+
io.read(sizes_len)
|
|
596
665
|
end
|
|
597
666
|
|
|
598
667
|
[result, 0]
|
|
@@ -669,6 +738,24 @@ module Xlsxrb
|
|
|
669
738
|
end
|
|
670
739
|
end
|
|
671
740
|
|
|
741
|
+
# Reads the entry count and central directory offset from the ZIP64 end of central
|
|
742
|
+
# directory record, located through the locator just before the end of central directory.
|
|
743
|
+
# Returns the given values unchanged when the archive has no ZIP64 record.
|
|
744
|
+
#: (untyped io, Integer eocd_pos, Integer total_entries, Integer cd_offset) -> [Integer, Integer]
|
|
745
|
+
def read_zip64_end_of_central_directory(io, eocd_pos, total_entries, cd_offset)
|
|
746
|
+
return [total_entries, cd_offset] if eocd_pos < 20
|
|
747
|
+
|
|
748
|
+
io.seek(eocd_pos - 20, IO::SEEK_SET)
|
|
749
|
+
locator = io.read(20)
|
|
750
|
+
return [total_entries, cd_offset] unless locator && locator.bytesize == 20 && locator.start_with?("PK\x06\x07")
|
|
751
|
+
|
|
752
|
+
io.seek(locator.byteslice(8, 8).unpack1("Q<"), IO::SEEK_SET)
|
|
753
|
+
record = io.read(56)
|
|
754
|
+
return [total_entries, cd_offset] unless record && record.bytesize == 56 && record.start_with?("PK\x06\x06")
|
|
755
|
+
|
|
756
|
+
[record.byteslice(32, 8).unpack1("Q<"), record.byteslice(48, 8).unpack1("Q<")]
|
|
757
|
+
end
|
|
758
|
+
|
|
672
759
|
#: (String extra_data, Integer usize, Integer csize, Integer offset) -> [Integer, Integer, Integer]
|
|
673
760
|
def parse_zip64_extra(extra_data, usize, csize, offset)
|
|
674
761
|
pos = 0
|
|
@@ -8,8 +8,18 @@ module Xlsxrb
|
|
|
8
8
|
module Ooxml
|
|
9
9
|
# Writes ZIP archives using only stdlib (zlib).
|
|
10
10
|
# Supports streaming: entries are written sequentially, central directory at close.
|
|
11
|
+
#
|
|
12
|
+
# Sizes, offsets, and entry counts that do not fit the 32-bit (16-bit for counts)
|
|
13
|
+
# fields switch to ZIP64 records for that value only, so archives below those
|
|
14
|
+
# limits are written exactly as plain ZIP.
|
|
11
15
|
class ZipWriter
|
|
12
|
-
|
|
16
|
+
MAX_UINT32 = 0xFFFF_FFFF
|
|
17
|
+
MAX_UINT16 = 0xFFFF
|
|
18
|
+
VERSION_DEFAULT = 20
|
|
19
|
+
VERSION_ZIP64 = 45
|
|
20
|
+
DATA_DESCRIPTOR_FLAG = 0x0008
|
|
21
|
+
|
|
22
|
+
def self.open(target, compression_level: DEFAULT_COMPRESSION_LEVEL, &block)
|
|
13
23
|
io = target.is_a?(String) ? File.open(target, "wb") : target
|
|
14
24
|
writer = new(io, compression_level: compression_level)
|
|
15
25
|
if block
|
|
@@ -29,9 +39,10 @@ module Xlsxrb
|
|
|
29
39
|
|
|
30
40
|
# @param io [IO, StringIO] Destination IO stream.
|
|
31
41
|
# @param compression_level [Integer, nil] ZIP deflate compression level (0..9, or Zlib::DEFAULT_COMPRESSION).
|
|
42
|
+
# nil selects {Xlsxrb::DEFAULT_COMPRESSION_LEVEL}.
|
|
32
43
|
#: (untyped io, ?compression_level: Integer | nil) -> void
|
|
33
|
-
def initialize(io, compression_level:
|
|
34
|
-
level = compression_level.nil? ?
|
|
44
|
+
def initialize(io, compression_level: DEFAULT_COMPRESSION_LEVEL)
|
|
45
|
+
level = compression_level.nil? ? DEFAULT_COMPRESSION_LEVEL : compression_level
|
|
35
46
|
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))
|
|
36
47
|
|
|
37
48
|
@io = io
|
|
@@ -40,6 +51,9 @@ module Xlsxrb
|
|
|
40
51
|
@closed = false
|
|
41
52
|
@bytes_written = 0
|
|
42
53
|
@seekable = determine_seekable
|
|
54
|
+
# Values at or above this limit are written as ZIP64. 0xFFFFFFFF itself is the
|
|
55
|
+
# ZIP64 marker, so it cannot be stored in a 32-bit field.
|
|
56
|
+
@zip64_limit = MAX_UINT32
|
|
43
57
|
end
|
|
44
58
|
|
|
45
59
|
# Add a file entry with string content.
|
|
@@ -65,7 +79,7 @@ module Xlsxrb
|
|
|
65
79
|
|
|
66
80
|
@entry_path = path
|
|
67
81
|
@entry_offset = @bytes_written
|
|
68
|
-
@entry_gp_flag = @seekable ? 0 :
|
|
82
|
+
@entry_gp_flag = @seekable ? 0 : DATA_DESCRIPTOR_FLAG
|
|
69
83
|
@entry_deflater = Zlib::Deflate.new(@compression_level, -Zlib::MAX_WBITS)
|
|
70
84
|
@entry_crc = Zlib.crc32
|
|
71
85
|
@entry_uncompressed_size = 0
|
|
@@ -73,8 +87,11 @@ module Xlsxrb
|
|
|
73
87
|
@entry_seekable = @seekable
|
|
74
88
|
@has_current_entry = true
|
|
75
89
|
|
|
76
|
-
# Write local header (if unseekable, sizes/crc are 0 and bit 3 is set)
|
|
77
|
-
|
|
90
|
+
# Write local header (if unseekable, sizes/crc are 0 and bit 3 is set). Unseekable
|
|
91
|
+
# output cannot patch the header once the size is known, and Excel reads the data
|
|
92
|
+
# descriptor as 8-byte ZIP64 only when the local header states version 4.5, so such
|
|
93
|
+
# entries are declared ZIP64 up front.
|
|
94
|
+
write_local_header(path, 0, 0, 0, gp_flag: @entry_gp_flag, version: @seekable ? VERSION_DEFAULT : VERSION_ZIP64)
|
|
78
95
|
end
|
|
79
96
|
|
|
80
97
|
def write_data(str)
|
|
@@ -100,17 +117,20 @@ module Xlsxrb
|
|
|
100
117
|
@entry_deflater.close
|
|
101
118
|
|
|
102
119
|
final_crc = @entry_crc & 0xFFFFFFFF
|
|
120
|
+
zip64 = !@entry_seekable || zip64?(@entry_compressed_size) || zip64?(@entry_uncompressed_size)
|
|
103
121
|
|
|
104
|
-
if @entry_seekable
|
|
122
|
+
if @entry_seekable && !zip64
|
|
105
123
|
# Patch the local header if seekable
|
|
106
|
-
|
|
107
|
-
@io.seek(@entry_offset)
|
|
108
|
-
write_local_header(@entry_path, final_crc, @entry_compressed_size, @entry_uncompressed_size, gp_flag: 0, count_bytes: false)
|
|
109
|
-
@io.seek(current_pos)
|
|
124
|
+
patch_local_header { write_local_header(@entry_path, final_crc, @entry_compressed_size, @entry_uncompressed_size, gp_flag: 0, count_bytes: false) }
|
|
110
125
|
else
|
|
111
|
-
#
|
|
112
|
-
|
|
113
|
-
|
|
126
|
+
# Unseekable output always ends a streamed entry with an 8-byte data descriptor.
|
|
127
|
+
# On seekable output, an entry that outgrew 32-bit sizes falls back to one too,
|
|
128
|
+
# because the local header has no room for a ZIP64 extra field.
|
|
129
|
+
write_data_descriptor(final_crc, @entry_compressed_size, @entry_uncompressed_size, zip64: zip64)
|
|
130
|
+
if @entry_seekable
|
|
131
|
+
@entry_gp_flag = DATA_DESCRIPTOR_FLAG
|
|
132
|
+
patch_local_header { write_local_header(@entry_path, 0, 0, 0, gp_flag: @entry_gp_flag, count_bytes: false, version: VERSION_ZIP64) }
|
|
133
|
+
end
|
|
114
134
|
end
|
|
115
135
|
|
|
116
136
|
@entries << {
|
|
@@ -120,7 +140,8 @@ module Xlsxrb
|
|
|
120
140
|
uncompressed_size: @entry_uncompressed_size,
|
|
121
141
|
offset: @entry_offset,
|
|
122
142
|
gp_flag: @entry_gp_flag,
|
|
123
|
-
method: 8
|
|
143
|
+
method: 8,
|
|
144
|
+
zip64_sizes: !@entry_seekable
|
|
124
145
|
}
|
|
125
146
|
end
|
|
126
147
|
|
|
@@ -174,6 +195,25 @@ module Xlsxrb
|
|
|
174
195
|
end
|
|
175
196
|
end
|
|
176
197
|
|
|
198
|
+
#: (Integer value) -> bool
|
|
199
|
+
def zip64?(value)
|
|
200
|
+
value >= @zip64_limit
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
#: () { () -> void } -> void
|
|
204
|
+
def patch_local_header
|
|
205
|
+
current_pos = @io.is_a?(StringIO) ? @io.pos : @io.tell
|
|
206
|
+
@io.seek(@entry_offset)
|
|
207
|
+
yield
|
|
208
|
+
@io.seek(current_pos)
|
|
209
|
+
end
|
|
210
|
+
|
|
211
|
+
# Data descriptor: signature + crc32 + compressed size + uncompressed size.
|
|
212
|
+
# ZIP64 entries store both sizes as 8-byte values (APPNOTE 4.3.9.1).
|
|
213
|
+
def write_data_descriptor(crc, compressed_size, uncompressed_size, zip64:)
|
|
214
|
+
write_bytes([0x08074B50, crc, compressed_size, uncompressed_size].pack(zip64 ? "VVQ<Q<" : "VVVV"))
|
|
215
|
+
end
|
|
216
|
+
|
|
177
217
|
def write_bytes(data)
|
|
178
218
|
return if data.nil? || data.empty?
|
|
179
219
|
|
|
@@ -209,11 +249,18 @@ module Xlsxrb
|
|
|
209
249
|
end
|
|
210
250
|
end
|
|
211
251
|
|
|
212
|
-
def write_local_header(path, crc, compressed_size, uncompressed_size, gp_flag: 0, count_bytes: true, method: 8)
|
|
252
|
+
def write_local_header(path, crc, compressed_size, uncompressed_size, gp_flag: 0, count_bytes: true, method: 8, version: VERSION_DEFAULT)
|
|
213
253
|
name_bytes = path.encode("UTF-8").b
|
|
254
|
+
extra = "".b
|
|
255
|
+
if zip64?(compressed_size) || zip64?(uncompressed_size)
|
|
256
|
+
# A ZIP64 extra field in a local header must carry both sizes (APPNOTE 4.5.3).
|
|
257
|
+
extra = [0x0001, 16, uncompressed_size, compressed_size].pack("vvQ<Q<")
|
|
258
|
+
compressed_size = uncompressed_size = MAX_UINT32
|
|
259
|
+
version = VERSION_ZIP64
|
|
260
|
+
end
|
|
214
261
|
header = [
|
|
215
262
|
0x04034B50, # local file header signature
|
|
216
|
-
|
|
263
|
+
version, # version needed (2.0, or 4.5 for ZIP64)
|
|
217
264
|
gp_flag, # general purpose bit flag (0x0008 if data descriptor follows)
|
|
218
265
|
method, # compression method (0 = store, 8 = deflate)
|
|
219
266
|
0, # last mod file time
|
|
@@ -222,15 +269,17 @@ module Xlsxrb
|
|
|
222
269
|
compressed_size,
|
|
223
270
|
uncompressed_size,
|
|
224
271
|
name_bytes.bytesize,
|
|
225
|
-
|
|
272
|
+
extra.bytesize # extra field length
|
|
226
273
|
].pack("VvvvvvVVVvv")
|
|
227
274
|
|
|
228
275
|
if count_bytes
|
|
229
276
|
write_bytes(header)
|
|
230
277
|
write_bytes(name_bytes)
|
|
278
|
+
write_bytes(extra)
|
|
231
279
|
else
|
|
232
280
|
@io.write(header)
|
|
233
281
|
@io.write(name_bytes)
|
|
282
|
+
@io.write(extra)
|
|
234
283
|
end
|
|
235
284
|
end
|
|
236
285
|
|
|
@@ -238,43 +287,83 @@ module Xlsxrb
|
|
|
238
287
|
size = 0
|
|
239
288
|
@entries.each do |entry|
|
|
240
289
|
name_bytes = entry[:path].encode("UTF-8").b
|
|
290
|
+
uncompressed_size = entry[:uncompressed_size]
|
|
291
|
+
compressed_size = entry[:compressed_size]
|
|
292
|
+
offset = entry[:offset]
|
|
293
|
+
# The ZIP64 extra field lists only the values whose 32-bit field holds
|
|
294
|
+
# 0xFFFFFFFF, in this fixed order (APPNOTE 4.5.3).
|
|
295
|
+
# Entries declared ZIP64 up front (streamed to unseekable output) list both sizes.
|
|
296
|
+
zip64_sizes = entry[:zip64_sizes]
|
|
297
|
+
usize64 = zip64_sizes || zip64?(uncompressed_size)
|
|
298
|
+
csize64 = zip64_sizes || zip64?(compressed_size)
|
|
299
|
+
zip64_values = []
|
|
300
|
+
zip64_values << uncompressed_size if usize64
|
|
301
|
+
zip64_values << compressed_size if csize64
|
|
302
|
+
zip64_values << offset if zip64?(offset)
|
|
303
|
+
extra = zip64_values.empty? ? "".b : [0x0001, zip64_values.size * 8, *zip64_values].pack("vvQ<*")
|
|
304
|
+
version = zip64_values.empty? ? VERSION_DEFAULT : VERSION_ZIP64
|
|
241
305
|
header = [
|
|
242
306
|
0x02014B50, # central directory file header signature
|
|
243
|
-
|
|
244
|
-
|
|
307
|
+
version, # version made by
|
|
308
|
+
version, # version needed
|
|
245
309
|
entry[:gp_flag] || 0, # general purpose bit flag
|
|
246
310
|
entry[:method] || 8, # compression method
|
|
247
311
|
0, # last mod file time
|
|
248
312
|
33, # last mod file date
|
|
249
313
|
entry[:crc32],
|
|
250
|
-
|
|
251
|
-
|
|
314
|
+
csize64 ? MAX_UINT32 : compressed_size,
|
|
315
|
+
usize64 ? MAX_UINT32 : uncompressed_size,
|
|
252
316
|
name_bytes.bytesize,
|
|
253
|
-
|
|
317
|
+
extra.bytesize, # extra field length
|
|
254
318
|
0, # file comment length
|
|
255
319
|
0, # disk number start
|
|
256
320
|
0, # internal file attributes
|
|
257
321
|
0, # external file attributes
|
|
258
|
-
|
|
322
|
+
zip64?(offset) ? MAX_UINT32 : offset # relative offset of local header
|
|
259
323
|
].pack("VvvvvvvVVVvvvvvVV")
|
|
260
324
|
|
|
261
325
|
write_bytes(header)
|
|
262
326
|
write_bytes(name_bytes)
|
|
263
|
-
|
|
327
|
+
write_bytes(extra)
|
|
328
|
+
size += header.bytesize + name_bytes.bytesize + extra.bytesize
|
|
264
329
|
end
|
|
265
330
|
size
|
|
266
331
|
end
|
|
267
332
|
|
|
268
333
|
def write_end_of_central_directory(cd_offset, cd_size)
|
|
334
|
+
count = @entries.size
|
|
335
|
+
zip64_count = count >= MAX_UINT16
|
|
336
|
+
if zip64_count || zip64?(cd_size) || zip64?(cd_offset)
|
|
337
|
+
zip64_eocd_offset = @bytes_written
|
|
338
|
+
write_bytes([
|
|
339
|
+
0x06064B50, # ZIP64 end of central dir signature
|
|
340
|
+
44, # size of the remaining record
|
|
341
|
+
VERSION_ZIP64, # version made by
|
|
342
|
+
VERSION_ZIP64, # version needed
|
|
343
|
+
0, # disk number
|
|
344
|
+
0, # disk with central directory
|
|
345
|
+
count, # entries on this disk
|
|
346
|
+
count, # total entries
|
|
347
|
+
cd_size, # size of central directory
|
|
348
|
+
cd_offset # offset of central directory
|
|
349
|
+
].pack("VQ<vvVVQ<Q<Q<Q<"))
|
|
350
|
+
write_bytes([
|
|
351
|
+
0x07064B50, # ZIP64 end of central dir locator signature
|
|
352
|
+
0, # disk with ZIP64 end of central dir
|
|
353
|
+
zip64_eocd_offset, # offset of ZIP64 end of central dir
|
|
354
|
+
1 # total number of disks
|
|
355
|
+
].pack("VVQ<V"))
|
|
356
|
+
end
|
|
357
|
+
|
|
269
358
|
eocd = [
|
|
270
359
|
0x06054B50, # end of central dir signature
|
|
271
360
|
0, # disk number
|
|
272
361
|
0, # disk with central directory
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
cd_size,
|
|
276
|
-
cd_offset,
|
|
277
|
-
0
|
|
362
|
+
zip64_count ? MAX_UINT16 : count, # entries on this disk
|
|
363
|
+
zip64_count ? MAX_UINT16 : count, # total entries
|
|
364
|
+
zip64?(cd_size) ? MAX_UINT32 : cd_size, # size of central directory
|
|
365
|
+
zip64?(cd_offset) ? MAX_UINT32 : cd_offset, # offset of central directory
|
|
366
|
+
0 # comment length
|
|
278
367
|
].pack("VvvvvVVv")
|
|
279
368
|
write_bytes(eocd)
|
|
280
369
|
end
|