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 ZipUnicodeFileNamesAndComments < MiniTest::Test
|
2015-03-21 16:27:44 +08:00
|
|
|
FILENAME = File.join(File.dirname(__FILE__), 'test1.zip')
|
2014-01-21 05:31:06 +08:00
|
|
|
|
2020-06-21 00:34:57 +08:00
|
|
|
def teardown
|
|
|
|
::Zip.reset!
|
|
|
|
end
|
|
|
|
|
2014-07-23 17:54:43 +08:00
|
|
|
def test_unicode_file_name
|
2015-03-21 16:27:44 +08:00
|
|
|
file_entrys = ['текстовыйфайл.txt', 'Résumé.txt', '슬레이어스휘.txt']
|
|
|
|
directory_entrys = ['папка/текстовыйфайл.txt', 'Résumé/Résumé.txt', '슬레이어스휘/슬레이어스휘.txt']
|
2014-01-21 05:31:06 +08:00
|
|
|
stream = ::Zip::OutputStream.open(FILENAME) do |io|
|
|
|
|
file_entrys.each do |filename|
|
|
|
|
io.put_next_entry(filename)
|
|
|
|
io.write(filename)
|
|
|
|
end
|
|
|
|
directory_entrys.each do |filepath|
|
|
|
|
io.put_next_entry(filepath)
|
|
|
|
io.write(filepath)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert(!stream.nil?)
|
|
|
|
::Zip::InputStream.open(FILENAME) do |io|
|
|
|
|
file_entrys.each do |filename|
|
|
|
|
entry = io.get_next_entry
|
|
|
|
entry_name = entry.name
|
2015-03-21 16:27:44 +08:00
|
|
|
entry_name = entry_name.force_encoding('UTF-8')
|
2014-01-21 05:31:06 +08:00
|
|
|
assert(filename == entry_name)
|
|
|
|
end
|
|
|
|
directory_entrys.each do |filepath|
|
|
|
|
entry = io.get_next_entry
|
|
|
|
entry_name = entry.name
|
2015-03-21 16:27:44 +08:00
|
|
|
entry_name = entry_name.force_encoding('UTF-8')
|
2014-01-21 05:31:06 +08:00
|
|
|
assert(filepath == entry_name)
|
|
|
|
end
|
|
|
|
end
|
2017-10-18 23:20:56 +08:00
|
|
|
|
|
|
|
::Zip.force_entry_names_encoding = 'UTF-8'
|
|
|
|
::Zip::File.open(FILENAME) do |zip|
|
|
|
|
file_entrys.each do |filename|
|
|
|
|
refute_nil(zip.find_entry(filename))
|
|
|
|
end
|
|
|
|
directory_entrys.each do |filepath|
|
|
|
|
refute_nil(zip.find_entry(filepath))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-01-21 05:31:06 +08:00
|
|
|
::File.unlink(FILENAME)
|
|
|
|
end
|
|
|
|
|
2014-07-23 17:54:43 +08:00
|
|
|
def test_unicode_comment
|
|
|
|
str = '渠道升级'
|
2021-06-09 04:24:52 +08:00
|
|
|
::Zip::File.open(FILENAME, create: true) do |z|
|
2014-07-23 17:54:43 +08:00
|
|
|
z.comment = str
|
|
|
|
end
|
|
|
|
|
|
|
|
::Zip::File.open(FILENAME) do |z|
|
|
|
|
assert(z.comment.force_encoding('UTF-8') == str)
|
|
|
|
end
|
|
|
|
::File.unlink(FILENAME)
|
|
|
|
end
|
2014-01-21 05:31:06 +08:00
|
|
|
end
|