From e3029d0b100257b530ee9ee62df56361d1e76df7 Mon Sep 17 00:00:00 2001 From: Tyler Pickett Date: Tue, 17 Jun 2014 10:08:26 -0600 Subject: [PATCH] Return created zip file from Zip::File.open when supplied a block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This change is to match ruby zip’s behavior to core’s IO.open when supplied a block. --- lib/zip/file.rb | 1 + test/file_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/zip/file.rb b/lib/zip/file.rb index 198b57f..af7d1d6 100755 --- a/lib/zip/file.rb +++ b/lib/zip/file.rb @@ -97,6 +97,7 @@ module Zip return zf unless block_given? begin yield zf + zf ensure zf.close end diff --git a/test/file_test.rb b/test/file_test.rb index 9fe35d1..931eea4 100644 --- a/test/file_test.rb +++ b/test/file_test.rb @@ -38,6 +38,18 @@ class ZipFileTest < MiniTest::Unit::TestCase assert_equal(2, zfRead.entries.length) end + def test_open_with_block_returns_zip_file + srcFile = "test/data/file2.txt" + entryName = "newEntryName.rb" + assert(::File.exist?(srcFile)) + created_file = ::Zip::File.open(EMPTY_FILENAME, ::Zip::File::CREATE) do |zf| + zf.add(entryName, srcFile) + end + + zfRead = ::Zip::File.new(EMPTY_FILENAME) + assert_equal(created_file, zfRead) + end + def test_get_output_stream entryCount = nil ::Zip::File.open(TEST_ZIP.zip_name) {