ziperu 0.3.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d696a3e103aeb025bdb36d6f631ed295574f87a9a3059a5c2c6192858a1b9f8
4
- data.tar.gz: 0e100226ee8f6c5410a23ebf742d1b9faec420f2b752fd5d63dbeeec8888a455
3
+ metadata.gz: b96255d80b3f9f9f6f5c981f309f396731a632a87807ba036440aff303f04865
4
+ data.tar.gz: 555da85ad0dcdea11e2df3aec1eb4ec3b5fd5574d97e1fd98585f2dee944a4bc
5
5
  SHA512:
6
- metadata.gz: a2c2fc7032401d846c5120591a35db950eba3da2dafd94a6817cf49ba5ee426191d675f986a9b9c75ecde81dcfba10f9cc0a66bc7ff2c4e02180909bb8e4796b
7
- data.tar.gz: ab5c5a04297cd4ff90e5591aafa4af3b97432437b3f1abd9ba4257d2199cebe488de87a88641963d5ce3390ee195567b7edbfa6659e30802980503bd9babd1c3
6
+ metadata.gz: 1534422e2d2846dadab5576ac3c66b31a76c138cf3f989b0415993e33ab9d7ab3228a6108effb6a2cf27c9ec603bd0ab6d334e45a4a795de86ca2ff787ae05f8
7
+ data.tar.gz: f38ec763b5e80e81a6de0deeb27a5457cc95a49920c45e70e78ae631c46344e94fc1e6e0d512f335ffae84cc210b1166860b2e5ddd71a5289a93cbdea5b2543e
data/CHANGELOG.md CHANGED
@@ -1,5 +1,44 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.1 (2026-11-16)
4
+
5
+ Fixed:
6
+
7
+ - An entry that `File.foreach` yielded from a path could not be read once
8
+ `foreach` had returned: `get_input_stream` raised `IOError: closed
9
+ archive`. That covered `foreach(path).find { ... }` and entries
10
+ collected in the block. rubyzip allows it, and so does an entry of a
11
+ closed `Ziperu::File`; the archive is now reopened for these entries
12
+ too. Found by write_xlsx's test suite, 974 of whose tests failed on it.
13
+ - `File#extract`, and `extract_all` with a block, left an empty file
14
+ behind when the entry could not be read (a bad CRC, a wrong password).
15
+ The file is removed, as the native path of `extract_all` already did.
16
+ - `File#read` and `extract_all` raised a plain `Zip::Error` for an entry
17
+ in a compression method that cannot be read. They raise
18
+ `Zip::CompressionMethodError`, as rubyzip does and as
19
+ `get_input_stream` did, so one `rescue` covers every way of reading.
20
+ - `File.extract_all(path, dest, password: "...")` raised "unknown
21
+ keyword": the one-liner passed every option to `#extract_all` and none
22
+ to `open`. It now takes both kinds, the limits included.
23
+ - `File.foreach` takes `password:`, and the entries it yields are read
24
+ with it. They could only be read by building a decrypter for each.
25
+ - A `metadata_encoding:` Ruby does not know raises `ArgumentError`. It was
26
+ ignored, and names stayed binary with nothing to say why.
27
+ - The message for an archive cut short named Rust types ("Unexpected end
28
+ of zip::format::blocks::Zip32CDEBlock"). It says what is cut short: the
29
+ end of central directory record, a central directory entry and so on.
30
+ - `File#inspect` is one line (the path, the entry count, read-only or
31
+ uncommitted). The default listed every entry: half a megabyte for an
32
+ archive of 5,000, in irb, a log line or an error message.
33
+ - `InputStream.new(path, offset: n)` closes the archive before raising
34
+ when no entry starts at `n`. It was left for the GC.
35
+
36
+ Internal:
37
+
38
+ - The weekly run of other gems' test suites never got as far as running
39
+ one: installing the built gem with `--local` could not find `rb_sys`,
40
+ and the script had lost its execute bit.
41
+
3
42
  ## 0.3.0 (2026-11-14)
4
43
 
5
44
  Security:
data/Cargo.lock CHANGED
@@ -879,7 +879,7 @@ dependencies = [
879
879
 
880
880
  [[package]]
881
881
  name = "ziperu"
882
- version = "0.3.0"
882
+ version = "0.3.1"
883
883
  dependencies = [
884
884
  "crc32fast",
885
885
  "flate2",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "ziperu"
3
- version = "0.3.0"
3
+ version = "0.3.1"
4
4
  edition = "2021"
5
5
  publish = false
6
6
  rust-version = "1.88"
@@ -95,6 +95,17 @@ pub fn zip_error(ruby: &Ruby, err: ZipError) -> Error {
95
95
  fn invalid_archive_message(msg: &str) -> String {
96
96
  if msg.contains("EOCD") {
97
97
  "Zip end of central directory signature not found".to_string()
98
+ } else if let Some(block) = msg.strip_prefix("Unexpected end of ") {
99
+ // The crate names the Rust type it was reading.
100
+ let what = match block.rsplit("::").next().unwrap_or(block) {
101
+ "Zip32CDEBlock" => "the end of central directory record",
102
+ "Zip64CDEBlock" => "the zip64 end of central directory record",
103
+ "Zip64CDELocatorBlock" => "the zip64 end of central directory locator",
104
+ "ZipCentralEntryBlock" => "a central directory entry",
105
+ "ZipLocalEntryBlock" => "a local file header",
106
+ _ => "a record",
107
+ };
108
+ format!("invalid Zip archive: {what} is cut short")
98
109
  } else {
99
110
  // Some of the crate's messages carry a placeholder it never fills.
100
111
  msg.replace(" of {properties_size}", "")
@@ -254,3 +265,29 @@ fn decompression_error(ruby: &Ruby, msg: &str) -> Error {
254
265
  Err(e) => e,
255
266
  }
256
267
  }
268
+
269
+ #[cfg(test)]
270
+ mod tests {
271
+ use super::invalid_archive_message;
272
+
273
+ #[test]
274
+ fn no_rust_type_names_in_messages() {
275
+ for block in [
276
+ "Zip32CDEBlock",
277
+ "Zip64CDEBlock",
278
+ "Zip64CDELocatorBlock",
279
+ "ZipCentralEntryBlock",
280
+ "ZipLocalEntryBlock",
281
+ "SomethingNew",
282
+ ] {
283
+ let said =
284
+ invalid_archive_message(&format!("Unexpected end of zip::format::blocks::{block}"));
285
+ assert!(said.ends_with("is cut short"), "{said}");
286
+ assert!(!said.contains("::") && !said.contains("Block"), "{said}");
287
+ }
288
+ assert_eq!(
289
+ invalid_archive_message("Unexpected end of zip::format::blocks::Zip32CDEBlock"),
290
+ "invalid Zip archive: the end of central directory record is cut short"
291
+ );
292
+ }
293
+ }
@@ -52,6 +52,11 @@ module Ziperu
52
52
  stored&.native(offset)
53
53
  end
54
54
 
55
+ # The password its owner was given for the archive, or nil.
56
+ def password
57
+ nil
58
+ end
59
+
55
60
  # The record's local-header extra fields, as stored.
56
61
  def local_extra(offset)
57
62
  stored.local_extra(offset)
@@ -69,8 +74,9 @@ module Ziperu
69
74
 
70
75
  NONE = None.new.freeze
71
76
 
72
- # A record of `native`, the archive `owner` (a Ziperu::File, or nil
73
- # for an entry read through an InputStream) has open. `index` is nil
77
+ # A record of `native`, the archive `owner` has open: a Ziperu::File,
78
+ # a Reopener for an entry `File.foreach` yielded from a path, or nil
79
+ # for an entry read through an InputStream. `index` is nil
74
80
  # for a record the native core cannot address by index (one of
75
81
  # several under the same name), which is found by its offset.
76
82
  class Archive
@@ -89,11 +95,14 @@ module Ziperu
89
95
  self
90
96
  end
91
97
 
92
- # The native archive, reopened if the owning Ziperu::File released
93
- # it on close: rubyzip lets entries be read after `close`.
98
+ def password
99
+ @owner.password if @owner.respond_to?(:password)
100
+ end
101
+
102
+ # The native archive, reopened if its owner released it on close:
103
+ # rubyzip lets entries be read after `close`.
94
104
  def native(offset)
95
- if @native.closed? && @owner.kind_of?(::Ziperu::File)
96
- reopened = @owner.native_archive
105
+ if @native.closed? && (reopened = (@owner.native_archive if @owner.respond_to?(:native_archive)))
97
106
  unless still_at_place?(reopened, offset)
98
107
  where = @index ? "at index #{@index} of" : 'in'
99
108
  raise EntryMovedError, "entry '#{@raw_name}' is no longer #{where} #{@owner.name}"
@@ -132,6 +141,32 @@ module Ziperu
132
141
  end
133
142
  end
134
143
 
144
+ # Stands in for a Ziperu::File as the owner of the entries that
145
+ # `File.foreach` yields: it has the password given for the archive,
146
+ # and for an archive at a path it reopens it, so the entries can be
147
+ # read once the walk is over (`foreach(path).find { ... }`, or
148
+ # entries collected in the block). One is shared by all the entries
149
+ # of a walk, and so is the archive it reopens.
150
+ class Reopener
151
+ attr_reader :name, :password
152
+
153
+ # `name` is the archive's path, or nil for one read from an IO,
154
+ # which cannot be opened again.
155
+ def initialize(name, settings, password = nil)
156
+ @name = name
157
+ @settings = settings
158
+ @password = password
159
+ @native = nil
160
+ end
161
+
162
+ def native_archive
163
+ return if @name.nil?
164
+
165
+ @native = InputStream.open_archive(@name, @settings) if @native.nil? || @native.closed?
166
+ @native
167
+ end
168
+ end
169
+
135
170
  # Data still outside the archive.
136
171
  class Pending
137
172
  include Defaults
data/lib/ziperu/entry.rb CHANGED
@@ -266,12 +266,22 @@ module Ziperu
266
266
  fstype version_made_by dos_date dos_time comment extra header_offset].freeze # :nodoc:
267
267
 
268
268
  # Build an entry from one row (see ROW_FIELDS).
269
- def self.from_row(archive, row, zipfile = nil, metadata_encoding: nil, settings: ::Ziperu) # :nodoc:
269
+ # `owner` is who reopens the archive for the entry once it is closed:
270
+ # `zipfile`, unless another is given (see Source::Reopener).
271
+ def self.from_row(archive, row, zipfile = nil, metadata_encoding: nil, settings: ::Ziperu, owner: zipfile) # :nodoc:
270
272
  entry = allocate
271
- entry.send(:init_from_row, archive, row, zipfile, metadata_encoding, settings)
273
+ entry.send(:init_from_row, archive, row, zipfile, metadata_encoding, settings, owner)
272
274
  entry
273
275
  end
274
276
 
277
+ # Raises for a `metadata_encoding:` Ruby does not know, which was once
278
+ # ignored: names then stayed binary and nothing said why.
279
+ def self.check_metadata_encoding!(encoding) # :nodoc:
280
+ ::Encoding.find(encoding) unless encoding.nil?
281
+ rescue ::ArgumentError
282
+ raise ::ArgumentError, "unknown metadata_encoding: #{encoding.inspect}"
283
+ end
284
+
275
285
  # Is this entry encrypted?
276
286
  def encrypted?
277
287
  gp_flags.anybits?(ENCRYPTED_GPFLAG)
@@ -597,9 +607,13 @@ module Ziperu
597
607
  end
598
608
 
599
609
  # The decrypter for this entry when the owning `Ziperu::File` was
600
- # opened with a password; nil otherwise, or for a plain entry.
610
+ # opened with a password, or `File.foreach` was given one; nil
611
+ # otherwise, or for a plain entry.
601
612
  def default_decrypter # :nodoc:
602
- return unless encrypted? && @zipfile.kind_of?(::Ziperu::File) && (pw = @zipfile.password)
613
+ return unless encrypted?
614
+
615
+ pw = @zipfile.kind_of?(::Ziperu::File) ? @zipfile.password : @source.password
616
+ return unless pw
603
617
 
604
618
  aes? ? AESDecrypter.new(pw) : TraditionalDecrypter.new(pw)
605
619
  end
@@ -839,7 +853,8 @@ module Ziperu
839
853
  # one: in a single native call when the entry is in an archive and its
840
854
  # declared size is to be trusted, through a stream otherwise.
841
855
  def read_all # :nodoc:
842
- if !pending_data? && @source.stored? && !encrypted? && @settings.validate_entry_sizes
856
+ if !pending_data? && @source.stored? && !encrypted? && @settings.validate_entry_sizes &&
857
+ READABLE_COMPRESSION_METHODS.include?(compression_method)
843
858
  get_raw_input_string
844
859
  else
845
860
  get_input_stream(&:read)
@@ -911,7 +926,15 @@ module Ziperu
911
926
  ::FileUtils.rm_f(staged) unless done
912
927
  end
913
928
  else
914
- ::File.open(dest_path, WRITE_NO_FOLLOW, binmode: true) { |os| write_data_to(os) }
929
+ # Nothing was there, so an extraction that fails takes away what
930
+ # it had written rather than leave a file cut short.
931
+ done = false
932
+ begin
933
+ ::File.open(dest_path, WRITE_NO_FOLLOW, binmode: true) { |os| write_data_to(os) }
934
+ done = true
935
+ ensure
936
+ ::FileUtils.rm_f(dest_path) unless done
937
+ end
915
938
  end
916
939
 
917
940
  set_extra_attributes_on_path(dest_path)
@@ -1064,7 +1087,7 @@ module Ziperu
1064
1087
  raise EntryNameError if name.length > MAX_FIELD_LENGTH
1065
1088
  end
1066
1089
 
1067
- def init_from_row(archive, row, zipfile, metadata_encoding, settings)
1090
+ def init_from_row(archive, row, zipfile, metadata_encoding, settings, owner)
1068
1091
  index, name, size, compressed_size, crc, method, flags, external_attributes, fstype, _version,
1069
1092
  dos_date, dos_time, comment, extra, header_offset = row
1070
1093
  @settings = settings
@@ -1080,7 +1103,7 @@ module Ziperu
1080
1103
  # the archive knows the entry by.
1081
1104
  @gp_flags = flags
1082
1105
  @original_name = name
1083
- @source = archive ? Source::Archive.new(archive, zipfile, index, @original_name) : Source::NONE
1106
+ @source = archive ? Source::Archive.new(archive, owner, index, @original_name) : Source::NONE
1084
1107
  # Most entries have no comment and many no extra fields: those share
1085
1108
  # one empty String rather than carrying their own.
1086
1109
  @extra = nil
@@ -44,6 +44,8 @@ module Ziperu
44
44
  # itself (`unzip -j`); `sanitize: :windows` rewrites names that Windows
45
45
  # refuses (`<>:"|?*`, control characters, a trailing dot or space,
46
46
  # `CON`, `NUL` and the like) so the archive extracts there at all.
47
+ EXTRACT_ALL_OPTIONS = %i[symlinks progress threads on_exists exclude junk_paths sanitize].freeze # :nodoc:
48
+
47
49
  def extract_all(destination_directory = '.', symlinks: :skip, progress: nil, threads: 1,
48
50
  on_exists: nil, exclude: nil, junk_paths: false, sanitize: nil, &block)
49
51
  targets = ExtractionPlan.new(@cdir.writable_entries, exclude: exclude, junk_paths: junk_paths,
@@ -262,6 +264,9 @@ module Ziperu
262
264
  return false unless entry.instance_of?(::Ziperu::Entry) && entry.index && entry.backed_by?(native)
263
265
  # An encrypted entry goes through the Ruby path, which has the password.
264
266
  return false if entry.encrypted?
267
+ # So does one in a method that cannot be read, for the error rubyzip
268
+ # raises (CompressionMethodError).
269
+ return false unless READABLE_COMPRESSION_METHODS.include?(entry.compression_method)
265
270
  return false unless entry.file? || entry.directory?
266
271
 
267
272
  IOExtras.native_text?(entry.name)
data/lib/ziperu/file.rb CHANGED
@@ -61,6 +61,7 @@ module Ziperu
61
61
  # encoding flag (an archive made on Windows in Japan, say:
62
62
  # "Shift_JIS"); they are transcoded to UTF-8. Without it such names
63
63
  # are binary Strings, as in rubyzip.
64
+ Entry.check_metadata_encoding!(metadata_encoding)
64
65
  @metadata_encoding = metadata_encoding
65
66
 
66
67
  # On commit, runs of files added from disk are compressed on this
@@ -208,19 +209,27 @@ module Ziperu
208
209
  # building the whole listing first: the way to walk an archive with
209
210
  # hundreds of thousands of entries, or to stop at the first match.
210
211
  # Each entry can be read (`get_input_stream`, `extract`) while the
211
- # block runs; the archive is closed when `foreach` returns. Without a
212
- # block, returns an Enumerator.
213
- def foreach(path_or_io, metadata_encoding: nil, **settings, &block)
214
- return enum_for(:foreach, path_or_io, metadata_encoding: metadata_encoding, **settings) unless block
212
+ # block runs; the archive is closed when `foreach` returns. An entry
213
+ # of an archive at a path can still be read afterwards, as rubyzip
214
+ # allows: the archive is reopened for it. `password:` is the one the
215
+ # entries are read with, as with `open`. Without a block, returns an
216
+ # Enumerator.
217
+ def foreach(path_or_io, metadata_encoding: nil, password: nil, **settings, &block)
218
+ unless block
219
+ return enum_for(:foreach, path_or_io, metadata_encoding: metadata_encoding, password: password, **settings)
220
+ end
215
221
 
222
+ Entry.check_metadata_encoding!(metadata_encoding)
216
223
  unknown = settings.dup
217
224
  settings = Settings.take!(unknown)
218
225
  raise ArgumentError, "unknown keyword: #{unknown.keys.map(&:inspect).join(', ')}" unless unknown.empty?
219
226
 
220
227
  native = InputStream.open_archive(path_or_io, settings)
228
+ owner = Entry::Source::Reopener.new((path_or_io if InputStream.path_like?(path_or_io)), settings, password)
221
229
  begin
222
230
  native.each_entry_row do |row|
223
- yield Entry.from_row(native, row, nil, metadata_encoding: metadata_encoding, settings: settings)
231
+ yield Entry.from_row(native, row, nil, metadata_encoding: metadata_encoding, settings: settings,
232
+ owner: owner)
224
233
  end
225
234
  ensure
226
235
  native.close
@@ -244,10 +253,13 @@ module Ziperu
244
253
  end
245
254
 
246
255
  # Extracts every entry of the archive at `zip_file_name` into
247
- # `destination_directory`.
256
+ # `destination_directory`. Takes the options of `#extract_all`; any
257
+ # other is for opening the archive (`password:`, the limits and the
258
+ # rest of what `open` takes).
248
259
  def extract_all(zip_file_name, destination_directory = '.', **options, &block)
249
- Ziperu::File.open(zip_file_name) do |zf|
250
- zf.extract_all(destination_directory, **options, &block)
260
+ how, open_options = options.partition { |name, _| EXTRACT_ALL_OPTIONS.include?(name) }.map(&:to_h)
261
+ Ziperu::File.open(zip_file_name, **open_options) do |zf|
262
+ zf.extract_all(destination_directory, **how, &block)
251
263
  end
252
264
  end
253
265
  end
@@ -300,6 +312,13 @@ module Ziperu
300
312
  @name
301
313
  end
302
314
 
315
+ # Short, where the default lists every entry: half a megabyte for an
316
+ # archive of 5,000, in irb, a log line or an error message.
317
+ def inspect
318
+ state = [('read-only' if @read_only), ('uncommitted changes' if @dirty)].compact
319
+ "#<#{self.class.name}:#{@name} entries=#{@cdir.size}#{state.map { |s| " (#{s})" }.join}>"
320
+ end
321
+
303
322
  # Returns a string containing the contents of the specified entry
304
323
  def read(entry)
305
324
  get_entry(entry).read_all
@@ -44,6 +44,7 @@ module Ziperu
44
44
  enforce_size: nil, metadata_encoding: nil, streaming: false, **opts)
45
45
  @settings = Settings.take!(opts)
46
46
  enforce_size = @settings.validate_entry_sizes if enforce_size.nil?
47
+ Entry.check_metadata_encoding!(metadata_encoding)
47
48
  @metadata_encoding = metadata_encoding
48
49
  # rubyzip 2.x's `new(io, offset, decrypter)`.
49
50
  offset, decrypter = Compat.positional(legacy, %i[offset decrypter],
@@ -99,6 +100,7 @@ module Ziperu
99
100
  @next_index = offset.zero? || @archive.nil? ? 0 : index_at_offset(offset)
100
101
  return unless @next_index.nil?
101
102
 
103
+ @archive.close
102
104
  raise Error, "no entry starts at offset #{offset} (Zip local header magic not found)"
103
105
  end
104
106
 
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ziperu
4
- VERSION = '0.3.0'
4
+ VERSION = '0.3.1'
5
5
  # The rubyzip API this gem tracks.
6
6
  RUBYZIP_COMPAT_VERSION = '3.7'
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ziperu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maor Caplan