2021-05-24 01:24:22 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
require 'test_helper'
|
|
|
|
|
2014-07-15 23:23:48 +08:00
|
|
|
class ZipOutputStreamTest < MiniTest::Test
|
2014-01-21 05:31:06 +08:00
|
|
|
include AssertEntry
|
|
|
|
|
|
|
|
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
2015-03-21 16:27:44 +08:00
|
|
|
TEST_ZIP.zip_name = 'test/data/generated/output.zip'
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
def test_new
|
|
|
|
zos = ::Zip::OutputStream.new(TEST_ZIP.zip_name)
|
|
|
|
zos.comment = TEST_ZIP.comment
|
|
|
|
write_test_zip(zos)
|
|
|
|
zos.close
|
|
|
|
assert_test_zip_contents(TEST_ZIP)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open
|
|
|
|
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
|
|
|
zos.comment = TEST_ZIP.comment
|
|
|
|
write_test_zip(zos)
|
|
|
|
end
|
|
|
|
assert_test_zip_contents(TEST_ZIP)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_write_buffer
|
2021-06-09 01:09:22 +08:00
|
|
|
buffer = ::Zip::OutputStream.write_buffer(::StringIO.new) do |zos|
|
2014-01-21 05:31:06 +08:00
|
|
|
zos.comment = TEST_ZIP.comment
|
|
|
|
write_test_zip(zos)
|
|
|
|
end
|
2024-03-02 06:14:48 +08:00
|
|
|
File.binwrite(TEST_ZIP.zip_name, buffer.string)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_test_zip_contents(TEST_ZIP)
|
|
|
|
end
|
|
|
|
|
2020-02-20 01:48:13 +08:00
|
|
|
def test_write_buffer_binmode
|
2021-06-09 01:09:22 +08:00
|
|
|
buffer = ::Zip::OutputStream.write_buffer(::StringIO.new) do |zos|
|
2020-02-20 01:48:13 +08:00
|
|
|
zos.comment = TEST_ZIP.comment
|
|
|
|
write_test_zip(zos)
|
|
|
|
end
|
2020-03-02 18:00:39 +08:00
|
|
|
assert_equal Encoding::ASCII_8BIT, buffer.external_encoding
|
2020-02-20 01:48:13 +08:00
|
|
|
end
|
|
|
|
|
2015-02-03 05:40:38 +08:00
|
|
|
def test_write_buffer_with_temp_file
|
|
|
|
tmp_file = Tempfile.new('')
|
|
|
|
|
|
|
|
::Zip::OutputStream.write_buffer(tmp_file) do |zos|
|
|
|
|
zos.comment = TEST_ZIP.comment
|
|
|
|
write_test_zip(zos)
|
|
|
|
end
|
|
|
|
|
|
|
|
tmp_file.rewind
|
2024-03-02 06:14:48 +08:00
|
|
|
File.binwrite(TEST_ZIP.zip_name, tmp_file.read)
|
2015-02-03 05:40:38 +08:00
|
|
|
tmp_file.unlink
|
|
|
|
|
|
|
|
assert_test_zip_contents(TEST_ZIP)
|
|
|
|
end
|
|
|
|
|
2021-06-24 05:24:44 +08:00
|
|
|
def test_write_buffer_with_temp_file2
|
|
|
|
tmp_file = ::File.join(Dir.tmpdir, 'zos.zip')
|
|
|
|
::File.open(tmp_file, 'wb') do |f|
|
|
|
|
::Zip::OutputStream.write_buffer(f) do |zos|
|
|
|
|
zos.comment = TEST_ZIP.comment
|
|
|
|
write_test_zip(zos)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
::Zip::File.open(tmp_file) # Should open without error.
|
|
|
|
ensure
|
|
|
|
::File.unlink(tmp_file)
|
|
|
|
end
|
|
|
|
|
2021-06-09 01:09:22 +08:00
|
|
|
def test_write_buffer_with_default_io
|
|
|
|
buffer = ::Zip::OutputStream.write_buffer do |zos|
|
|
|
|
zos.comment = TEST_ZIP.comment
|
|
|
|
write_test_zip(zos)
|
|
|
|
end
|
2024-03-02 06:14:48 +08:00
|
|
|
File.binwrite(TEST_ZIP.zip_name, buffer.string)
|
2021-06-09 01:09:22 +08:00
|
|
|
assert_test_zip_contents(TEST_ZIP)
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_writing_to_closed_stream
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_i_o_error_in_closed_stream { |zos| zos << 'hello world' }
|
|
|
|
assert_i_o_error_in_closed_stream { |zos| zos.puts 'hello world' }
|
|
|
|
assert_i_o_error_in_closed_stream { |zos| zos.write 'hello world' }
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_cannot_open_file
|
2014-01-21 05:31:06 +08:00
|
|
|
name = TestFiles::EMPTY_TEST_DIR
|
|
|
|
begin
|
|
|
|
::Zip::OutputStream.open(name)
|
2020-02-09 19:15:17 +08:00
|
|
|
rescue SystemCallError
|
2019-09-27 04:46:00 +08:00
|
|
|
assert($ERROR_INFO.kind_of?(Errno::EISDIR) || # Linux
|
|
|
|
$ERROR_INFO.kind_of?(Errno::EEXIST) || # Windows/cygwin
|
|
|
|
$ERROR_INFO.kind_of?(Errno::EACCES), # Windows
|
|
|
|
"Expected Errno::EISDIR (or on win/cygwin: Errno::EEXIST), but was: #{$ERROR_INFO.class}")
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_put_next_entry
|
2015-03-21 16:27:44 +08:00
|
|
|
stored_text = 'hello world in stored text'
|
|
|
|
entry_name = 'file1'
|
|
|
|
comment = 'my comment'
|
2014-01-21 05:31:06 +08:00
|
|
|
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
|
|
|
zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
|
|
|
|
zos << stored_text
|
|
|
|
end
|
|
|
|
|
2021-05-30 14:04:56 +08:00
|
|
|
assert(File.read(TEST_ZIP.zip_name, mode: 'rb')[stored_text])
|
2014-01-21 05:31:06 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
|
|
|
assert_equal(stored_text, zf.read(entry_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_put_next_entry_using_zip_entry_creates_entries_with_correct_timestamps
|
2015-03-21 16:27:44 +08:00
|
|
|
file = ::File.open('test/data/file2.txt', 'rb')
|
2014-01-21 05:31:06 +08:00
|
|
|
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
2020-08-03 00:51:08 +08:00
|
|
|
zip_entry = ::Zip::Entry.new(
|
|
|
|
zos, file.path, time: ::Zip::DOSTime.at(file.mtime)
|
|
|
|
)
|
2014-01-21 05:31:06 +08:00
|
|
|
zos.put_next_entry(zip_entry)
|
|
|
|
zos << file.read
|
|
|
|
end
|
|
|
|
|
2015-03-21 16:16:06 +08:00
|
|
|
::Zip::InputStream.open(TEST_ZIP.zip_name) do |io|
|
2014-01-21 05:31:06 +08:00
|
|
|
while (entry = io.get_next_entry)
|
2019-09-27 05:00:46 +08:00
|
|
|
# Compare DOS Times, since they are stored with two seconds accuracy
|
2020-11-29 05:19:58 +08:00
|
|
|
assert(::Zip::DOSTime.at(file.mtime) == ::Zip::DOSTime.at(entry.mtime))
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-01 06:31:10 +08:00
|
|
|
def test_chained_put_into_next_entry
|
2015-03-21 16:27:44 +08:00
|
|
|
stored_text = 'hello world in stored text'
|
|
|
|
stored_text2 = 'with chain'
|
|
|
|
entry_name = 'file1'
|
|
|
|
comment = 'my comment'
|
2014-03-01 06:31:10 +08:00
|
|
|
::Zip::OutputStream.open(TEST_ZIP.zip_name) do |zos|
|
|
|
|
zos.put_next_entry(entry_name, comment, nil, ::Zip::Entry::STORED)
|
|
|
|
zos << stored_text << stored_text2
|
|
|
|
end
|
|
|
|
|
|
|
|
assert(File.read(TEST_ZIP.zip_name)[stored_text])
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
|
|
|
assert_equal(stored_text + stored_text2, zf.read(entry_name))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def assert_i_o_error_in_closed_stream
|
2015-03-21 16:10:37 +08:00
|
|
|
assert_raises(IOError) do
|
2015-03-21 16:27:44 +08:00
|
|
|
zos = ::Zip::OutputStream.new('test/data/generated/test_putOnClosedStream.zip')
|
2014-01-21 05:31:06 +08:00
|
|
|
zos.close
|
|
|
|
yield zos
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def write_test_zip(zos)
|
2020-02-18 06:35:08 +08:00
|
|
|
TEST_ZIP.entry_names.each do |entry_name|
|
|
|
|
zos.put_next_entry(entry_name)
|
|
|
|
File.open(entry_name, 'rb') { |f| zos.write(f.read) }
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|