xlsxrb 0.1.18 → 0.2.1
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 +200 -0
- data/README.md +35 -12
- data/lib/ruby_lsp/xlsxrb/completion_listener.rb +410 -80
- data/lib/xlsxrb/chart_builder.rb +251 -386
- data/lib/xlsxrb/dsl_helpers.rb +169 -68
- data/lib/xlsxrb/elements/cell.rb +37 -157
- data/lib/xlsxrb/elements/column.rb +11 -19
- data/lib/xlsxrb/elements/coordinate_access.rb +15 -86
- data/lib/xlsxrb/elements/image.rb +5 -72
- data/lib/xlsxrb/elements/metadata.rb +463 -817
- data/lib/xlsxrb/elements/row.rb +27 -145
- data/lib/xlsxrb/elements/styles.rb +30 -88
- data/lib/xlsxrb/elements/types.rb +6 -99
- data/lib/xlsxrb/elements/workbook.rb +19 -126
- data/lib/xlsxrb/elements/worksheet.rb +45 -316
- data/lib/xlsxrb/ooxml/reader/listeners/core_listeners.rb +4 -1
- data/lib/xlsxrb/ooxml/reader/listeners/drawing_listeners.rb +7 -7
- data/lib/xlsxrb/ooxml/reader/listeners/feature_listeners.rb +32 -17
- data/lib/xlsxrb/ooxml/reader.rb +49 -51
- data/lib/xlsxrb/ooxml/styles_parser.rb +26 -2
- data/lib/xlsxrb/ooxml/utils.rb +15 -2
- data/lib/xlsxrb/ooxml/workbook_parser.rb +0 -1
- data/lib/xlsxrb/ooxml/workbook_writer.rb +184 -94
- data/lib/xlsxrb/ooxml/worksheet_parser.rb +61 -29
- data/lib/xlsxrb/ooxml/worksheet_writer.rb +353 -261
- data/lib/xlsxrb/ooxml/writer/drawing_xml.rb +28 -11
- data/lib/xlsxrb/ooxml/writer/features_xml.rb +62 -16
- data/lib/xlsxrb/ooxml/writer/styles_xml.rb +1 -1
- data/lib/xlsxrb/ooxml/writer.rb +106 -42
- data/lib/xlsxrb/ooxml/xml_builder.rb +18 -0
- data/lib/xlsxrb/ooxml/xml_parser.rb +78 -5
- data/lib/xlsxrb/ooxml/zip_reader.rb +65 -15
- data/lib/xlsxrb/ooxml/zip_writer.rb +119 -30
- data/lib/xlsxrb/stream_row.rb +14 -118
- data/lib/xlsxrb/stream_sheet.rb +14 -261
- data/lib/xlsxrb/stream_writer.rb +807 -1537
- data/lib/xlsxrb/style_builder.rb +715 -593
- data/lib/xlsxrb/version.rb +1 -1
- data/lib/xlsxrb/workbook_builder.rb +403 -286
- data/lib/xlsxrb/worksheet_builder.rb +1297 -1458
- data/lib/xlsxrb.rb +281 -1079
- data/sig/generated/xlsxrb/chart_builder.rbs +202 -205
- data/sig/generated/xlsxrb/dsl_helpers.rbs +92 -16
- data/sig/generated/xlsxrb/elements/cell.rbs +12 -76
- data/sig/generated/xlsxrb/elements/column.rbs +0 -7
- data/sig/generated/xlsxrb/elements/coordinate_access.rbs +5 -41
- data/sig/generated/xlsxrb/elements/image.rbs +3 -29
- data/sig/generated/xlsxrb/elements/metadata.rbs +138 -151
- data/sig/generated/xlsxrb/elements/row.rbs +10 -53
- data/sig/generated/xlsxrb/elements/styles.rbs +1 -39
- data/sig/generated/xlsxrb/elements/workbook.rbs +14 -78
- data/sig/generated/xlsxrb/elements/worksheet.rbs +5 -121
- data/sig/generated/xlsxrb/ooxml/reader/listeners/feature_listeners.rbs +0 -4
- data/sig/generated/xlsxrb/ooxml/reader.rbs +0 -3
- data/sig/generated/xlsxrb/ooxml/styles_parser.rbs +1 -1
- data/sig/generated/xlsxrb/ooxml/utils.rbs +13 -3
- data/sig/generated/xlsxrb/ooxml/workbook_writer.rbs +17 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_parser.rbs +16 -0
- data/sig/generated/xlsxrb/ooxml/worksheet_writer.rbs +58 -3
- data/sig/generated/xlsxrb/ooxml/writer/features_xml.rbs +10 -0
- data/sig/generated/xlsxrb/ooxml/writer.rbs +20 -14
- data/sig/generated/xlsxrb/ooxml/xml_builder.rbs +12 -0
- data/sig/generated/xlsxrb/ooxml/xml_parser.rbs +70 -0
- data/sig/generated/xlsxrb/ooxml/zip_reader.rbs +22 -7
- data/sig/generated/xlsxrb/ooxml/zip_writer.rbs +26 -1
- data/sig/generated/xlsxrb/stream_row.rbs +9 -44
- data/sig/generated/xlsxrb/stream_sheet.rbs +1 -158
- data/sig/generated/xlsxrb/stream_writer.rbs +491 -974
- data/sig/generated/xlsxrb/style_builder.rbs +464 -239
- data/sig/generated/xlsxrb/workbook_builder.rbs +124 -139
- data/sig/generated/xlsxrb/worksheet_builder.rbs +922 -686
- data/sig/generated/xlsxrb.rbs +56 -188
- data/sig/rexml.rbs +8 -0
- metadata +3 -5
- data/lib/xlsxrb/utils.rb +0 -251
- data/sig/generated/xlsxrb/utils.rbs +0 -200
|
@@ -17,15 +17,20 @@ begin
|
|
|
17
17
|
end
|
|
18
18
|
end
|
|
19
19
|
if defined?(REXML::Security)
|
|
20
|
+
# REXML keeps these limits in class variables, which non-main Ractors cannot read.
|
|
21
|
+
# Move them to module instance variables, carrying over the values the application
|
|
22
|
+
# has already set (e.g. 0 to disable entity expansion).
|
|
23
|
+
current_limit = REXML::Security.entity_expansion_limit
|
|
24
|
+
current_text_limit = REXML::Security.entity_expansion_text_limit
|
|
20
25
|
prev_verbose = $VERBOSE
|
|
21
26
|
$VERBOSE = nil
|
|
22
27
|
begin
|
|
23
28
|
REXML::Security.instance_eval do
|
|
24
|
-
@entity_expansion_limit =
|
|
25
|
-
@entity_expansion_text_limit =
|
|
29
|
+
@entity_expansion_limit = current_limit
|
|
30
|
+
@entity_expansion_text_limit = current_text_limit
|
|
26
31
|
|
|
27
32
|
def self.entity_expansion_limit
|
|
28
|
-
@entity_expansion_limit
|
|
33
|
+
@entity_expansion_limit
|
|
29
34
|
end
|
|
30
35
|
|
|
31
36
|
def self.entity_expansion_limit=(val)
|
|
@@ -33,7 +38,7 @@ begin
|
|
|
33
38
|
end
|
|
34
39
|
|
|
35
40
|
def self.entity_expansion_text_limit
|
|
36
|
-
@entity_expansion_text_limit
|
|
41
|
+
@entity_expansion_text_limit
|
|
37
42
|
end
|
|
38
43
|
|
|
39
44
|
def self.entity_expansion_text_limit=(val)
|
|
@@ -57,11 +62,79 @@ module Xlsxrb
|
|
|
57
62
|
module XmlParser
|
|
58
63
|
# Parses XML string using SAX2, calling the given listener.
|
|
59
64
|
def self.parse(xml_string, listener)
|
|
60
|
-
parser =
|
|
65
|
+
parser = sax2_parser(xml_string)
|
|
61
66
|
parser.listen(listener)
|
|
62
67
|
parser.parse
|
|
63
68
|
end
|
|
64
69
|
|
|
70
|
+
# Returns a SAX2 parser whose listeners receive attribute values with XML entities
|
|
71
|
+
# decoded. REXML's SAX2 parser decodes text but passes attribute values as written, so
|
|
72
|
+
# formatCode=""$"0" would otherwise arrive with the " references.
|
|
73
|
+
#: (String xml_string) -> REXML::Parsers::SAX2Parser
|
|
74
|
+
def self.sax2_parser(xml_string)
|
|
75
|
+
DecodingSAX2Parser.new(xml_string)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# SAX2 parser that wraps each listener in an AttributeDecoder.
|
|
79
|
+
class DecodingSAX2Parser < REXML::Parsers::SAX2Parser
|
|
80
|
+
#: (*untyped args) ?{ (*untyped) -> untyped } -> untyped
|
|
81
|
+
def listen(*args, &)
|
|
82
|
+
return super if args.size != 1 || args.first.is_a?(Symbol) || args.first.is_a?(Array) || block_given?
|
|
83
|
+
|
|
84
|
+
super(AttributeDecoder.new(args.first))
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Forwards SAX2 events to a listener, decoding entity references in attribute values.
|
|
89
|
+
class AttributeDecoder
|
|
90
|
+
#: (untyped listener) -> void
|
|
91
|
+
def initialize(listener)
|
|
92
|
+
@listener = listener
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Plain methods rather than define_method blocks, which Ractors cannot share.
|
|
96
|
+
#: (*untyped args) -> untyped
|
|
97
|
+
def start_document(*args) = @listener.start_document(*args)
|
|
98
|
+
#: (*untyped args) -> untyped
|
|
99
|
+
def end_document(*args) = @listener.end_document(*args)
|
|
100
|
+
#: (*untyped args) -> untyped
|
|
101
|
+
def start_prefix_mapping(*args) = @listener.start_prefix_mapping(*args)
|
|
102
|
+
#: (*untyped args) -> untyped
|
|
103
|
+
def end_prefix_mapping(*args) = @listener.end_prefix_mapping(*args)
|
|
104
|
+
#: (*untyped args) -> untyped
|
|
105
|
+
def end_element(*args) = @listener.end_element(*args)
|
|
106
|
+
#: (*untyped args) -> untyped
|
|
107
|
+
def characters(*args) = @listener.characters(*args)
|
|
108
|
+
#: (*untyped args) -> untyped
|
|
109
|
+
def processing_instruction(*args) = @listener.processing_instruction(*args)
|
|
110
|
+
#: (*untyped args) -> untyped
|
|
111
|
+
def doctype(*args) = @listener.doctype(*args)
|
|
112
|
+
#: (*untyped args) -> untyped
|
|
113
|
+
def attlistdecl(*args) = @listener.attlistdecl(*args)
|
|
114
|
+
#: (*untyped args) -> untyped
|
|
115
|
+
def elementdecl(*args) = @listener.elementdecl(*args)
|
|
116
|
+
#: (*untyped args) -> untyped
|
|
117
|
+
def entitydecl(*args) = @listener.entitydecl(*args)
|
|
118
|
+
#: (*untyped args) -> untyped
|
|
119
|
+
def notationdecl(*args) = @listener.notationdecl(*args)
|
|
120
|
+
#: (*untyped args) -> untyped
|
|
121
|
+
def cdata(*args) = @listener.cdata(*args)
|
|
122
|
+
#: (*untyped args) -> untyped
|
|
123
|
+
def xmldecl(*args) = @listener.xmldecl(*args)
|
|
124
|
+
#: (*untyped args) -> untyped
|
|
125
|
+
def comment(*args) = @listener.comment(*args)
|
|
126
|
+
#: (*untyped args) -> untyped
|
|
127
|
+
def progress(*args) = @listener.progress(*args)
|
|
128
|
+
|
|
129
|
+
#: (String? uri, String local_name, String qname, Hash[String, String] attributes) -> untyped
|
|
130
|
+
def start_element(uri, local_name, qname, attributes)
|
|
131
|
+
if attributes.each_value.any? { |value| value.include?("&") }
|
|
132
|
+
attributes = attributes.transform_values { |value| value.include?("&") ? REXML::Text.unnormalize(value) : value }
|
|
133
|
+
end
|
|
134
|
+
@listener.start_element(uri, local_name, qname, attributes)
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
|
|
65
138
|
# Base listener with unmapped-data collection support.
|
|
66
139
|
class BaseListener
|
|
67
140
|
include REXML::SAX2Listener
|
|
@@ -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
|
|
|
@@ -293,7 +315,8 @@ module Xlsxrb
|
|
|
293
315
|
end
|
|
294
316
|
|
|
295
317
|
# Yields (entry_name, data_string) for each file in the archive.
|
|
296
|
-
#: ()
|
|
318
|
+
#: () { (String, String?) -> void } -> void
|
|
319
|
+
#: () -> Enumerator[[String, String?], void]
|
|
297
320
|
def each_entry(&block)
|
|
298
321
|
return enum_for(:each_entry) unless block
|
|
299
322
|
|
|
@@ -327,7 +350,8 @@ module Xlsxrb
|
|
|
327
350
|
end
|
|
328
351
|
|
|
329
352
|
# Yields inflated binary chunks directly from the compressed stream.
|
|
330
|
-
#: (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]
|
|
331
355
|
def each_entry_chunk(entry_name, chunk_size: 65_536, encoding: nil, &block)
|
|
332
356
|
return enum_for(:each_entry_chunk, entry_name, chunk_size: chunk_size, encoding: encoding) unless block
|
|
333
357
|
|
|
@@ -479,6 +503,7 @@ module Xlsxrb
|
|
|
479
503
|
return nil unless eocd_data && eocd_data.bytesize == 18
|
|
480
504
|
|
|
481
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
|
|
482
507
|
return nil if cd_offset > file_size
|
|
483
508
|
|
|
484
509
|
io.seek(cd_offset, IO::SEEK_SET)
|
|
@@ -546,6 +571,7 @@ module Xlsxrb
|
|
|
546
571
|
header = io.read(26)
|
|
547
572
|
break unless header && header.bytesize == 26
|
|
548
573
|
|
|
574
|
+
version_needed = header[0, 2].unpack1("v")
|
|
549
575
|
gp_flag = header[2, 2].unpack1("v")
|
|
550
576
|
method = header[4, 2].unpack1("v")
|
|
551
577
|
crc = header[10, 4].unpack1("V")
|
|
@@ -565,7 +591,8 @@ module Xlsxrb
|
|
|
565
591
|
has_data_descriptor = gp_flag.anybits?(0x08)
|
|
566
592
|
|
|
567
593
|
if has_data_descriptor && compressed_size.zero?
|
|
568
|
-
|
|
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)
|
|
569
596
|
@cached_entries ||= {}
|
|
570
597
|
@cached_entries[norm_name] = entry_data
|
|
571
598
|
@cached_entries[entry_name] = entry_data if entry_name != norm_name
|
|
@@ -599,15 +626,17 @@ module Xlsxrb
|
|
|
599
626
|
result
|
|
600
627
|
end
|
|
601
628
|
|
|
602
|
-
def find_data_descriptor_stream(io, method)
|
|
629
|
+
def find_data_descriptor_stream(io, method, zip64: false)
|
|
603
630
|
if method == 8
|
|
604
631
|
inflater = Zlib::Inflate.new(-Zlib::MAX_WBITS)
|
|
605
632
|
result = +""
|
|
606
633
|
chunk_size = 4096
|
|
634
|
+
fed = 0
|
|
607
635
|
begin
|
|
608
636
|
while !inflater.finished? && (chunk = io.read(chunk_size))
|
|
609
637
|
break if chunk.empty?
|
|
610
638
|
|
|
639
|
+
fed += chunk.bytesize
|
|
611
640
|
inflated = inflater.inflate(chunk)
|
|
612
641
|
limit = @max_uncompressed_size || MAX_UNCOMPRESSED_SIZE
|
|
613
642
|
raise ZipBombError, "ZIP bomb detected: Uncompressed size exceeds #{limit} bytes" if result.bytesize + inflated.bytesize > limit
|
|
@@ -617,19 +646,22 @@ module Xlsxrb
|
|
|
617
646
|
rescue Zlib::BufError, Zlib::DataError
|
|
618
647
|
# Inflation ended
|
|
619
648
|
ensure
|
|
620
|
-
|
|
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
|
|
621
652
|
inflater.close
|
|
622
653
|
end
|
|
623
654
|
|
|
624
655
|
io.seek(-remaining_bytes, IO::SEEK_CUR) if io.respond_to?(:seek) && remaining_bytes.positive?
|
|
625
656
|
|
|
626
657
|
desc_sig = io.read(4)
|
|
658
|
+
sizes_len = zip64 ? 16 : 8
|
|
627
659
|
if desc_sig == [0x50, 0x4B, 0x07, 0x08].pack("C4")
|
|
628
|
-
io.read(
|
|
660
|
+
io.read(4 + sizes_len)
|
|
629
661
|
elsif desc_sig == [0x50, 0x4B, 0x03, 0x04].pack("C4") || desc_sig == [0x50, 0x4B, 0x01, 0x02].pack("C4")
|
|
630
662
|
io.seek(-4, IO::SEEK_CUR) if io.respond_to?(:seek)
|
|
631
663
|
elsif desc_sig
|
|
632
|
-
io.read(
|
|
664
|
+
io.read(sizes_len)
|
|
633
665
|
end
|
|
634
666
|
|
|
635
667
|
[result, 0]
|
|
@@ -706,6 +738,24 @@ module Xlsxrb
|
|
|
706
738
|
end
|
|
707
739
|
end
|
|
708
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
|
+
|
|
709
759
|
#: (String extra_data, Integer usize, Integer csize, Integer offset) -> [Integer, Integer, Integer]
|
|
710
760
|
def parse_zip64_extra(extra_data, usize, csize, offset)
|
|
711
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
|