diff --git a/lib/zip/entry.rb b/lib/zip/entry.rb index a563aa4d..7c7f2e17 100644 --- a/lib/zip/entry.rb +++ b/lib/zip/entry.rb @@ -689,9 +689,9 @@ def set_time(binary_dos_date, binary_dos_time) def create_file(dest_path, _continue_on_exists_proc = proc { Zip.continue_on_exists_proc }) if ::File.exist?(dest_path) && !yield(self, dest_path) - raise ::Zip::DestinationFileExistsError, - "Destination '#{dest_path}' already exists" + raise ::Zip::DestinationExistsError, dest_path end + ::File.open(dest_path, 'wb') do |os| get_input_stream do |is| bytes_written = 0 @@ -718,14 +718,11 @@ def create_directory(dest_path) return if ::File.directory?(dest_path) if ::File.exist?(dest_path) - if block_given? && yield(self, dest_path) - ::FileUtils.rm_f dest_path - else - raise ::Zip::DestinationFileExistsError, - "Cannot create directory '#{dest_path}'. " \ - 'A file already exists with that name' - end + raise ::Zip::DestinationExistsError, dest_path unless block_given? && yield(self, dest_path) + + ::FileUtils.rm_f dest_path end + ::FileUtils.mkdir_p(dest_path) set_extra_attributes_on_path(dest_path) end diff --git a/lib/zip/errors.rb b/lib/zip/errors.rb index feac3b84..74bf3a3d 100644 --- a/lib/zip/errors.rb +++ b/lib/zip/errors.rb @@ -2,7 +2,6 @@ module Zip class Error < StandardError; end - class DestinationFileExistsError < Error; end class CompressionMethodError < Error attr_reader :compression_method @@ -30,6 +29,18 @@ def message end end + class DestinationExistsError < Error + def initialize(destination) + super() + @destination = destination + end + + def message + "Cannot create file or directory '#{@destination}'. " \ + 'A file already exists with that name.' + end + end + class EntryExistsError < Error def initialize(source, name) super() diff --git a/test/file_extract_directory_test.rb b/test/file_extract_directory_test.rb index fc10979d..26730a08 100644 --- a/test/file_extract_directory_test.rb +++ b/test/file_extract_directory_test.rb @@ -38,7 +38,9 @@ def test_extract_directory_exists_as_dir def test_extract_directory_exists_as_file File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' } - assert_raises(::Zip::DestinationFileExistsError) { extract_test_dir } + assert_raises(::Zip::DestinationExistsError) do + extract_test_dir + end end def test_extract_directory_exists_as_file_overwrite diff --git a/test/file_extract_test.rb b/test/file_extract_test.rb index 734351a8..0bce8e1b 100644 --- a/test/file_extract_test.rb +++ b/test/file_extract_test.rb @@ -39,11 +39,12 @@ def test_extract_exists text = 'written text' ::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(text) } - assert_raises(::Zip::DestinationFileExistsError) do + assert_raises(::Zip::DestinationExistsError) do ::Zip::File.open(TEST_ZIP.zip_name) do |zf| zf.extract(zf.entries.first, EXTRACTED_FILENAME) end end + File.open(EXTRACTED_FILENAME, 'r') do |f| assert_equal(text, f.read) end diff --git a/test/settings_test.rb b/test/settings_test.rb index 9fb50f1e..3bf66e4e 100644 --- a/test/settings_test.rb +++ b/test/settings_test.rb @@ -40,7 +40,9 @@ def test_true_on_exists_proc def test_false_on_exists_proc Zip.on_exists_proc = false File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' } - assert_raises(Zip::DestinationFileExistsError) { extract_test_dir } + assert_raises(Zip::DestinationExistsError) do + extract_test_dir + end end def test_false_continue_on_exists_proc