rubyzip 1.3.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +2 -8
- data/lib/zip.rb +1 -1
- data/lib/zip/version.rb +1 -1
- metadata +6 -154
- data/test/basic_zip_file_test.rb +0 -60
- data/test/case_sensitivity_test.rb +0 -69
- data/test/central_directory_entry_test.rb +0 -69
- data/test/central_directory_test.rb +0 -100
- data/test/crypto/null_encryption_test.rb +0 -57
- data/test/crypto/traditional_encryption_test.rb +0 -80
- data/test/data/WarnInvalidDate.zip +0 -0
- data/test/data/file1.txt +0 -46
- data/test/data/file1.txt.deflatedData +0 -0
- data/test/data/file2.txt +0 -1504
- data/test/data/globTest.zip +0 -0
- data/test/data/globTest/foo.txt +0 -0
- data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
- data/test/data/globTest/food.txt +0 -0
- data/test/data/gpbit3stored.zip +0 -0
- data/test/data/mimetype +0 -1
- data/test/data/notzippedruby.rb +0 -7
- data/test/data/ntfs.zip +0 -0
- data/test/data/oddExtraField.zip +0 -0
- data/test/data/path_traversal/Makefile +0 -10
- data/test/data/path_traversal/jwilk/README.md +0 -5
- data/test/data/path_traversal/jwilk/absolute1.zip +0 -0
- data/test/data/path_traversal/jwilk/absolute2.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink2a.zip +0 -0
- data/test/data/path_traversal/jwilk/dirsymlink2b.zip +0 -0
- data/test/data/path_traversal/jwilk/relative0.zip +0 -0
- data/test/data/path_traversal/jwilk/relative2.zip +0 -0
- data/test/data/path_traversal/jwilk/symlink.zip +0 -0
- data/test/data/path_traversal/relative1.zip +0 -0
- data/test/data/path_traversal/tilde.zip +0 -0
- data/test/data/path_traversal/tuzovakaoff/README.md +0 -3
- data/test/data/path_traversal/tuzovakaoff/absolutepath.zip +0 -0
- data/test/data/path_traversal/tuzovakaoff/symlink.zip +0 -0
- data/test/data/rubycode.zip +0 -0
- data/test/data/rubycode2.zip +0 -0
- data/test/data/test.xls +0 -0
- data/test/data/testDirectory.bin +0 -0
- data/test/data/zip64-sample.zip +0 -0
- data/test/data/zipWithDirs.zip +0 -0
- data/test/data/zipWithEncryption.zip +0 -0
- data/test/deflater_test.rb +0 -65
- data/test/encryption_test.rb +0 -42
- data/test/entry_set_test.rb +0 -163
- data/test/entry_test.rb +0 -154
- data/test/errors_test.rb +0 -35
- data/test/extra_field_test.rb +0 -76
- data/test/file_extract_directory_test.rb +0 -54
- data/test/file_extract_test.rb +0 -145
- data/test/file_permissions_test.rb +0 -65
- data/test/file_split_test.rb +0 -57
- data/test/file_test.rb +0 -666
- data/test/filesystem/dir_iterator_test.rb +0 -58
- data/test/filesystem/directory_test.rb +0 -139
- data/test/filesystem/file_mutating_test.rb +0 -87
- data/test/filesystem/file_nonmutating_test.rb +0 -508
- data/test/filesystem/file_stat_test.rb +0 -64
- data/test/gentestfiles.rb +0 -126
- data/test/inflater_test.rb +0 -14
- data/test/input_stream_test.rb +0 -182
- data/test/ioextras/abstract_input_stream_test.rb +0 -102
- data/test/ioextras/abstract_output_stream_test.rb +0 -106
- data/test/ioextras/fake_io_test.rb +0 -18
- data/test/local_entry_test.rb +0 -154
- data/test/output_stream_test.rb +0 -128
- data/test/pass_thru_compressor_test.rb +0 -30
- data/test/pass_thru_decompressor_test.rb +0 -14
- data/test/path_traversal_test.rb +0 -141
- data/test/samples/example_recursive_test.rb +0 -37
- data/test/settings_test.rb +0 -95
- data/test/test_helper.rb +0 -234
- data/test/unicode_file_names_and_comments_test.rb +0 -62
- data/test/zip64_full_test.rb +0 -51
- data/test/zip64_support_test.rb +0 -14
data/test/errors_test.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'test_helper'
|
4
|
-
|
5
|
-
class ErrorsTest < MiniTest::Test
|
6
|
-
def test_rescue_legacy_zip_error
|
7
|
-
raise ::Zip::Error
|
8
|
-
rescue ::Zip::ZipError
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_rescue_legacy_zip_entry_exists_error
|
12
|
-
raise ::Zip::EntryExistsError
|
13
|
-
rescue ::Zip::ZipEntryExistsError
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_rescue_legacy_zip_destination_file_exists_error
|
17
|
-
raise ::Zip::DestinationFileExistsError
|
18
|
-
rescue ::Zip::ZipDestinationFileExistsError
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_rescue_legacy_zip_compression_method_error
|
22
|
-
raise ::Zip::CompressionMethodError
|
23
|
-
rescue ::Zip::ZipCompressionMethodError
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_rescue_legacy_zip_entry_name_error
|
27
|
-
raise ::Zip::EntryNameError
|
28
|
-
rescue ::Zip::ZipEntryNameError
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_rescue_legacy_zip_internal_error
|
32
|
-
raise ::Zip::InternalError
|
33
|
-
rescue ::Zip::ZipInternalError
|
34
|
-
end
|
35
|
-
end
|
data/test/extra_field_test.rb
DELETED
@@ -1,76 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipExtraFieldTest < MiniTest::Test
|
4
|
-
def test_new
|
5
|
-
extra_pure = ::Zip::ExtraField.new('')
|
6
|
-
extra_withstr = ::Zip::ExtraField.new('foo')
|
7
|
-
assert_instance_of(::Zip::ExtraField, extra_pure)
|
8
|
-
assert_instance_of(::Zip::ExtraField, extra_withstr)
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_unknownfield
|
12
|
-
extra = ::Zip::ExtraField.new('foo')
|
13
|
-
assert_equal(extra['Unknown'], 'foo')
|
14
|
-
extra.merge('a')
|
15
|
-
assert_equal(extra['Unknown'], 'fooa')
|
16
|
-
extra.merge('barbaz')
|
17
|
-
assert_equal(extra.to_s, 'fooabarbaz')
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_ntfs
|
21
|
-
str = "\x0A\x00 \x00\x00\x00\x00\x00\x01\x00\x18\x00\xC0\x81\x17\xE8B\xCE\xCF\x01\xC0\x81\x17\xE8B\xCE\xCF\x01\xC0\x81\x17\xE8B\xCE\xCF\x01"
|
22
|
-
extra = ::Zip::ExtraField.new(str)
|
23
|
-
assert(extra.member?('NTFS'))
|
24
|
-
t = ::Zip::DOSTime.at(1_410_496_497.405178)
|
25
|
-
assert_equal(t, extra['NTFS'].mtime)
|
26
|
-
assert_equal(t, extra['NTFS'].atime)
|
27
|
-
assert_equal(t, extra['NTFS'].ctime)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_merge
|
31
|
-
str = "UT\x5\0\x3\250$\r@Ux\0\0"
|
32
|
-
extra1 = ::Zip::ExtraField.new('')
|
33
|
-
extra2 = ::Zip::ExtraField.new(str)
|
34
|
-
assert(!extra1.member?('UniversalTime'))
|
35
|
-
assert(extra2.member?('UniversalTime'))
|
36
|
-
extra1.merge(str)
|
37
|
-
assert_equal(extra1['UniversalTime'].mtime, extra2['UniversalTime'].mtime)
|
38
|
-
end
|
39
|
-
|
40
|
-
def test_length
|
41
|
-
str = "UT\x5\0\x3\250$\r@Ux\0\0Te\0\0testit"
|
42
|
-
extra = ::Zip::ExtraField.new(str)
|
43
|
-
assert_equal(extra.local_size, extra.to_local_bin.size)
|
44
|
-
assert_equal(extra.c_dir_size, extra.to_c_dir_bin.size)
|
45
|
-
extra.merge('foo')
|
46
|
-
assert_equal(extra.local_size, extra.to_local_bin.size)
|
47
|
-
assert_equal(extra.c_dir_size, extra.to_c_dir_bin.size)
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_to_s
|
51
|
-
str = "UT\x5\0\x3\250$\r@Ux\0\0Te\0\0testit"
|
52
|
-
extra = ::Zip::ExtraField.new(str)
|
53
|
-
assert_instance_of(String, extra.to_s)
|
54
|
-
|
55
|
-
s = extra.to_s
|
56
|
-
extra.merge('foo')
|
57
|
-
assert_equal(s.length + 3, extra.to_s.length)
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_equality
|
61
|
-
str = "UT\x5\0\x3\250$\r@"
|
62
|
-
extra1 = ::Zip::ExtraField.new(str)
|
63
|
-
extra2 = ::Zip::ExtraField.new(str)
|
64
|
-
extra3 = ::Zip::ExtraField.new(str)
|
65
|
-
assert_equal(extra1, extra2)
|
66
|
-
|
67
|
-
extra2['UniversalTime'].mtime = ::Zip::DOSTime.now
|
68
|
-
assert(extra1 != extra2)
|
69
|
-
|
70
|
-
extra3.create('IUnix')
|
71
|
-
assert(extra1 != extra3)
|
72
|
-
|
73
|
-
extra1.create('IUnix')
|
74
|
-
assert_equal(extra1, extra3)
|
75
|
-
end
|
76
|
-
end
|
@@ -1,54 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipFileExtractDirectoryTest < MiniTest::Test
|
4
|
-
include CommonZipFileFixture
|
5
|
-
|
6
|
-
TEST_OUT_NAME = 'test/data/generated/emptyOutDir'
|
7
|
-
|
8
|
-
def open_zip(&aProc)
|
9
|
-
assert(!aProc.nil?)
|
10
|
-
::Zip::File.open(TestZipFile::TEST_ZIP4.zip_name, &aProc)
|
11
|
-
end
|
12
|
-
|
13
|
-
def extract_test_dir(&aProc)
|
14
|
-
open_zip do |zf|
|
15
|
-
zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &aProc)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
def setup
|
20
|
-
super
|
21
|
-
|
22
|
-
Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME
|
23
|
-
File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_extract_directory
|
27
|
-
extract_test_dir
|
28
|
-
assert(File.directory?(TEST_OUT_NAME))
|
29
|
-
end
|
30
|
-
|
31
|
-
def test_extract_directory_exists_as_dir
|
32
|
-
Dir.mkdir TEST_OUT_NAME
|
33
|
-
extract_test_dir
|
34
|
-
assert(File.directory?(TEST_OUT_NAME))
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_extract_directory_exists_as_file
|
38
|
-
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
|
39
|
-
assert_raises(::Zip::DestinationFileExistsError) { extract_test_dir }
|
40
|
-
end
|
41
|
-
|
42
|
-
def test_extract_directory_exists_as_file_overwrite
|
43
|
-
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
|
44
|
-
gotCalled = false
|
45
|
-
extract_test_dir do |entry, destPath|
|
46
|
-
gotCalled = true
|
47
|
-
assert_equal(TEST_OUT_NAME, destPath)
|
48
|
-
assert(entry.directory?)
|
49
|
-
true
|
50
|
-
end
|
51
|
-
assert(gotCalled)
|
52
|
-
assert(File.directory?(TEST_OUT_NAME))
|
53
|
-
end
|
54
|
-
end
|
data/test/file_extract_test.rb
DELETED
@@ -1,145 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipFileExtractTest < MiniTest::Test
|
4
|
-
include CommonZipFileFixture
|
5
|
-
EXTRACTED_FILENAME = 'test/data/generated/extEntry'
|
6
|
-
ENTRY_TO_EXTRACT, *REMAINING_ENTRIES = TEST_ZIP.entry_names.reverse
|
7
|
-
|
8
|
-
def setup
|
9
|
-
super
|
10
|
-
::File.delete(EXTRACTED_FILENAME) if ::File.exist?(EXTRACTED_FILENAME)
|
11
|
-
end
|
12
|
-
|
13
|
-
def teardown
|
14
|
-
::Zip.reset!
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_extract
|
18
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
19
|
-
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
20
|
-
|
21
|
-
assert(File.exist?(EXTRACTED_FILENAME))
|
22
|
-
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
23
|
-
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
24
|
-
|
25
|
-
::File.unlink(EXTRACTED_FILENAME)
|
26
|
-
|
27
|
-
entry = zf.get_entry(ENTRY_TO_EXTRACT)
|
28
|
-
entry.extract(EXTRACTED_FILENAME)
|
29
|
-
|
30
|
-
assert(File.exist?(EXTRACTED_FILENAME))
|
31
|
-
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
32
|
-
entry.get_input_stream { |is| is.read })
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def test_extract_exists
|
37
|
-
writtenText = 'written text'
|
38
|
-
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
39
|
-
|
40
|
-
assert_raises(::Zip::DestinationFileExistsError) do
|
41
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
42
|
-
zf.extract(zf.entries.first, EXTRACTED_FILENAME)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
File.open(EXTRACTED_FILENAME, 'r') do |f|
|
46
|
-
assert_equal(writtenText, f.read)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_extract_exists_overwrite
|
51
|
-
writtenText = 'written text'
|
52
|
-
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
53
|
-
|
54
|
-
gotCalledCorrectly = false
|
55
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
56
|
-
zf.extract(zf.entries.first, EXTRACTED_FILENAME) do |entry, extractLoc|
|
57
|
-
gotCalledCorrectly = zf.entries.first == entry &&
|
58
|
-
extractLoc == EXTRACTED_FILENAME
|
59
|
-
true
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
assert(gotCalledCorrectly)
|
64
|
-
::File.open(EXTRACTED_FILENAME, 'r') do |f|
|
65
|
-
assert(writtenText != f.read)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
def test_extract_non_entry
|
70
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
71
|
-
assert_raises(Errno::ENOENT) { zf.extract('nonExistingEntry', 'nonExistingEntry') }
|
72
|
-
ensure
|
73
|
-
zf.close if zf
|
74
|
-
end
|
75
|
-
|
76
|
-
def test_extract_non_entry_2
|
77
|
-
outFile = 'outfile'
|
78
|
-
assert_raises(Errno::ENOENT) do
|
79
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
80
|
-
nonEntry = 'hotdog-diddelidoo'
|
81
|
-
assert(!zf.entries.include?(nonEntry))
|
82
|
-
zf.extract(nonEntry, outFile)
|
83
|
-
zf.close
|
84
|
-
end
|
85
|
-
assert(!File.exist?(outFile))
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_extract_incorrect_size
|
89
|
-
# The uncompressed size fields in the zip file cannot be trusted. This makes
|
90
|
-
# it harder for callers to validate the sizes of the files they are
|
91
|
-
# extracting, which can lead to denial of service. See also
|
92
|
-
# https://en.wikipedia.org/wiki/Zip_bomb
|
93
|
-
Dir.mktmpdir do |tmp|
|
94
|
-
real_zip = File.join(tmp, 'real.zip')
|
95
|
-
fake_zip = File.join(tmp, 'fake.zip')
|
96
|
-
file_name = 'a'
|
97
|
-
true_size = 500_000
|
98
|
-
fake_size = 1
|
99
|
-
|
100
|
-
::Zip::File.open(real_zip, ::Zip::File::CREATE) do |zf|
|
101
|
-
zf.get_output_stream(file_name) do |os|
|
102
|
-
os.write 'a' * true_size
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
compressed_size = nil
|
107
|
-
::Zip::File.open(real_zip) do |zf|
|
108
|
-
a_entry = zf.find_entry(file_name)
|
109
|
-
compressed_size = a_entry.compressed_size
|
110
|
-
assert_equal true_size, a_entry.size
|
111
|
-
end
|
112
|
-
|
113
|
-
true_size_bytes = [compressed_size, true_size, file_name.size].pack('LLS')
|
114
|
-
fake_size_bytes = [compressed_size, fake_size, file_name.size].pack('LLS')
|
115
|
-
|
116
|
-
data = File.binread(real_zip)
|
117
|
-
assert data.include?(true_size_bytes)
|
118
|
-
data.gsub! true_size_bytes, fake_size_bytes
|
119
|
-
|
120
|
-
File.open(fake_zip, 'wb') do |file|
|
121
|
-
file.write data
|
122
|
-
end
|
123
|
-
|
124
|
-
Dir.chdir tmp do
|
125
|
-
::Zip::File.open(fake_zip) do |zf|
|
126
|
-
a_entry = zf.find_entry(file_name)
|
127
|
-
assert_equal fake_size, a_entry.size
|
128
|
-
|
129
|
-
::Zip.validate_entry_sizes = false
|
130
|
-
a_entry.extract
|
131
|
-
assert_equal true_size, File.size(file_name)
|
132
|
-
FileUtils.rm file_name
|
133
|
-
|
134
|
-
::Zip.validate_entry_sizes = true
|
135
|
-
error = assert_raises ::Zip::EntrySizeError do
|
136
|
-
a_entry.extract
|
137
|
-
end
|
138
|
-
assert_equal \
|
139
|
-
'Entry a should be 1B but is larger when inflated',
|
140
|
-
error.message
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
end
|
145
|
-
end
|
@@ -1,65 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class FilePermissionsTest < MiniTest::Test
|
4
|
-
ZIPNAME = File.join(File.dirname(__FILE__), 'umask.zip')
|
5
|
-
FILENAME = File.join(File.dirname(__FILE__), 'umask.txt')
|
6
|
-
|
7
|
-
def teardown
|
8
|
-
::File.unlink(ZIPNAME)
|
9
|
-
::File.unlink(FILENAME)
|
10
|
-
end
|
11
|
-
|
12
|
-
def test_current_umask
|
13
|
-
create_files
|
14
|
-
assert_matching_permissions FILENAME, ZIPNAME
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_umask_000
|
18
|
-
set_umask(0o000) do
|
19
|
-
create_files
|
20
|
-
end
|
21
|
-
|
22
|
-
assert_matching_permissions FILENAME, ZIPNAME
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_umask_066
|
26
|
-
set_umask(0o066) do
|
27
|
-
create_files
|
28
|
-
end
|
29
|
-
|
30
|
-
assert_matching_permissions FILENAME, ZIPNAME
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_umask_027
|
34
|
-
set_umask(0o027) do
|
35
|
-
create_files
|
36
|
-
end
|
37
|
-
|
38
|
-
assert_matching_permissions FILENAME, ZIPNAME
|
39
|
-
end
|
40
|
-
|
41
|
-
def assert_matching_permissions(expected_file, actual_file)
|
42
|
-
assert_equal(
|
43
|
-
::File.stat(expected_file).mode.to_s(8).rjust(4, '0'),
|
44
|
-
::File.stat(actual_file).mode.to_s(8).rjust(4, '0')
|
45
|
-
)
|
46
|
-
end
|
47
|
-
|
48
|
-
def create_files
|
49
|
-
::Zip::File.open(ZIPNAME, ::Zip::File::CREATE) do |zip|
|
50
|
-
zip.comment = 'test'
|
51
|
-
end
|
52
|
-
|
53
|
-
::File.open(FILENAME, 'w') do |file|
|
54
|
-
file << 'test'
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
# If anything goes wrong, make sure the umask is restored.
|
59
|
-
def set_umask(umask)
|
60
|
-
saved_umask = ::File.umask(umask)
|
61
|
-
yield
|
62
|
-
ensure
|
63
|
-
::File.umask(saved_umask)
|
64
|
-
end
|
65
|
-
end
|
data/test/file_split_test.rb
DELETED
@@ -1,57 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipFileSplitTest < MiniTest::Test
|
4
|
-
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
5
|
-
TEST_ZIP.zip_name = 'large_zip_file.zip'
|
6
|
-
EXTRACTED_FILENAME = 'test/data/generated/extEntrySplit'
|
7
|
-
UNSPLITTED_FILENAME = 'test/data/generated/unsplitted.zip'
|
8
|
-
ENTRY_TO_EXTRACT = TEST_ZIP.entry_names.first
|
9
|
-
|
10
|
-
def setup
|
11
|
-
FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
12
|
-
end
|
13
|
-
|
14
|
-
def teardown
|
15
|
-
File.delete(TEST_ZIP.zip_name)
|
16
|
-
File.delete(UNSPLITTED_FILENAME) if File.exist?(UNSPLITTED_FILENAME)
|
17
|
-
|
18
|
-
Dir["#{TEST_ZIP.zip_name}.*"].each do |zip_file_name|
|
19
|
-
File.delete(zip_file_name) if File.exist?(zip_file_name)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
def test_split_method_respond
|
24
|
-
assert_respond_to ::Zip::File, :split, 'Does not have split class method'
|
25
|
-
end
|
26
|
-
|
27
|
-
def test_split
|
28
|
-
result = ::Zip::File.split(TEST_ZIP.zip_name, 65_536, false)
|
29
|
-
|
30
|
-
return if result.nil?
|
31
|
-
Dir["#{TEST_ZIP.zip_name}.*"].sort.each_with_index do |zip_file_name, index|
|
32
|
-
File.open(zip_file_name, 'rb') do |zip_file|
|
33
|
-
zip_file.read([::Zip::File::SPLIT_SIGNATURE].pack('V').size) if index == 0
|
34
|
-
File.open(UNSPLITTED_FILENAME, 'ab') do |file|
|
35
|
-
file << zip_file.read
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
::Zip::File.open(UNSPLITTED_FILENAME) do |zf|
|
41
|
-
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
42
|
-
|
43
|
-
assert(File.exist?(EXTRACTED_FILENAME))
|
44
|
-
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
45
|
-
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
46
|
-
|
47
|
-
File.unlink(EXTRACTED_FILENAME)
|
48
|
-
|
49
|
-
entry = zf.get_entry(ENTRY_TO_EXTRACT)
|
50
|
-
entry.extract(EXTRACTED_FILENAME)
|
51
|
-
|
52
|
-
assert(File.exist?(EXTRACTED_FILENAME))
|
53
|
-
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
54
|
-
entry.get_input_stream { |is| is.read })
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end
|
data/test/file_test.rb
DELETED
@@ -1,666 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class ZipFileTest < MiniTest::Test
|
4
|
-
include CommonZipFileFixture
|
5
|
-
include ZipEntryData
|
6
|
-
|
7
|
-
OK_DELETE_FILE = 'test/data/generated/okToDelete.txt'
|
8
|
-
OK_DELETE_MOVED_FILE = 'test/data/generated/okToDeleteMoved.txt'
|
9
|
-
|
10
|
-
def teardown
|
11
|
-
::Zip.write_zip64_support = false
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_create_from_scratch_to_buffer
|
15
|
-
comment = 'a short comment'
|
16
|
-
|
17
|
-
buffer = ::Zip::File.add_buffer do |zf|
|
18
|
-
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
19
|
-
zf.mkdir('dir1')
|
20
|
-
zf.comment = comment
|
21
|
-
end
|
22
|
-
|
23
|
-
::File.open(EMPTY_FILENAME, 'wb') { |file| file.write buffer.string }
|
24
|
-
|
25
|
-
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
26
|
-
assert_equal(comment, zfRead.comment)
|
27
|
-
assert_equal(2, zfRead.entries.length)
|
28
|
-
end
|
29
|
-
|
30
|
-
def test_create_from_scratch
|
31
|
-
comment = 'a short comment'
|
32
|
-
|
33
|
-
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
34
|
-
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
35
|
-
zf.mkdir('dir1')
|
36
|
-
zf.comment = comment
|
37
|
-
zf.close
|
38
|
-
|
39
|
-
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
40
|
-
assert_equal(comment, zfRead.comment)
|
41
|
-
assert_equal(2, zfRead.entries.length)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_create_from_scratch_with_old_create_parameter
|
45
|
-
comment = 'a short comment'
|
46
|
-
|
47
|
-
zf = ::Zip::File.new(EMPTY_FILENAME, 1)
|
48
|
-
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
49
|
-
zf.mkdir('dir1')
|
50
|
-
zf.comment = comment
|
51
|
-
zf.close
|
52
|
-
|
53
|
-
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
54
|
-
assert_equal(comment, zfRead.comment)
|
55
|
-
assert_equal(2, zfRead.entries.length)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_get_input_stream_stored_with_gpflag_bit3
|
59
|
-
::Zip::File.open('test/data/gpbit3stored.zip') do |zf|
|
60
|
-
assert_equal("foo\n", zf.read("foo.txt"))
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def test_get_output_stream
|
65
|
-
entryCount = nil
|
66
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
67
|
-
entryCount = zf.size
|
68
|
-
zf.get_output_stream('newEntry.txt') do |os|
|
69
|
-
os.write 'Putting stuff in newEntry.txt'
|
70
|
-
end
|
71
|
-
assert_equal(entryCount + 1, zf.size)
|
72
|
-
assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
|
73
|
-
|
74
|
-
zf.get_output_stream(zf.get_entry('test/data/generated/empty.txt')) do |os|
|
75
|
-
os.write 'Putting stuff in data/generated/empty.txt'
|
76
|
-
end
|
77
|
-
assert_equal(entryCount + 1, zf.size)
|
78
|
-
assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
|
79
|
-
|
80
|
-
custom_entry_args = [TEST_COMMENT, TEST_EXTRA, TEST_COMPRESSED_SIZE, TEST_CRC, ::Zip::Entry::STORED, TEST_SIZE, TEST_TIME]
|
81
|
-
zf.get_output_stream('entry_with_custom_args.txt', nil, *custom_entry_args) do |os|
|
82
|
-
os.write 'Some data'
|
83
|
-
end
|
84
|
-
assert_equal(entryCount + 2, zf.size)
|
85
|
-
entry = zf.get_entry('entry_with_custom_args.txt')
|
86
|
-
assert_equal(custom_entry_args[0], entry.comment)
|
87
|
-
assert_equal(custom_entry_args[2], entry.compressed_size)
|
88
|
-
assert_equal(custom_entry_args[3], entry.crc)
|
89
|
-
assert_equal(custom_entry_args[4], entry.compression_method)
|
90
|
-
assert_equal(custom_entry_args[5], entry.size)
|
91
|
-
assert_equal(custom_entry_args[6], entry.time)
|
92
|
-
|
93
|
-
zf.get_output_stream('entry.bin') do |os|
|
94
|
-
os.write(::File.open('test/data/generated/5entry.zip', 'rb').read)
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
99
|
-
assert_equal(entryCount + 3, zf.size)
|
100
|
-
assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
|
101
|
-
assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
|
102
|
-
assert_equal(File.open('test/data/generated/5entry.zip', 'rb').read, zf.read('entry.bin'))
|
103
|
-
end
|
104
|
-
end
|
105
|
-
|
106
|
-
def test_open_buffer_with_string
|
107
|
-
string = File.read('test/data/rubycode.zip')
|
108
|
-
::Zip::File.open_buffer string do |zf|
|
109
|
-
assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
def test_open_buffer_with_stringio
|
114
|
-
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
115
|
-
::Zip::File.open_buffer string_io do |zf|
|
116
|
-
assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
def test_close_buffer_with_stringio
|
121
|
-
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
122
|
-
zf = ::Zip::File.open_buffer string_io
|
123
|
-
assert_nil zf.close
|
124
|
-
end
|
125
|
-
|
126
|
-
def test_open_buffer_no_op_does_not_change_file
|
127
|
-
Dir.mktmpdir do |tmp|
|
128
|
-
test_zip = File.join(tmp, 'test.zip')
|
129
|
-
FileUtils.cp 'test/data/rubycode.zip', test_zip
|
130
|
-
|
131
|
-
# Note: this may change the file if it is opened with r+b instead of rb.
|
132
|
-
# The 'extra fields' in this particular zip file get reordered.
|
133
|
-
File.open(test_zip, 'rb') do |file|
|
134
|
-
Zip::File.open_buffer(file) do |zf|
|
135
|
-
nil # do nothing
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
assert_equal \
|
140
|
-
File.binread('test/data/rubycode.zip'),
|
141
|
-
File.binread(test_zip)
|
142
|
-
end
|
143
|
-
end
|
144
|
-
|
145
|
-
def test_open_buffer_close_does_not_change_file
|
146
|
-
Dir.mktmpdir do |tmp|
|
147
|
-
test_zip = File.join(tmp, 'test.zip')
|
148
|
-
FileUtils.cp 'test/data/rubycode.zip', test_zip
|
149
|
-
|
150
|
-
File.open(test_zip, 'rb') do |file|
|
151
|
-
zf = Zip::File.open_buffer(file)
|
152
|
-
refute zf.commit_required?
|
153
|
-
assert_nil zf.close
|
154
|
-
end
|
155
|
-
|
156
|
-
assert_equal \
|
157
|
-
File.binread('test/data/rubycode.zip'),
|
158
|
-
File.binread(test_zip)
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
def test_open_buffer_with_io_and_block
|
163
|
-
File.open('test/data/rubycode.zip') do |io|
|
164
|
-
io.set_encoding(Encoding::BINARY) # not strictly required but can be set
|
165
|
-
Zip::File.open_buffer(io) do |zip_io|
|
166
|
-
# left empty on purpose
|
167
|
-
end
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_open_buffer_without_block
|
172
|
-
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
173
|
-
zf = ::Zip::File.open_buffer string_io
|
174
|
-
assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
|
175
|
-
end
|
176
|
-
|
177
|
-
def test_cleans_up_tempfiles_after_close
|
178
|
-
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
179
|
-
zf.get_output_stream('myFile') do |os|
|
180
|
-
@tempfile_path = os.path
|
181
|
-
os.write 'myFile contains just this'
|
182
|
-
end
|
183
|
-
|
184
|
-
assert_equal(true, File.exist?(@tempfile_path))
|
185
|
-
|
186
|
-
zf.close
|
187
|
-
|
188
|
-
assert_equal(false, File.exist?(@tempfile_path))
|
189
|
-
end
|
190
|
-
|
191
|
-
def test_add
|
192
|
-
srcFile = 'test/data/file2.txt'
|
193
|
-
entryName = 'newEntryName.rb'
|
194
|
-
assert(::File.exist?(srcFile))
|
195
|
-
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
196
|
-
zf.add(entryName, srcFile)
|
197
|
-
zf.close
|
198
|
-
|
199
|
-
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
200
|
-
assert_equal('', zfRead.comment)
|
201
|
-
assert_equal(1, zfRead.entries.length)
|
202
|
-
assert_equal(entryName, zfRead.entries.first.name)
|
203
|
-
AssertEntry.assert_contents(srcFile,
|
204
|
-
zfRead.get_input_stream(entryName) { |zis| zis.read })
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_add_stored
|
208
|
-
srcFile = 'test/data/file2.txt'
|
209
|
-
entryName = 'newEntryName.rb'
|
210
|
-
assert(::File.exist?(srcFile))
|
211
|
-
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
212
|
-
zf.add_stored(entryName, srcFile)
|
213
|
-
zf.close
|
214
|
-
|
215
|
-
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
216
|
-
entry = zfRead.entries.first
|
217
|
-
assert_equal('', zfRead.comment)
|
218
|
-
assert_equal(1, zfRead.entries.length)
|
219
|
-
assert_equal(entryName, entry.name)
|
220
|
-
assert_equal(File.size(srcFile), entry.size)
|
221
|
-
assert_equal(entry.size, entry.compressed_size)
|
222
|
-
assert_equal(::Zip::Entry::STORED, entry.compression_method)
|
223
|
-
AssertEntry.assert_contents(srcFile,
|
224
|
-
zfRead.get_input_stream(entryName) { |zis| zis.read })
|
225
|
-
end
|
226
|
-
|
227
|
-
def test_recover_permissions_after_add_files_to_archive
|
228
|
-
srcZip = TEST_ZIP.zip_name
|
229
|
-
::File.chmod(0o664, srcZip)
|
230
|
-
srcFile = 'test/data/file2.txt'
|
231
|
-
entryName = 'newEntryName.rb'
|
232
|
-
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
233
|
-
assert(::File.exist?(srcZip))
|
234
|
-
zf = ::Zip::File.new(srcZip, ::Zip::File::CREATE)
|
235
|
-
zf.add(entryName, srcFile)
|
236
|
-
zf.close
|
237
|
-
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
238
|
-
end
|
239
|
-
|
240
|
-
def test_add_existing_entry_name
|
241
|
-
assert_raises(::Zip::EntryExistsError) do
|
242
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
243
|
-
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
244
|
-
end
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
def test_add_existing_entry_name_replace
|
249
|
-
gotCalled = false
|
250
|
-
replacedEntry = nil
|
251
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
252
|
-
replacedEntry = zf.entries.first.name
|
253
|
-
zf.add(replacedEntry, 'test/data/file2.txt') { gotCalled = true; true }
|
254
|
-
end
|
255
|
-
assert(gotCalled)
|
256
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
257
|
-
assert_contains(zf, replacedEntry, 'test/data/file2.txt')
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
def test_add_directory
|
262
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
263
|
-
zf.add(TestFiles::EMPTY_TEST_DIR, TestFiles::EMPTY_TEST_DIR)
|
264
|
-
end
|
265
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
266
|
-
dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR + '/' }
|
267
|
-
assert(dirEntry.directory?)
|
268
|
-
end
|
269
|
-
end
|
270
|
-
|
271
|
-
def test_remove
|
272
|
-
entryToRemove, *remainingEntries = TEST_ZIP.entry_names
|
273
|
-
|
274
|
-
FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
275
|
-
|
276
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
277
|
-
assert(zf.entries.map { |e| e.name }.include?(entryToRemove))
|
278
|
-
zf.remove(entryToRemove)
|
279
|
-
assert(!zf.entries.map { |e| e.name }.include?(entryToRemove))
|
280
|
-
assert_equal(zf.entries.map { |x| x.name }.sort, remainingEntries.sort)
|
281
|
-
zf.close
|
282
|
-
|
283
|
-
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
284
|
-
assert(!zfRead.entries.map { |e| e.name }.include?(entryToRemove))
|
285
|
-
assert_equal(zfRead.entries.map { |x| x.name }.sort, remainingEntries.sort)
|
286
|
-
zfRead.close
|
287
|
-
end
|
288
|
-
|
289
|
-
def test_rename
|
290
|
-
entryToRename, * = TEST_ZIP.entry_names
|
291
|
-
|
292
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
293
|
-
assert(zf.entries.map { |e| e.name }.include?(entryToRename))
|
294
|
-
|
295
|
-
contents = zf.read(entryToRename)
|
296
|
-
newName = 'changed entry name'
|
297
|
-
assert(!zf.entries.map { |e| e.name }.include?(newName))
|
298
|
-
|
299
|
-
zf.rename(entryToRename, newName)
|
300
|
-
assert(zf.entries.map { |e| e.name }.include?(newName))
|
301
|
-
|
302
|
-
assert_equal(contents, zf.read(newName))
|
303
|
-
|
304
|
-
zf.close
|
305
|
-
|
306
|
-
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
307
|
-
assert(zfRead.entries.map { |e| e.name }.include?(newName))
|
308
|
-
assert_equal(contents, zfRead.read(newName))
|
309
|
-
zfRead.close
|
310
|
-
end
|
311
|
-
|
312
|
-
def test_rename_with_each
|
313
|
-
zf_name = 'test_rename_zip.zip'
|
314
|
-
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
315
|
-
arr = []
|
316
|
-
arr_renamed = []
|
317
|
-
::Zip::File.open(zf_name, ::Zip::File::CREATE) do |zf|
|
318
|
-
zf.mkdir('test')
|
319
|
-
arr << 'test/'
|
320
|
-
arr_renamed << 'Ztest/'
|
321
|
-
%w[a b c d].each do |f|
|
322
|
-
zf.get_output_stream("test/#{f}") { |file| file.puts 'aaaa' }
|
323
|
-
arr << "test/#{f}"
|
324
|
-
arr_renamed << "Ztest/#{f}"
|
325
|
-
end
|
326
|
-
end
|
327
|
-
zf = ::Zip::File.open(zf_name)
|
328
|
-
assert_equal(zf.entries.map(&:name), arr)
|
329
|
-
zf.close
|
330
|
-
Zip::File.open(zf_name, 'wb') do |z|
|
331
|
-
z.each do |f|
|
332
|
-
z.rename(f, "Z#{f.name}")
|
333
|
-
end
|
334
|
-
end
|
335
|
-
zf = ::Zip::File.open(zf_name)
|
336
|
-
assert_equal(zf.entries.map(&:name), arr_renamed)
|
337
|
-
zf.close
|
338
|
-
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
339
|
-
end
|
340
|
-
|
341
|
-
def test_rename_to_existing_entry
|
342
|
-
oldEntries = nil
|
343
|
-
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
344
|
-
|
345
|
-
assert_raises(::Zip::EntryExistsError) do
|
346
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
347
|
-
zf.rename(zf.entries[0], zf.entries[1].name)
|
348
|
-
end
|
349
|
-
end
|
350
|
-
|
351
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
352
|
-
assert_equal(oldEntries.sort.map { |e| e.name }, zf.entries.sort.map { |e| e.name })
|
353
|
-
end
|
354
|
-
end
|
355
|
-
|
356
|
-
def test_rename_to_existing_entry_overwrite
|
357
|
-
oldEntries = nil
|
358
|
-
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
359
|
-
|
360
|
-
gotCalled = false
|
361
|
-
renamedEntryName = nil
|
362
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
363
|
-
renamedEntryName = zf.entries[0].name
|
364
|
-
zf.rename(zf.entries[0], zf.entries[1].name) { gotCalled = true; true }
|
365
|
-
end
|
366
|
-
|
367
|
-
assert(gotCalled)
|
368
|
-
oldEntries.delete_if { |e| e.name == renamedEntryName }
|
369
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
370
|
-
assert_equal(oldEntries.sort.map { |e| e.name },
|
371
|
-
zf.entries.sort.map { |e| e.name })
|
372
|
-
end
|
373
|
-
end
|
374
|
-
|
375
|
-
def test_rename_non_entry
|
376
|
-
nonEntry = 'bogusEntry'
|
377
|
-
target_entry = 'target_entryName'
|
378
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
379
|
-
assert(!zf.entries.include?(nonEntry))
|
380
|
-
assert_raises(Errno::ENOENT) { zf.rename(nonEntry, target_entry) }
|
381
|
-
zf.commit
|
382
|
-
assert(!zf.entries.include?(target_entry))
|
383
|
-
ensure
|
384
|
-
zf.close
|
385
|
-
end
|
386
|
-
|
387
|
-
def test_rename_entry_to_existing_entry
|
388
|
-
entry1, entry2, * = TEST_ZIP.entry_names
|
389
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
390
|
-
assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
|
391
|
-
ensure
|
392
|
-
zf.close
|
393
|
-
end
|
394
|
-
|
395
|
-
def test_replace
|
396
|
-
entryToReplace = TEST_ZIP.entry_names[2]
|
397
|
-
newEntrySrcFilename = 'test/data/file2.txt'
|
398
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
399
|
-
zf.replace(entryToReplace, newEntrySrcFilename)
|
400
|
-
|
401
|
-
zf.close
|
402
|
-
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
403
|
-
AssertEntry.assert_contents(newEntrySrcFilename,
|
404
|
-
zfRead.get_input_stream(entryToReplace) { |is| is.read })
|
405
|
-
AssertEntry.assert_contents(TEST_ZIP.entry_names[0],
|
406
|
-
zfRead.get_input_stream(TEST_ZIP.entry_names[0]) { |is| is.read })
|
407
|
-
AssertEntry.assert_contents(TEST_ZIP.entry_names[1],
|
408
|
-
zfRead.get_input_stream(TEST_ZIP.entry_names[1]) { |is| is.read })
|
409
|
-
AssertEntry.assert_contents(TEST_ZIP.entry_names[3],
|
410
|
-
zfRead.get_input_stream(TEST_ZIP.entry_names[3]) { |is| is.read })
|
411
|
-
zfRead.close
|
412
|
-
end
|
413
|
-
|
414
|
-
def test_replace_non_entry
|
415
|
-
entryToReplace = 'nonExistingEntryname'
|
416
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
417
|
-
assert_raises(Errno::ENOENT) { zf.replace(entryToReplace, 'test/data/file2.txt') }
|
418
|
-
end
|
419
|
-
end
|
420
|
-
|
421
|
-
def test_commit
|
422
|
-
newName = 'renamedFirst'
|
423
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
424
|
-
oldName = zf.entries.first
|
425
|
-
zf.rename(oldName, newName)
|
426
|
-
zf.commit
|
427
|
-
|
428
|
-
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
429
|
-
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
430
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
431
|
-
zfRead.close
|
432
|
-
|
433
|
-
zf.close
|
434
|
-
res = system("unzip -tqq #{TEST_ZIP.zip_name}")
|
435
|
-
assert_equal(res, true)
|
436
|
-
end
|
437
|
-
|
438
|
-
def test_double_commit(filename = 'test/data/generated/double_commit_test.zip')
|
439
|
-
::FileUtils.touch('test/data/generated/test_double_commit1.txt')
|
440
|
-
::FileUtils.touch('test/data/generated/test_double_commit2.txt')
|
441
|
-
zf = ::Zip::File.open(filename, ::Zip::File::CREATE)
|
442
|
-
zf.add('test1.txt', 'test/data/generated/test_double_commit1.txt')
|
443
|
-
zf.commit
|
444
|
-
zf.add('test2.txt', 'test/data/generated/test_double_commit2.txt')
|
445
|
-
zf.commit
|
446
|
-
zf.close
|
447
|
-
zf2 = ::Zip::File.open(filename)
|
448
|
-
assert(zf2.entries.detect { |e| e.name == 'test1.txt' } != nil)
|
449
|
-
assert(zf2.entries.detect { |e| e.name == 'test2.txt' } != nil)
|
450
|
-
res = system("unzip -tqq #{filename}")
|
451
|
-
assert_equal(res, true)
|
452
|
-
end
|
453
|
-
|
454
|
-
def test_double_commit_zip64
|
455
|
-
::Zip.write_zip64_support = true
|
456
|
-
test_double_commit('test/data/generated/double_commit_test64.zip')
|
457
|
-
end
|
458
|
-
|
459
|
-
def test_write_buffer
|
460
|
-
newName = 'renamedFirst'
|
461
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
462
|
-
oldName = zf.entries.first
|
463
|
-
zf.rename(oldName, newName)
|
464
|
-
io = ::StringIO.new('')
|
465
|
-
buffer = zf.write_buffer(io)
|
466
|
-
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
|
467
|
-
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
468
|
-
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
469
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
470
|
-
zfRead.close
|
471
|
-
|
472
|
-
zf.close
|
473
|
-
end
|
474
|
-
|
475
|
-
# This test tests that after commit, you
|
476
|
-
# can delete the file you used to add the entry to the zip file
|
477
|
-
# with
|
478
|
-
def test_commit_use_zip_entry
|
479
|
-
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
|
480
|
-
zf = ::Zip::File.open(TEST_ZIP.zip_name)
|
481
|
-
zf.add('okToDelete.txt', OK_DELETE_FILE)
|
482
|
-
assert_contains(zf, 'okToDelete.txt')
|
483
|
-
zf.commit
|
484
|
-
File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
|
485
|
-
assert_contains(zf, 'okToDelete.txt', OK_DELETE_MOVED_FILE)
|
486
|
-
end
|
487
|
-
|
488
|
-
# def test_close
|
489
|
-
# zf = ZipFile.new(TEST_ZIP.zip_name)
|
490
|
-
# zf.close
|
491
|
-
# assert_raises(IOError) {
|
492
|
-
# zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
|
493
|
-
# }
|
494
|
-
# end
|
495
|
-
|
496
|
-
def test_compound1
|
497
|
-
renamedName = 'renamedName'
|
498
|
-
filename_to_remove = ''
|
499
|
-
begin
|
500
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
501
|
-
originalEntries = zf.entries.dup
|
502
|
-
|
503
|
-
assert_not_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
504
|
-
zf.add(TestFiles::RANDOM_ASCII_FILE1,
|
505
|
-
TestFiles::RANDOM_ASCII_FILE1)
|
506
|
-
assert_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
507
|
-
|
508
|
-
entry_to_rename = zf.entries.find { |entry| entry.name.match('longAscii') }
|
509
|
-
zf.rename(entry_to_rename, renamedName)
|
510
|
-
assert_contains(zf, renamedName)
|
511
|
-
|
512
|
-
TestFiles::BINARY_TEST_FILES.each do |filename|
|
513
|
-
zf.add(filename, filename)
|
514
|
-
assert_contains(zf, filename)
|
515
|
-
end
|
516
|
-
|
517
|
-
assert_contains(zf, originalEntries.last.to_s)
|
518
|
-
filename_to_remove = originalEntries.map(&:to_s).find { |name| name.match('longBinary') }
|
519
|
-
zf.remove(filename_to_remove)
|
520
|
-
assert_not_contains(zf, filename_to_remove)
|
521
|
-
ensure
|
522
|
-
zf.close
|
523
|
-
end
|
524
|
-
begin
|
525
|
-
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
526
|
-
assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
|
527
|
-
assert_contains(zfRead, renamedName)
|
528
|
-
TestFiles::BINARY_TEST_FILES.each do |filename|
|
529
|
-
assert_contains(zfRead, filename)
|
530
|
-
end
|
531
|
-
assert_not_contains(zfRead, filename_to_remove)
|
532
|
-
ensure
|
533
|
-
zfRead.close
|
534
|
-
end
|
535
|
-
end
|
536
|
-
|
537
|
-
def test_compound2
|
538
|
-
begin
|
539
|
-
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
540
|
-
originalEntries = zf.entries.dup
|
541
|
-
|
542
|
-
originalEntries.each do |entry|
|
543
|
-
zf.remove(entry)
|
544
|
-
assert_not_contains(zf, entry)
|
545
|
-
end
|
546
|
-
assert(zf.entries.empty?)
|
547
|
-
|
548
|
-
TestFiles::ASCII_TEST_FILES.each do |filename|
|
549
|
-
zf.add(filename, filename)
|
550
|
-
assert_contains(zf, filename)
|
551
|
-
end
|
552
|
-
assert_equal(zf.entries.sort.map { |e| e.name }, TestFiles::ASCII_TEST_FILES)
|
553
|
-
|
554
|
-
zf.rename(TestFiles::ASCII_TEST_FILES[0], 'newName')
|
555
|
-
assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
|
556
|
-
assert_contains(zf, 'newName')
|
557
|
-
ensure
|
558
|
-
zf.close
|
559
|
-
end
|
560
|
-
begin
|
561
|
-
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
562
|
-
asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
|
563
|
-
asciiTestFiles.shift
|
564
|
-
asciiTestFiles.each do |filename|
|
565
|
-
assert_contains(zf, filename)
|
566
|
-
end
|
567
|
-
|
568
|
-
assert_contains(zf, 'newName')
|
569
|
-
ensure
|
570
|
-
zfRead.close
|
571
|
-
end
|
572
|
-
end
|
573
|
-
|
574
|
-
def test_change_comment
|
575
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
576
|
-
zf.comment = 'my changed comment'
|
577
|
-
end
|
578
|
-
zfRead = ::Zip::File.open(TEST_ZIP.zip_name)
|
579
|
-
assert_equal('my changed comment', zfRead.comment)
|
580
|
-
end
|
581
|
-
|
582
|
-
def test_preserve_file_order
|
583
|
-
entryNames = nil
|
584
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
585
|
-
entryNames = zf.entries.map { |e| e.to_s }
|
586
|
-
zf.get_output_stream('a.txt') { |os| os.write 'this is a.txt' }
|
587
|
-
zf.get_output_stream('z.txt') { |os| os.write 'this is z.txt' }
|
588
|
-
zf.get_output_stream('k.txt') { |os| os.write 'this is k.txt' }
|
589
|
-
entryNames << 'a.txt' << 'z.txt' << 'k.txt'
|
590
|
-
end
|
591
|
-
|
592
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
593
|
-
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
594
|
-
entries = zf.entries.sort_by { |e| e.name }.reverse
|
595
|
-
entries.each do |e|
|
596
|
-
zf.remove e
|
597
|
-
zf.get_output_stream(e) { |os| os.write 'foo' }
|
598
|
-
end
|
599
|
-
entryNames = entries.map { |e| e.to_s }
|
600
|
-
end
|
601
|
-
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
602
|
-
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
603
|
-
end
|
604
|
-
end
|
605
|
-
|
606
|
-
def test_streaming
|
607
|
-
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '../README.md')
|
608
|
-
zname = 'test/data/generated/README.zip'
|
609
|
-
Zip::File.open(zname, Zip::File::CREATE) do |zipfile|
|
610
|
-
zipfile.get_output_stream(File.basename(fname)) do |f|
|
611
|
-
f.puts File.read(fname)
|
612
|
-
end
|
613
|
-
end
|
614
|
-
|
615
|
-
data = nil
|
616
|
-
File.open(zname, 'rb') do |f|
|
617
|
-
Zip::File.open_buffer(f) do |zipfile|
|
618
|
-
zipfile.each do |entry|
|
619
|
-
next unless entry.name =~ /README.md/
|
620
|
-
data = zipfile.read(entry)
|
621
|
-
end
|
622
|
-
end
|
623
|
-
end
|
624
|
-
assert data
|
625
|
-
assert data =~ /Simonov/
|
626
|
-
end
|
627
|
-
|
628
|
-
def test_nonexistant_zip
|
629
|
-
assert_raises(::Zip::Error) do
|
630
|
-
::Zip::File.open('fake.zip')
|
631
|
-
end
|
632
|
-
end
|
633
|
-
|
634
|
-
def test_empty_zip
|
635
|
-
assert_raises(::Zip::Error) do
|
636
|
-
::Zip::File.open(TestFiles::NULL_FILE)
|
637
|
-
end
|
638
|
-
end
|
639
|
-
|
640
|
-
def test_odd_extra_field
|
641
|
-
entry_count = 0
|
642
|
-
File.open 'test/data/oddExtraField.zip', 'rb' do |zip_io|
|
643
|
-
Zip::File.open_buffer zip_io.read do |zip|
|
644
|
-
zip.each do |_zip_entry|
|
645
|
-
entry_count += 1
|
646
|
-
end
|
647
|
-
end
|
648
|
-
end
|
649
|
-
assert_equal 13, entry_count
|
650
|
-
end
|
651
|
-
|
652
|
-
def test_open_xls_does_not_raise_type_error
|
653
|
-
::Zip::File.open('test/data/test.xls')
|
654
|
-
end
|
655
|
-
|
656
|
-
private
|
657
|
-
|
658
|
-
def assert_contains(zf, entryName, filename = entryName)
|
659
|
-
assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
|
660
|
-
assert_entry_contents(zf, entryName, filename) if File.exist?(filename)
|
661
|
-
end
|
662
|
-
|
663
|
-
def assert_not_contains(zf, entryName)
|
664
|
-
assert(zf.entries.detect { |e| e.name == entryName }.nil?, "entry #{entryName} in #{zf.entries.join(', ')} in zip file #{zf}")
|
665
|
-
end
|
666
|
-
end
|