From b4886dfc212465ead531cc8325ad44d5443de73c Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Tue, 12 Nov 2024 15:51:15 +0100 Subject: [PATCH] Add RubyZip 2.4 and 3.0 compatibility Close #384 --- .github/workflows/test.yml | 3 ++ .gitignore | 3 +- gemfiles/rubyzip_3.gemfile | 14 ++++++++ lib/axlsx.rb | 1 + lib/axlsx/package.rb | 7 +++- lib/axlsx/util/buffered_zip_output_stream.rb | 38 ++++++++++++++------ 6 files changed, 54 insertions(+), 12 deletions(-) create mode 100644 gemfiles/rubyzip_3.gemfile diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5bb8232a7..0c01b8955 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -30,6 +30,9 @@ jobs: allow_failures: 'allow failures' - ruby: jruby-head allow_failures: 'allow failures' + - ruby: 3.3 + gemfile: 'gemfiles/rubyzip_3.gemfile' + allow_failures: 'allow failures' steps: - uses: actions/checkout@v4 diff --git a/.gitignore b/.gitignore index 95ae026ce..d55b660db 100644 --- a/.gitignore +++ b/.gitignore @@ -18,4 +18,5 @@ examples/sprk2012 .~lock* *.qcachegrind *.iml -.idea \ No newline at end of file +.idea +gemfiles/*.lock diff --git a/gemfiles/rubyzip_3.gemfile b/gemfiles/rubyzip_3.gemfile new file mode 100644 index 000000000..1a3a59896 --- /dev/null +++ b/gemfiles/rubyzip_3.gemfile @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' +gemspec path: "../" + +gem 'rubyzip', git: 'https://github.com/rubyzip/rubyzip.git' + +group :test do + gem 'rake' + gem 'simplecov' + gem 'minitest' + gem 'timecop' + gem 'webmock' +end diff --git a/lib/axlsx.rb b/lib/axlsx.rb index 931b515fb..f51f208bf 100644 --- a/lib/axlsx.rb +++ b/lib/axlsx.rb @@ -7,6 +7,7 @@ require 'marcel' require 'nokogiri' require 'zip' +require 'zip/version' # Ruby core dependencies require 'cgi' diff --git a/lib/axlsx/package.rb b/lib/axlsx/package.rb index a711b90ea..208b747b1 100644 --- a/lib/axlsx/package.rb +++ b/lib/axlsx/package.rb @@ -211,7 +211,12 @@ def write_parts(zip) # @return [Zip::Entry] def zip_entry_for_part(part) timestamp = Zip::DOSTime.at(@core.created.to_i) - Zip::Entry.new("", part[:entry], "", "", 0, 0, Zip::Entry::DEFLATED, 0, timestamp) + + if Zip::VERSION >= '2.4' + Zip::Entry.new("", part[:entry], time: timestamp) + else + Zip::Entry.new("", part[:entry], "", "", 0, 0, Zip::Entry::DEFLATED, 0, timestamp) + end end # The parts of a package diff --git a/lib/axlsx/util/buffered_zip_output_stream.rb b/lib/axlsx/util/buffered_zip_output_stream.rb index b348dc558..496ef5e7a 100644 --- a/lib/axlsx/util/buffered_zip_output_stream.rb +++ b/lib/axlsx/util/buffered_zip_output_stream.rb @@ -19,20 +19,38 @@ def initialize(zos) # # The directory and its contents are removed at the end of the block. def self.open(file_name, encrypter = nil) - Zip::OutputStream.open(file_name, encrypter) do |zos| - bzos = new(zos) - yield(bzos) - ensure - bzos.flush if bzos + if Zip::VERSION >= '2.4' + Zip::OutputStream.open(file_name, encrypter: encrypter) do |zos| + bzos = new(zos) + yield(bzos) + ensure + bzos.flush if bzos + end + else + Zip::OutputStream.open(file_name, encrypter) do |zos| + bzos = new(zos) + yield(bzos) + ensure + bzos.flush if bzos + end end end def self.write_buffer(io = ::StringIO.new, encrypter = nil) - Zip::OutputStream.write_buffer(io, encrypter) do |zos| - bzos = new(zos) - yield(bzos) - ensure - bzos.flush if bzos + if Zip::VERSION >= '2.4' + Zip::OutputStream.write_buffer(io, encrypter: encrypter) do |zos| + bzos = new(zos) + yield(bzos) + ensure + bzos.flush if bzos + end + else + Zip::OutputStream.write_buffer(io, encrypter) do |zos| + bzos = new(zos) + yield(bzos) + ensure + bzos.flush if bzos + end end end