2014-01-21 05:31:06 +08:00
|
|
|
require 'test_helper'
|
|
|
|
|
2014-07-15 23:23:48 +08:00
|
|
|
class ZipFileTest < MiniTest::Test
|
2014-01-21 05:31:06 +08:00
|
|
|
include CommonZipFileFixture
|
2016-10-09 06:20:45 +08:00
|
|
|
include ZipEntryData
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2014-07-16 00:12:45 +08:00
|
|
|
OK_DELETE_FILE = 'test/data/generated/okToDelete.txt'
|
|
|
|
OK_DELETE_MOVED_FILE = 'test/data/generated/okToDeleteMoved.txt'
|
|
|
|
|
2014-01-24 17:37:38 +08:00
|
|
|
def teardown
|
|
|
|
::Zip.write_zip64_support = false
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_create_from_scratch_to_buffer
|
2015-03-21 16:27:44 +08:00
|
|
|
comment = 'a short comment'
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
buffer = ::Zip::File.add_buffer do |zf|
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
|
|
|
zf.mkdir('dir1')
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.comment = comment
|
|
|
|
end
|
|
|
|
|
|
|
|
::File.open(EMPTY_FILENAME, 'wb') { |file| file.write buffer.string }
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
|
|
|
assert_equal(comment, zfRead.comment)
|
|
|
|
assert_equal(2, zfRead.entries.length)
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_create_from_scratch
|
2015-03-21 16:27:44 +08:00
|
|
|
comment = 'a short comment'
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
|
|
|
zf.mkdir('dir1')
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.comment = comment
|
|
|
|
zf.close
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
|
|
|
assert_equal(comment, zfRead.comment)
|
|
|
|
assert_equal(2, zfRead.entries.length)
|
|
|
|
end
|
|
|
|
|
2016-09-05 22:16:57 +08:00
|
|
|
def test_create_from_scratch_with_old_create_parameter
|
|
|
|
comment = 'a short comment'
|
|
|
|
|
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, 1)
|
|
|
|
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
|
|
|
zf.mkdir('dir1')
|
|
|
|
zf.comment = comment
|
|
|
|
zf.close
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
|
|
|
assert_equal(comment, zfRead.comment)
|
|
|
|
assert_equal(2, zfRead.entries.length)
|
|
|
|
end
|
|
|
|
|
2018-04-04 04:07:18 +08:00
|
|
|
def test_get_input_stream_stored_with_gpflag_bit3
|
|
|
|
::Zip::File.open('test/data/gpbit3stored.zip') do |zf|
|
2020-02-09 23:57:46 +08:00
|
|
|
assert_equal("foo\n", zf.read('foo.txt'))
|
2018-04-04 04:07:18 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_get_output_stream
|
|
|
|
entryCount = nil
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2014-01-21 05:31:06 +08:00
|
|
|
entryCount = zf.size
|
2015-03-21 16:10:37 +08:00
|
|
|
zf.get_output_stream('newEntry.txt') do |os|
|
2015-03-21 16:27:44 +08:00
|
|
|
os.write 'Putting stuff in newEntry.txt'
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2015-03-23 01:03:50 +08:00
|
|
|
assert_equal(entryCount + 1, zf.size)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
zf.get_output_stream(zf.get_entry('test/data/generated/empty.txt')) do |os|
|
2015-03-21 16:27:44 +08:00
|
|
|
os.write 'Putting stuff in data/generated/empty.txt'
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2015-03-23 01:03:50 +08:00
|
|
|
assert_equal(entryCount + 1, zf.size)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2016-10-09 06:20:45 +08:00
|
|
|
custom_entry_args = [TEST_COMMENT, TEST_EXTRA, TEST_COMPRESSED_SIZE, TEST_CRC, ::Zip::Entry::STORED, TEST_SIZE, TEST_TIME]
|
2015-03-21 16:10:37 +08:00
|
|
|
zf.get_output_stream('entry_with_custom_args.txt', nil, *custom_entry_args) do |os|
|
2015-03-21 16:27:44 +08:00
|
|
|
os.write 'Some data'
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2015-03-23 01:03:50 +08:00
|
|
|
assert_equal(entryCount + 2, zf.size)
|
2014-01-21 05:31:06 +08:00
|
|
|
entry = zf.get_entry('entry_with_custom_args.txt')
|
|
|
|
assert_equal(custom_entry_args[0], entry.comment)
|
|
|
|
assert_equal(custom_entry_args[2], entry.compressed_size)
|
|
|
|
assert_equal(custom_entry_args[3], entry.crc)
|
|
|
|
assert_equal(custom_entry_args[4], entry.compression_method)
|
|
|
|
assert_equal(custom_entry_args[5], entry.size)
|
|
|
|
assert_equal(custom_entry_args[6], entry.time)
|
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
zf.get_output_stream('entry.bin') do |os|
|
2014-01-21 05:31:06 +08:00
|
|
|
os.write(::File.open('test/data/generated/5entry.zip', 'rb').read)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2015-03-23 01:03:50 +08:00
|
|
|
assert_equal(entryCount + 3, zf.size)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
|
|
|
|
assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
|
|
|
|
assert_equal(File.open('test/data/generated/5entry.zip', 'rb').read, zf.read('entry.bin'))
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2018-04-04 22:40:38 +08:00
|
|
|
def test_open_buffer_with_string
|
|
|
|
string = File.read('test/data/rubycode.zip')
|
|
|
|
::Zip::File.open_buffer string do |zf|
|
2019-09-27 05:38:28 +08:00
|
|
|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
2018-04-04 22:40:38 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-07-03 02:17:17 +08:00
|
|
|
def test_open_buffer_with_stringio
|
|
|
|
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
|
|
|
::Zip::File.open_buffer string_io do |zf|
|
2019-09-27 05:38:28 +08:00
|
|
|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
2015-07-03 02:17:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-02-22 03:48:56 +08:00
|
|
|
def test_close_buffer_with_stringio
|
|
|
|
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
|
|
|
zf = ::Zip::File.open_buffer string_io
|
2018-04-04 22:12:22 +08:00
|
|
|
assert_nil zf.close
|
2018-02-22 03:48:56 +08:00
|
|
|
end
|
|
|
|
|
2019-09-07 00:58:38 +08:00
|
|
|
def test_open_buffer_no_op_does_not_change_file
|
|
|
|
Dir.mktmpdir do |tmp|
|
|
|
|
test_zip = File.join(tmp, 'test.zip')
|
|
|
|
FileUtils.cp 'test/data/rubycode.zip', test_zip
|
|
|
|
|
|
|
|
# Note: this may change the file if it is opened with r+b instead of rb.
|
|
|
|
# The 'extra fields' in this particular zip file get reordered.
|
|
|
|
File.open(test_zip, 'rb') do |file|
|
2020-02-09 19:08:43 +08:00
|
|
|
Zip::File.open_buffer(file) do
|
2019-09-07 00:58:38 +08:00
|
|
|
nil # do nothing
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal \
|
|
|
|
File.binread('test/data/rubycode.zip'),
|
|
|
|
File.binread(test_zip)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_open_buffer_close_does_not_change_file
|
|
|
|
Dir.mktmpdir do |tmp|
|
|
|
|
test_zip = File.join(tmp, 'test.zip')
|
|
|
|
FileUtils.cp 'test/data/rubycode.zip', test_zip
|
|
|
|
|
|
|
|
File.open(test_zip, 'rb') do |file|
|
|
|
|
zf = Zip::File.open_buffer(file)
|
|
|
|
refute zf.commit_required?
|
|
|
|
assert_nil zf.close
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal \
|
|
|
|
File.binread('test/data/rubycode.zip'),
|
|
|
|
File.binread(test_zip)
|
|
|
|
end
|
2018-02-22 03:48:56 +08:00
|
|
|
end
|
|
|
|
|
2019-09-06 02:00:34 +08:00
|
|
|
def test_open_buffer_with_io_and_block
|
|
|
|
File.open('test/data/rubycode.zip') do |io|
|
|
|
|
io.set_encoding(Encoding::BINARY) # not strictly required but can be set
|
|
|
|
Zip::File.open_buffer(io) do |zip_io|
|
|
|
|
# left empty on purpose
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-17 03:24:22 +08:00
|
|
|
def test_open_buffer_without_block
|
|
|
|
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
|
|
|
zf = ::Zip::File.open_buffer string_io
|
2019-09-27 05:38:28 +08:00
|
|
|
assert zf.entries.map(&:name).include?('zippedruby1.rb')
|
2017-01-17 03:24:22 +08:00
|
|
|
end
|
|
|
|
|
2014-04-05 05:32:11 +08:00
|
|
|
def test_cleans_up_tempfiles_after_close
|
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.get_output_stream('myFile') do |os|
|
2014-04-05 05:32:11 +08:00
|
|
|
@tempfile_path = os.path
|
2015-03-21 16:27:44 +08:00
|
|
|
os.write 'myFile contains just this'
|
2014-04-05 05:32:11 +08:00
|
|
|
end
|
|
|
|
|
2014-07-16 00:51:29 +08:00
|
|
|
assert_equal(true, File.exist?(@tempfile_path))
|
2014-04-05 05:32:11 +08:00
|
|
|
|
|
|
|
zf.close
|
|
|
|
|
2014-07-16 00:51:29 +08:00
|
|
|
assert_equal(false, File.exist?(@tempfile_path))
|
2014-04-05 05:32:11 +08:00
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_add
|
2015-03-21 16:27:44 +08:00
|
|
|
srcFile = 'test/data/file2.txt'
|
|
|
|
entryName = 'newEntryName.rb'
|
2014-02-07 07:00:38 +08:00
|
|
|
assert(::File.exist?(srcFile))
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
|
|
|
zf.add(entryName, srcFile)
|
|
|
|
zf.close
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('', zfRead.comment)
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(1, zfRead.entries.length)
|
|
|
|
assert_equal(entryName, zfRead.entries.first.name)
|
|
|
|
AssertEntry.assert_contents(srcFile,
|
2019-09-27 05:38:28 +08:00
|
|
|
zfRead.get_input_stream(entryName, &:read))
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2018-05-20 22:34:55 +08:00
|
|
|
def test_add_stored
|
|
|
|
srcFile = 'test/data/file2.txt'
|
|
|
|
entryName = 'newEntryName.rb'
|
|
|
|
assert(::File.exist?(srcFile))
|
|
|
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
|
|
|
zf.add_stored(entryName, srcFile)
|
|
|
|
zf.close
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
|
|
|
entry = zfRead.entries.first
|
|
|
|
assert_equal('', zfRead.comment)
|
|
|
|
assert_equal(1, zfRead.entries.length)
|
|
|
|
assert_equal(entryName, entry.name)
|
2019-09-15 21:58:13 +08:00
|
|
|
assert_equal(File.size(srcFile), entry.size)
|
2018-05-20 22:34:55 +08:00
|
|
|
assert_equal(entry.size, entry.compressed_size)
|
|
|
|
assert_equal(::Zip::Entry::STORED, entry.compression_method)
|
|
|
|
AssertEntry.assert_contents(srcFile,
|
2019-09-27 05:38:28 +08:00
|
|
|
zfRead.get_input_stream(entryName, &:read))
|
2018-05-20 22:34:55 +08:00
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_recover_permissions_after_add_files_to_archive
|
|
|
|
srcZip = TEST_ZIP.zip_name
|
2017-06-29 10:57:12 +08:00
|
|
|
::File.chmod(0o664, srcZip)
|
2015-03-21 16:27:44 +08:00
|
|
|
srcFile = 'test/data/file2.txt'
|
|
|
|
entryName = 'newEntryName.rb'
|
2017-06-29 10:57:12 +08:00
|
|
|
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
2014-02-07 07:00:38 +08:00
|
|
|
assert(::File.exist?(srcZip))
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(srcZip, ::Zip::File::CREATE)
|
|
|
|
zf.add(entryName, srcFile)
|
|
|
|
zf.close
|
2017-06-29 10:57:12 +08:00
|
|
|
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_add_existing_entry_name
|
2015-03-21 16:10:37 +08:00
|
|
|
assert_raises(::Zip::EntryExistsError) do
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_add_existing_entry_name_replace
|
2014-01-21 05:31:06 +08:00
|
|
|
gotCalled = false
|
|
|
|
replacedEntry = nil
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2014-01-21 05:31:06 +08:00
|
|
|
replacedEntry = zf.entries.first.name
|
2019-09-14 23:03:43 +08:00
|
|
|
zf.add(replacedEntry, 'test/data/file2.txt') do
|
|
|
|
gotCalled = true
|
|
|
|
true
|
|
|
|
end
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
assert(gotCalled)
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_contains(zf, replacedEntry, 'test/data/file2.txt')
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_add_directory
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.add(TestFiles::EMPTY_TEST_DIR, TestFiles::EMPTY_TEST_DIR)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2015-03-23 01:03:50 +08:00
|
|
|
dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR + '/' }
|
2014-01-21 05:31:06 +08:00
|
|
|
assert(dirEntry.directory?)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_remove
|
|
|
|
entryToRemove, *remainingEntries = TEST_ZIP.entry_names
|
|
|
|
|
|
|
|
FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
|
|
|
|
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
2019-09-27 05:38:28 +08:00
|
|
|
assert(zf.entries.map(&:name).include?(entryToRemove))
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.remove(entryToRemove)
|
2019-09-27 05:38:28 +08:00
|
|
|
assert(!zf.entries.map(&:name).include?(entryToRemove))
|
|
|
|
assert_equal(zf.entries.map(&:name).sort, remainingEntries.sort)
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.close
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
2019-09-27 05:38:28 +08:00
|
|
|
assert(!zfRead.entries.map(&:name).include?(entryToRemove))
|
|
|
|
assert_equal(zfRead.entries.map(&:name).sort, remainingEntries.sort)
|
2014-01-21 05:31:06 +08:00
|
|
|
zfRead.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rename
|
|
|
|
entryToRename, * = TEST_ZIP.entry_names
|
|
|
|
|
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
2019-09-27 05:38:28 +08:00
|
|
|
assert(zf.entries.map(&:name).include?(entryToRename))
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
contents = zf.read(entryToRename)
|
2015-03-21 16:27:44 +08:00
|
|
|
newName = 'changed entry name'
|
2019-09-27 05:38:28 +08:00
|
|
|
assert(!zf.entries.map(&:name).include?(newName))
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
zf.rename(entryToRename, newName)
|
2019-09-27 05:38:28 +08:00
|
|
|
assert(zf.entries.map(&:name).include?(newName))
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
assert_equal(contents, zf.read(newName))
|
|
|
|
|
|
|
|
zf.close
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
2019-09-27 05:38:28 +08:00
|
|
|
assert(zfRead.entries.map(&:name).include?(newName))
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_equal(contents, zfRead.read(newName))
|
|
|
|
zfRead.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_rename_with_each
|
|
|
|
zf_name = 'test_rename_zip.zip'
|
2015-03-25 00:09:22 +08:00
|
|
|
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
2014-01-21 05:31:06 +08:00
|
|
|
arr = []
|
|
|
|
arr_renamed = []
|
|
|
|
::Zip::File.open(zf_name, ::Zip::File::CREATE) do |zf|
|
|
|
|
zf.mkdir('test')
|
|
|
|
arr << 'test/'
|
|
|
|
arr_renamed << 'Ztest/'
|
2017-06-29 10:57:12 +08:00
|
|
|
%w[a b c d].each do |f|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.get_output_stream("test/#{f}") { |file| file.puts 'aaaa' }
|
|
|
|
arr << "test/#{f}"
|
|
|
|
arr_renamed << "Ztest/#{f}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
zf = ::Zip::File.open(zf_name)
|
|
|
|
assert_equal(zf.entries.map(&:name), arr)
|
|
|
|
zf.close
|
2015-03-21 16:27:44 +08:00
|
|
|
Zip::File.open(zf_name, 'wb') do |z|
|
2014-01-21 05:31:06 +08:00
|
|
|
z.each do |f|
|
|
|
|
z.rename(f, "Z#{f.name}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
zf = ::Zip::File.open(zf_name)
|
|
|
|
assert_equal(zf.entries.map(&:name), arr_renamed)
|
|
|
|
zf.close
|
2015-03-25 00:09:22 +08:00
|
|
|
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_rename_to_existing_entry
|
2014-01-21 05:31:06 +08:00
|
|
|
oldEntries = nil
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
|
|
|
|
2014-01-24 17:37:38 +08:00
|
|
|
assert_raises(::Zip::EntryExistsError) do
|
2014-01-21 05:31:06 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
|
|
|
zf.rename(zf.entries[0], zf.entries[1].name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2019-09-27 05:38:28 +08:00
|
|
|
assert_equal(oldEntries.sort.map(&:name), zf.entries.sort.map(&:name))
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_rename_to_existing_entry_overwrite
|
2014-01-21 05:31:06 +08:00
|
|
|
oldEntries = nil
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
|
|
|
|
|
|
|
gotCalled = false
|
|
|
|
renamedEntryName = nil
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
|
|
|
renamedEntryName = zf.entries[0].name
|
2019-09-14 23:03:43 +08:00
|
|
|
zf.rename(zf.entries[0], zf.entries[1].name) do
|
|
|
|
gotCalled = true
|
|
|
|
true
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
assert(gotCalled)
|
|
|
|
oldEntries.delete_if { |e| e.name == renamedEntryName }
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2019-09-27 05:38:28 +08:00
|
|
|
assert_equal(oldEntries.sort.map(&:name),
|
|
|
|
zf.entries.sort.map(&:name))
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_rename_non_entry
|
2015-03-21 16:27:44 +08:00
|
|
|
nonEntry = 'bogusEntry'
|
|
|
|
target_entry = 'target_entryName'
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
assert(!zf.entries.include?(nonEntry))
|
2015-03-21 16:10:37 +08:00
|
|
|
assert_raises(Errno::ENOENT) { zf.rename(nonEntry, target_entry) }
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.commit
|
|
|
|
assert(!zf.entries.include?(target_entry))
|
|
|
|
ensure
|
|
|
|
zf.close
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_rename_entry_to_existing_entry
|
2014-01-21 05:31:06 +08:00
|
|
|
entry1, entry2, * = TEST_ZIP.entry_names
|
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
2015-03-21 16:10:37 +08:00
|
|
|
assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
|
2014-01-21 05:31:06 +08:00
|
|
|
ensure
|
|
|
|
zf.close
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_replace
|
|
|
|
entryToReplace = TEST_ZIP.entry_names[2]
|
2015-03-21 16:27:44 +08:00
|
|
|
newEntrySrcFilename = 'test/data/file2.txt'
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
zf.replace(entryToReplace, newEntrySrcFilename)
|
|
|
|
|
|
|
|
zf.close
|
|
|
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
2015-03-21 16:16:06 +08:00
|
|
|
AssertEntry.assert_contents(newEntrySrcFilename,
|
2019-09-27 05:38:28 +08:00
|
|
|
zfRead.get_input_stream(entryToReplace, &:read))
|
2015-03-21 16:16:06 +08:00
|
|
|
AssertEntry.assert_contents(TEST_ZIP.entry_names[0],
|
2019-09-27 05:38:28 +08:00
|
|
|
zfRead.get_input_stream(TEST_ZIP.entry_names[0],
|
|
|
|
&:read))
|
2015-03-21 16:16:06 +08:00
|
|
|
AssertEntry.assert_contents(TEST_ZIP.entry_names[1],
|
2019-09-27 05:38:28 +08:00
|
|
|
zfRead.get_input_stream(TEST_ZIP.entry_names[1],
|
|
|
|
&:read))
|
2015-03-21 16:16:06 +08:00
|
|
|
AssertEntry.assert_contents(TEST_ZIP.entry_names[3],
|
2019-09-27 05:38:28 +08:00
|
|
|
zfRead.get_input_stream(TEST_ZIP.entry_names[3],
|
|
|
|
&:read))
|
2014-01-21 05:31:06 +08:00
|
|
|
zfRead.close
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_replace_non_entry
|
2015-03-21 16:27:44 +08:00
|
|
|
entryToReplace = 'nonExistingEntryname'
|
2017-06-29 10:57:12 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_raises(Errno::ENOENT) { zf.replace(entryToReplace, 'test/data/file2.txt') }
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_commit
|
2015-03-21 16:27:44 +08:00
|
|
|
newName = 'renamedFirst'
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
oldName = zf.entries.first
|
|
|
|
zf.rename(oldName, newName)
|
|
|
|
zf.commit
|
|
|
|
|
|
|
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
2019-09-16 02:01:57 +08:00
|
|
|
refute_nil(zfRead.entries.detect { |e| e.name == newName })
|
|
|
|
assert_nil(zfRead.entries.detect { |e| e.name == oldName })
|
2014-01-21 05:31:06 +08:00
|
|
|
zfRead.close
|
|
|
|
|
|
|
|
zf.close
|
2018-04-05 02:45:54 +08:00
|
|
|
res = system("unzip -tqq #{TEST_ZIP.zip_name}")
|
2014-01-24 17:37:38 +08:00
|
|
|
assert_equal(res, true)
|
|
|
|
end
|
|
|
|
|
2014-03-13 05:42:53 +08:00
|
|
|
def test_double_commit(filename = 'test/data/generated/double_commit_test.zip')
|
2014-01-24 17:37:38 +08:00
|
|
|
::FileUtils.touch('test/data/generated/test_double_commit1.txt')
|
|
|
|
::FileUtils.touch('test/data/generated/test_double_commit2.txt')
|
2014-03-13 05:42:53 +08:00
|
|
|
zf = ::Zip::File.open(filename, ::Zip::File::CREATE)
|
2014-01-24 17:37:38 +08:00
|
|
|
zf.add('test1.txt', 'test/data/generated/test_double_commit1.txt')
|
|
|
|
zf.commit
|
|
|
|
zf.add('test2.txt', 'test/data/generated/test_double_commit2.txt')
|
|
|
|
zf.commit
|
|
|
|
zf.close
|
2014-03-13 05:42:53 +08:00
|
|
|
zf2 = ::Zip::File.open(filename)
|
2019-09-16 02:01:57 +08:00
|
|
|
refute_nil(zf2.entries.detect { |e| e.name == 'test1.txt' })
|
|
|
|
refute_nil(zf2.entries.detect { |e| e.name == 'test2.txt' })
|
2018-04-05 02:45:54 +08:00
|
|
|
res = system("unzip -tqq #{filename}")
|
2014-01-24 17:37:38 +08:00
|
|
|
assert_equal(res, true)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2014-03-13 05:42:53 +08:00
|
|
|
def test_double_commit_zip64
|
|
|
|
::Zip.write_zip64_support = true
|
|
|
|
test_double_commit('test/data/generated/double_commit_test64.zip')
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
def test_write_buffer
|
2015-03-21 16:27:44 +08:00
|
|
|
newName = 'renamedFirst'
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
oldName = zf.entries.first
|
|
|
|
zf.rename(oldName, newName)
|
|
|
|
io = ::StringIO.new('')
|
|
|
|
buffer = zf.write_buffer(io)
|
|
|
|
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
|
|
|
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
2019-09-16 02:01:57 +08:00
|
|
|
refute_nil(zfRead.entries.detect { |e| e.name == newName })
|
|
|
|
assert_nil(zfRead.entries.detect { |e| e.name == oldName })
|
2014-01-21 05:31:06 +08:00
|
|
|
zfRead.close
|
|
|
|
|
|
|
|
zf.close
|
|
|
|
end
|
|
|
|
|
|
|
|
# This test tests that after commit, you
|
|
|
|
# can delete the file you used to add the entry to the zip file
|
|
|
|
# with
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_commit_use_zip_entry
|
2014-07-16 00:12:45 +08:00
|
|
|
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
|
2014-01-21 05:31:06 +08:00
|
|
|
zf = ::Zip::File.open(TEST_ZIP.zip_name)
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.add('okToDelete.txt', OK_DELETE_FILE)
|
|
|
|
assert_contains(zf, 'okToDelete.txt')
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.commit
|
2014-07-16 00:12:45 +08:00
|
|
|
File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_contains(zf, 'okToDelete.txt', OK_DELETE_MOVED_FILE)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
2015-03-25 00:31:28 +08:00
|
|
|
# def test_close
|
|
|
|
# zf = ZipFile.new(TEST_ZIP.zip_name)
|
|
|
|
# zf.close
|
|
|
|
# assert_raises(IOError) {
|
|
|
|
# zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
|
|
|
|
# }
|
|
|
|
# end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
def test_compound1
|
2015-03-21 16:27:44 +08:00
|
|
|
renamedName = 'renamedName'
|
2014-01-21 05:31:06 +08:00
|
|
|
filename_to_remove = ''
|
|
|
|
begin
|
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
originalEntries = zf.entries.dup
|
|
|
|
|
|
|
|
assert_not_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
|
|
|
zf.add(TestFiles::RANDOM_ASCII_FILE1,
|
|
|
|
TestFiles::RANDOM_ASCII_FILE1)
|
|
|
|
assert_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
|
|
|
|
|
|
|
entry_to_rename = zf.entries.find { |entry| entry.name.match('longAscii') }
|
|
|
|
zf.rename(entry_to_rename, renamedName)
|
|
|
|
assert_contains(zf, renamedName)
|
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
TestFiles::BINARY_TEST_FILES.each do |filename|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.add(filename, filename)
|
|
|
|
assert_contains(zf, filename)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
|
|
|
assert_contains(zf, originalEntries.last.to_s)
|
|
|
|
filename_to_remove = originalEntries.map(&:to_s).find { |name| name.match('longBinary') }
|
|
|
|
zf.remove(filename_to_remove)
|
|
|
|
assert_not_contains(zf, filename_to_remove)
|
|
|
|
ensure
|
|
|
|
zf.close
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
|
|
|
|
assert_contains(zfRead, renamedName)
|
2015-03-21 16:10:37 +08:00
|
|
|
TestFiles::BINARY_TEST_FILES.each do |filename|
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_contains(zfRead, filename)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_not_contains(zfRead, filename_to_remove)
|
|
|
|
ensure
|
|
|
|
zfRead.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_compound2
|
|
|
|
begin
|
|
|
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
originalEntries = zf.entries.dup
|
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
originalEntries.each do |entry|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.remove(entry)
|
|
|
|
assert_not_contains(zf, entry)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
assert(zf.entries.empty?)
|
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
TestFiles::ASCII_TEST_FILES.each do |filename|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.add(filename, filename)
|
|
|
|
assert_contains(zf, filename)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2019-09-27 05:38:28 +08:00
|
|
|
assert_equal(zf.entries.sort.map(&:name), TestFiles::ASCII_TEST_FILES)
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.rename(TestFiles::ASCII_TEST_FILES[0], 'newName')
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_contains(zf, 'newName')
|
2014-01-21 05:31:06 +08:00
|
|
|
ensure
|
|
|
|
zf.close
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
|
|
|
asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
|
|
|
|
asciiTestFiles.shift
|
2015-03-21 16:10:37 +08:00
|
|
|
asciiTestFiles.each do |filename|
|
2014-01-21 05:31:06 +08:00
|
|
|
assert_contains(zf, filename)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_contains(zf, 'newName')
|
2014-01-21 05:31:06 +08:00
|
|
|
ensure
|
|
|
|
zfRead.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-25 00:02:54 +08:00
|
|
|
def test_change_comment
|
2014-01-21 05:31:06 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.comment = 'my changed comment'
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
zfRead = ::Zip::File.open(TEST_ZIP.zip_name)
|
2015-03-21 16:27:44 +08:00
|
|
|
assert_equal('my changed comment', zfRead.comment)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_preserve_file_order
|
|
|
|
entryNames = nil
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2019-09-27 05:38:28 +08:00
|
|
|
entryNames = zf.entries.map(&:to_s)
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.get_output_stream('a.txt') { |os| os.write 'this is a.txt' }
|
|
|
|
zf.get_output_stream('z.txt') { |os| os.write 'this is z.txt' }
|
|
|
|
zf.get_output_stream('k.txt') { |os| os.write 'this is k.txt' }
|
|
|
|
entryNames << 'a.txt' << 'z.txt' << 'k.txt'
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2015-03-21 16:10:37 +08:00
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2019-09-27 05:38:28 +08:00
|
|
|
assert_equal(entryNames, zf.entries.map(&:to_s))
|
|
|
|
entries = zf.entries.sort_by(&:name).reverse
|
2015-03-21 16:10:37 +08:00
|
|
|
entries.each do |e|
|
2014-01-21 05:31:06 +08:00
|
|
|
zf.remove e
|
2015-03-21 16:27:44 +08:00
|
|
|
zf.get_output_stream(e) { |os| os.write 'foo' }
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2019-09-27 05:38:28 +08:00
|
|
|
entryNames = entries.map(&:to_s)
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
2019-09-27 05:38:28 +08:00
|
|
|
assert_equal(entryNames, zf.entries.map(&:to_s))
|
2015-03-21 16:10:37 +08:00
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_streaming
|
2015-03-21 16:27:44 +08:00
|
|
|
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '../README.md')
|
|
|
|
zname = 'test/data/generated/README.zip'
|
2014-01-21 05:31:06 +08:00
|
|
|
Zip::File.open(zname, Zip::File::CREATE) do |zipfile|
|
|
|
|
zipfile.get_output_stream(File.basename(fname)) do |f|
|
|
|
|
f.puts File.read(fname)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
data = nil
|
2015-02-03 05:40:38 +08:00
|
|
|
File.open(zname, 'rb') do |f|
|
|
|
|
Zip::File.open_buffer(f) do |zipfile|
|
|
|
|
zipfile.each do |entry|
|
|
|
|
next unless entry.name =~ /README.md/
|
2020-02-09 21:13:21 +08:00
|
|
|
|
2015-02-03 05:40:38 +08:00
|
|
|
data = zipfile.read(entry)
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
assert data
|
|
|
|
assert data =~ /Simonov/
|
|
|
|
end
|
|
|
|
|
2015-07-28 14:20:59 +08:00
|
|
|
def test_nonexistant_zip
|
|
|
|
assert_raises(::Zip::Error) do
|
2015-09-30 15:55:50 +08:00
|
|
|
::Zip::File.open('fake.zip')
|
2015-07-28 14:20:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_empty_zip
|
|
|
|
assert_raises(::Zip::Error) do
|
2016-08-28 19:17:17 +08:00
|
|
|
::Zip::File.open(TestFiles::NULL_FILE)
|
2015-07-28 14:20:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-30 15:55:50 +08:00
|
|
|
def test_odd_extra_field
|
|
|
|
entry_count = 0
|
|
|
|
File.open 'test/data/oddExtraField.zip', 'rb' do |zip_io|
|
|
|
|
Zip::File.open_buffer zip_io.read do |zip|
|
2017-06-29 10:57:12 +08:00
|
|
|
zip.each do |_zip_entry|
|
2015-09-30 15:55:50 +08:00
|
|
|
entry_count += 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert_equal 13, entry_count
|
|
|
|
end
|
|
|
|
|
2015-12-19 17:26:20 +08:00
|
|
|
def test_open_xls_does_not_raise_type_error
|
|
|
|
::Zip::File.open('test/data/test.xls')
|
|
|
|
end
|
|
|
|
|
2019-11-01 02:15:17 +08:00
|
|
|
def test_find_get_entry
|
|
|
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
|
|
|
assert_nil zf.find_entry('not_in_here.txt')
|
|
|
|
|
|
|
|
refute_nil zf.find_entry('test/data/generated/empty.txt')
|
|
|
|
|
|
|
|
assert_raises(Errno::ENOENT) do
|
|
|
|
zf.get_entry('not_in_here.txt')
|
|
|
|
end
|
|
|
|
|
|
|
|
# Should not raise anything.
|
|
|
|
zf.get_entry('test/data/generated/empty.txt')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
private
|
2015-03-21 16:19:43 +08:00
|
|
|
|
2020-02-18 07:13:46 +08:00
|
|
|
def assert_contains(zip_file, entry_name, filename = entry_name)
|
|
|
|
refute_nil(
|
|
|
|
zip_file.entries.detect { |e| e.name == entry_name },
|
|
|
|
"entry #{entry_name} not in #{zip_file.entries.join(', ')} in zip file #{zip_file}"
|
|
|
|
)
|
|
|
|
assert_entry_contents(zip_file, entry_name, filename) if File.exist?(filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_not_contains(zip_file, entry_name)
|
|
|
|
assert_nil(
|
|
|
|
zip_file.entries.detect { |e| e.name == entry_name },
|
|
|
|
"entry #{entry_name} in #{zip_file.entries.join(', ')} in zip file #{zip_file}"
|
|
|
|
)
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|
|
|
|
end
|