rubyzip 1.1.7 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubyzip might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/README.md +87 -45
- data/Rakefile +3 -4
- data/lib/zip.rb +11 -5
- data/lib/zip/central_directory.rb +8 -8
- data/lib/zip/compressor.rb +1 -2
- data/lib/zip/constants.rb +5 -5
- data/lib/zip/crypto/null_encryption.rb +4 -6
- data/lib/zip/crypto/traditional_encryption.rb +5 -5
- data/lib/zip/decompressor.rb +3 -3
- data/lib/zip/deflater.rb +8 -6
- data/lib/zip/dos_time.rb +5 -6
- data/lib/zip/entry.rb +120 -128
- data/lib/zip/entry_set.rb +14 -14
- data/lib/zip/errors.rb +1 -0
- data/lib/zip/extra_field.rb +8 -8
- data/lib/zip/extra_field/generic.rb +8 -8
- data/lib/zip/extra_field/ntfs.rb +14 -16
- data/lib/zip/extra_field/old_unix.rb +9 -10
- data/lib/zip/extra_field/universal_time.rb +14 -14
- data/lib/zip/extra_field/unix.rb +8 -9
- data/lib/zip/extra_field/zip64.rb +12 -11
- data/lib/zip/extra_field/zip64_placeholder.rb +1 -2
- data/lib/zip/file.rb +81 -81
- data/lib/zip/filesystem.rb +144 -143
- data/lib/zip/inflater.rb +5 -5
- data/lib/zip/input_stream.rb +22 -13
- data/lib/zip/ioextras.rb +1 -3
- data/lib/zip/ioextras/abstract_input_stream.rb +6 -10
- data/lib/zip/ioextras/abstract_output_stream.rb +3 -5
- data/lib/zip/null_compressor.rb +2 -2
- data/lib/zip/null_decompressor.rb +3 -3
- data/lib/zip/null_input_stream.rb +0 -0
- data/lib/zip/output_stream.rb +13 -14
- data/lib/zip/pass_thru_compressor.rb +4 -4
- data/lib/zip/pass_thru_decompressor.rb +3 -4
- data/lib/zip/streamable_directory.rb +2 -2
- data/lib/zip/streamable_stream.rb +3 -3
- data/lib/zip/version.rb +1 -1
- data/samples/example.rb +29 -39
- data/samples/example_filesystem.rb +16 -18
- data/samples/example_recursive.rb +31 -25
- data/samples/{gtkRubyzip.rb → gtk_ruby_zip.rb} +23 -25
- data/samples/qtzip.rb +18 -27
- data/samples/write_simple.rb +12 -13
- data/samples/zipfind.rb +26 -34
- data/test/basic_zip_file_test.rb +11 -15
- data/test/case_sensitivity_test.rb +69 -0
- data/test/central_directory_entry_test.rb +32 -36
- data/test/central_directory_test.rb +46 -50
- data/test/crypto/null_encryption_test.rb +8 -4
- data/test/crypto/traditional_encryption_test.rb +5 -5
- data/test/data/gpbit3stored.zip +0 -0
- data/test/data/notzippedruby.rb +1 -1
- data/test/data/oddExtraField.zip +0 -0
- data/test/data/path_traversal/Makefile +10 -0
- data/test/data/path_traversal/jwilk/README.md +5 -0
- 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 +3 -0
- 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/test.xls +0 -0
- data/test/deflater_test.rb +10 -12
- data/test/encryption_test.rb +2 -2
- data/test/entry_set_test.rb +50 -25
- data/test/entry_test.rb +76 -87
- data/test/errors_test.rb +1 -2
- data/test/extra_field_test.rb +19 -21
- data/test/file_extract_directory_test.rb +12 -14
- data/test/file_extract_test.rb +33 -40
- data/test/file_permissions_test.rb +65 -0
- data/test/file_split_test.rb +24 -27
- data/test/file_test.rb +266 -179
- data/test/filesystem/dir_iterator_test.rb +13 -17
- data/test/filesystem/directory_test.rb +101 -93
- data/test/filesystem/file_mutating_test.rb +52 -65
- data/test/filesystem/file_nonmutating_test.rb +223 -229
- data/test/filesystem/file_stat_test.rb +17 -19
- data/test/gentestfiles.rb +54 -62
- data/test/inflater_test.rb +1 -1
- data/test/input_stream_test.rb +52 -40
- data/test/ioextras/abstract_input_stream_test.rb +22 -23
- data/test/ioextras/abstract_output_stream_test.rb +33 -33
- data/test/ioextras/fake_io_test.rb +1 -1
- data/test/local_entry_test.rb +36 -38
- data/test/output_stream_test.rb +20 -21
- data/test/pass_thru_compressor_test.rb +5 -6
- data/test/pass_thru_decompressor_test.rb +0 -1
- data/test/path_traversal_test.rb +141 -0
- data/test/samples/example_recursive_test.rb +37 -0
- data/test/settings_test.rb +18 -15
- data/test/test_helper.rb +52 -46
- data/test/unicode_file_names_and_comments_test.rb +17 -7
- data/test/zip64_full_test.rb +10 -12
- data/test/zip64_support_test.rb +0 -1
- metadata +94 -65
data/test/errors_test.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
|
2
3
|
require 'test_helper'
|
3
4
|
|
4
5
|
class ErrorsTest < MiniTest::Test
|
5
|
-
|
6
6
|
def test_rescue_legacy_zip_error
|
7
7
|
raise ::Zip::Error
|
8
8
|
rescue ::Zip::ZipError
|
@@ -32,5 +32,4 @@ class ErrorsTest < MiniTest::Test
|
|
32
32
|
raise ::Zip::InternalError
|
33
33
|
rescue ::Zip::ZipInternalError
|
34
34
|
end
|
35
|
-
|
36
35
|
end
|
data/test/extra_field_test.rb
CHANGED
@@ -2,26 +2,26 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ZipExtraFieldTest < MiniTest::Test
|
4
4
|
def test_new
|
5
|
-
extra_pure = ::Zip::ExtraField.new(
|
6
|
-
extra_withstr = ::Zip::ExtraField.new(
|
5
|
+
extra_pure = ::Zip::ExtraField.new('')
|
6
|
+
extra_withstr = ::Zip::ExtraField.new('foo')
|
7
7
|
assert_instance_of(::Zip::ExtraField, extra_pure)
|
8
8
|
assert_instance_of(::Zip::ExtraField, extra_withstr)
|
9
9
|
end
|
10
10
|
|
11
11
|
def test_unknownfield
|
12
|
-
extra = ::Zip::ExtraField.new(
|
13
|
-
assert_equal(extra[
|
14
|
-
extra.merge(
|
15
|
-
assert_equal(extra[
|
16
|
-
extra.merge(
|
17
|
-
assert_equal(extra.to_s,
|
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
18
|
end
|
19
19
|
|
20
20
|
def test_ntfs
|
21
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
22
|
extra = ::Zip::ExtraField.new(str)
|
23
|
-
assert(extra.member?(
|
24
|
-
t = ::Zip::DOSTime.at(
|
23
|
+
assert(extra.member?('NTFS'))
|
24
|
+
t = ::Zip::DOSTime.at(1_410_496_497.405178)
|
25
25
|
assert_equal(t, extra['NTFS'].mtime)
|
26
26
|
assert_equal(t, extra['NTFS'].atime)
|
27
27
|
assert_equal(t, extra['NTFS'].ctime)
|
@@ -29,12 +29,12 @@ class ZipExtraFieldTest < MiniTest::Test
|
|
29
29
|
|
30
30
|
def test_merge
|
31
31
|
str = "UT\x5\0\x3\250$\r@Ux\0\0"
|
32
|
-
extra1 = ::Zip::ExtraField.new(
|
32
|
+
extra1 = ::Zip::ExtraField.new('')
|
33
33
|
extra2 = ::Zip::ExtraField.new(str)
|
34
|
-
assert(!extra1.member?(
|
35
|
-
assert(extra2.member?(
|
34
|
+
assert(!extra1.member?('UniversalTime'))
|
35
|
+
assert(extra2.member?('UniversalTime'))
|
36
36
|
extra1.merge(str)
|
37
|
-
assert_equal(extra1[
|
37
|
+
assert_equal(extra1['UniversalTime'].mtime, extra2['UniversalTime'].mtime)
|
38
38
|
end
|
39
39
|
|
40
40
|
def test_length
|
@@ -42,19 +42,18 @@ class ZipExtraFieldTest < MiniTest::Test
|
|
42
42
|
extra = ::Zip::ExtraField.new(str)
|
43
43
|
assert_equal(extra.local_size, extra.to_local_bin.size)
|
44
44
|
assert_equal(extra.c_dir_size, extra.to_c_dir_bin.size)
|
45
|
-
extra.merge(
|
45
|
+
extra.merge('foo')
|
46
46
|
assert_equal(extra.local_size, extra.to_local_bin.size)
|
47
47
|
assert_equal(extra.c_dir_size, extra.to_c_dir_bin.size)
|
48
48
|
end
|
49
49
|
|
50
|
-
|
51
50
|
def test_to_s
|
52
51
|
str = "UT\x5\0\x3\250$\r@Ux\0\0Te\0\0testit"
|
53
52
|
extra = ::Zip::ExtraField.new(str)
|
54
53
|
assert_instance_of(String, extra.to_s)
|
55
54
|
|
56
55
|
s = extra.to_s
|
57
|
-
extra.merge(
|
56
|
+
extra.merge('foo')
|
58
57
|
assert_equal(s.length + 3, extra.to_s.length)
|
59
58
|
end
|
60
59
|
|
@@ -65,14 +64,13 @@ class ZipExtraFieldTest < MiniTest::Test
|
|
65
64
|
extra3 = ::Zip::ExtraField.new(str)
|
66
65
|
assert_equal(extra1, extra2)
|
67
66
|
|
68
|
-
extra2[
|
67
|
+
extra2['UniversalTime'].mtime = ::Zip::DOSTime.now
|
69
68
|
assert(extra1 != extra2)
|
70
69
|
|
71
|
-
extra3.create(
|
70
|
+
extra3.create('IUnix')
|
72
71
|
assert(extra1 != extra3)
|
73
72
|
|
74
|
-
extra1.create(
|
73
|
+
extra1.create('IUnix')
|
75
74
|
assert_equal(extra1, extra3)
|
76
75
|
end
|
77
|
-
|
78
76
|
end
|
@@ -3,18 +3,17 @@ require 'test_helper'
|
|
3
3
|
class ZipFileExtractDirectoryTest < MiniTest::Test
|
4
4
|
include CommonZipFileFixture
|
5
5
|
|
6
|
-
TEST_OUT_NAME =
|
6
|
+
TEST_OUT_NAME = 'test/data/generated/emptyOutDir'
|
7
7
|
|
8
8
|
def open_zip(&aProc)
|
9
|
-
assert(aProc
|
9
|
+
assert(!aProc.nil?)
|
10
10
|
::Zip::File.open(TestZipFile::TEST_ZIP4.zip_name, &aProc)
|
11
11
|
end
|
12
12
|
|
13
13
|
def extract_test_dir(&aProc)
|
14
|
-
open_zip
|
15
|
-
|zf|
|
14
|
+
open_zip do |zf|
|
16
15
|
zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &aProc)
|
17
|
-
|
16
|
+
end
|
18
17
|
end
|
19
18
|
|
20
19
|
def setup
|
@@ -24,32 +23,31 @@ class ZipFileExtractDirectoryTest < MiniTest::Test
|
|
24
23
|
File.delete(TEST_OUT_NAME) if File.exist? TEST_OUT_NAME
|
25
24
|
end
|
26
25
|
|
27
|
-
def
|
26
|
+
def test_extract_directory
|
28
27
|
extract_test_dir
|
29
28
|
assert(File.directory?(TEST_OUT_NAME))
|
30
29
|
end
|
31
30
|
|
32
|
-
def
|
31
|
+
def test_extract_directory_exists_as_dir
|
33
32
|
Dir.mkdir TEST_OUT_NAME
|
34
33
|
extract_test_dir
|
35
34
|
assert(File.directory?(TEST_OUT_NAME))
|
36
35
|
end
|
37
36
|
|
38
|
-
def
|
39
|
-
File.open(TEST_OUT_NAME,
|
37
|
+
def test_extract_directory_exists_as_file
|
38
|
+
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
|
40
39
|
assert_raises(::Zip::DestinationFileExistsError) { extract_test_dir }
|
41
40
|
end
|
42
41
|
|
43
|
-
def
|
44
|
-
File.open(TEST_OUT_NAME,
|
42
|
+
def test_extract_directory_exists_as_file_overwrite
|
43
|
+
File.open(TEST_OUT_NAME, 'w') { |f| f.puts 'something' }
|
45
44
|
gotCalled = false
|
46
|
-
extract_test_dir
|
47
|
-
|entry, destPath|
|
45
|
+
extract_test_dir do |entry, destPath|
|
48
46
|
gotCalled = true
|
49
47
|
assert_equal(TEST_OUT_NAME, destPath)
|
50
48
|
assert(entry.directory?)
|
51
49
|
true
|
52
|
-
|
50
|
+
end
|
53
51
|
assert(gotCalled)
|
54
52
|
assert(File.directory?(TEST_OUT_NAME))
|
55
53
|
end
|
data/test/file_extract_test.rb
CHANGED
@@ -2,7 +2,7 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ZipFileExtractTest < MiniTest::Test
|
4
4
|
include CommonZipFileFixture
|
5
|
-
EXTRACTED_FILENAME =
|
5
|
+
EXTRACTED_FILENAME = 'test/data/generated/extEntry'
|
6
6
|
ENTRY_TO_EXTRACT, *REMAINING_ENTRIES = TEST_ZIP.entry_names.reverse
|
7
7
|
|
8
8
|
def setup
|
@@ -11,14 +11,12 @@ class ZipFileExtractTest < MiniTest::Test
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def test_extract
|
14
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
15
|
-
|zf|
|
14
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
16
15
|
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
17
16
|
|
18
17
|
assert(File.exist?(EXTRACTED_FILENAME))
|
19
|
-
AssertEntry
|
20
|
-
|
21
|
-
|
18
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
19
|
+
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
22
20
|
|
23
21
|
::File.unlink(EXTRACTED_FILENAME)
|
24
22
|
|
@@ -26,65 +24,60 @@ class ZipFileExtractTest < MiniTest::Test
|
|
26
24
|
entry.extract(EXTRACTED_FILENAME)
|
27
25
|
|
28
26
|
assert(File.exist?(EXTRACTED_FILENAME))
|
29
|
-
AssertEntry
|
30
|
-
|
31
|
-
|
32
|
-
}
|
27
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
28
|
+
entry.get_input_stream { |is| is.read })
|
29
|
+
end
|
33
30
|
end
|
34
31
|
|
35
|
-
def
|
36
|
-
writtenText =
|
37
|
-
::File.open(EXTRACTED_FILENAME,
|
32
|
+
def test_extract_exists
|
33
|
+
writtenText = 'written text'
|
34
|
+
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
38
35
|
|
39
|
-
assert_raises(::Zip::DestinationFileExistsError)
|
40
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
36
|
+
assert_raises(::Zip::DestinationFileExistsError) do
|
37
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
41
38
|
zf.extract(zf.entries.first, EXTRACTED_FILENAME)
|
42
|
-
|
43
|
-
|
44
|
-
File.open(EXTRACTED_FILENAME,
|
39
|
+
end
|
40
|
+
end
|
41
|
+
File.open(EXTRACTED_FILENAME, 'r') do |f|
|
45
42
|
assert_equal(writtenText, f.read)
|
46
|
-
|
43
|
+
end
|
47
44
|
end
|
48
45
|
|
49
|
-
def
|
50
|
-
writtenText =
|
51
|
-
::File.open(EXTRACTED_FILENAME,
|
46
|
+
def test_extract_exists_overwrite
|
47
|
+
writtenText = 'written text'
|
48
|
+
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
52
49
|
|
53
50
|
gotCalledCorrectly = false
|
54
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
55
|
-
|
56
|
-
zf.extract(zf.entries.first, EXTRACTED_FILENAME) {
|
57
|
-
|entry, extractLoc|
|
51
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
52
|
+
zf.extract(zf.entries.first, EXTRACTED_FILENAME) do |entry, extractLoc|
|
58
53
|
gotCalledCorrectly = zf.entries.first == entry &&
|
59
|
-
|
54
|
+
extractLoc == EXTRACTED_FILENAME
|
60
55
|
true
|
61
|
-
|
62
|
-
|
56
|
+
end
|
57
|
+
end
|
63
58
|
|
64
59
|
assert(gotCalledCorrectly)
|
65
|
-
::File.open(EXTRACTED_FILENAME,
|
66
|
-
|f|
|
60
|
+
::File.open(EXTRACTED_FILENAME, 'r') do |f|
|
67
61
|
assert(writtenText != f.read)
|
68
|
-
|
62
|
+
end
|
69
63
|
end
|
70
64
|
|
71
|
-
def
|
65
|
+
def test_extract_non_entry
|
72
66
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
73
|
-
assert_raises(Errno::ENOENT) { zf.extract(
|
67
|
+
assert_raises(Errno::ENOENT) { zf.extract('nonExistingEntry', 'nonExistingEntry') }
|
74
68
|
ensure
|
75
69
|
zf.close if zf
|
76
70
|
end
|
77
71
|
|
78
|
-
def
|
79
|
-
outFile =
|
80
|
-
assert_raises(Errno::ENOENT)
|
72
|
+
def test_extract_non_entry_2
|
73
|
+
outFile = 'outfile'
|
74
|
+
assert_raises(Errno::ENOENT) do
|
81
75
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
82
|
-
nonEntry =
|
76
|
+
nonEntry = 'hotdog-diddelidoo'
|
83
77
|
assert(!zf.entries.include?(nonEntry))
|
84
78
|
zf.extract(nonEntry, outFile)
|
85
79
|
zf.close
|
86
|
-
|
80
|
+
end
|
87
81
|
assert(!File.exist?(outFile))
|
88
82
|
end
|
89
|
-
|
90
83
|
end
|
@@ -0,0 +1,65 @@
|
|
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
CHANGED
@@ -2,9 +2,9 @@ require 'test_helper'
|
|
2
2
|
|
3
3
|
class ZipFileSplitTest < MiniTest::Test
|
4
4
|
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
5
|
-
TEST_ZIP.zip_name =
|
6
|
-
EXTRACTED_FILENAME =
|
7
|
-
UNSPLITTED_FILENAME =
|
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
8
|
ENTRY_TO_EXTRACT = TEST_ZIP.entry_names.first
|
9
9
|
|
10
10
|
def setup
|
@@ -21,40 +21,37 @@ class ZipFileSplitTest < MiniTest::Test
|
|
21
21
|
end
|
22
22
|
|
23
23
|
def test_split_method_respond
|
24
|
-
assert_respond_to ::Zip::File, :split,
|
24
|
+
assert_respond_to ::Zip::File, :split, 'Does not have split class method'
|
25
25
|
end
|
26
26
|
|
27
27
|
def test_split
|
28
|
-
result = ::Zip::File.split(TEST_ZIP.zip_name,
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
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
|
37
36
|
end
|
38
37
|
end
|
38
|
+
end
|
39
39
|
|
40
|
-
|
41
|
-
|
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
|
-
|
40
|
+
::Zip::File.open(UNSPLITTED_FILENAME) do |zf|
|
41
|
+
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
47
42
|
|
48
|
-
|
43
|
+
assert(File.exist?(EXTRACTED_FILENAME))
|
44
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
45
|
+
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
49
46
|
|
50
|
-
|
51
|
-
entry.extract(EXTRACTED_FILENAME)
|
47
|
+
File.unlink(EXTRACTED_FILENAME)
|
52
48
|
|
53
|
-
|
54
|
-
|
55
|
-
entry.get_input_stream() { |is| is.read })
|
49
|
+
entry = zf.get_entry(ENTRY_TO_EXTRACT)
|
50
|
+
entry.extract(EXTRACTED_FILENAME)
|
56
51
|
|
57
|
-
|
52
|
+
assert(File.exist?(EXTRACTED_FILENAME))
|
53
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
54
|
+
entry.get_input_stream { |is| is.read })
|
58
55
|
end
|
59
56
|
end
|
60
57
|
end
|
data/test/file_test.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
|
4
3
|
class ZipFileTest < MiniTest::Test
|
5
4
|
include CommonZipFileFixture
|
5
|
+
include ZipEntryData
|
6
6
|
|
7
7
|
OK_DELETE_FILE = 'test/data/generated/okToDelete.txt'
|
8
8
|
OK_DELETE_MOVED_FILE = 'test/data/generated/okToDeleteMoved.txt'
|
@@ -11,12 +11,12 @@ class ZipFileTest < MiniTest::Test
|
|
11
11
|
::Zip.write_zip64_support = false
|
12
12
|
end
|
13
13
|
|
14
|
-
def
|
15
|
-
comment =
|
14
|
+
def test_create_from_scratch_to_buffer
|
15
|
+
comment = 'a short comment'
|
16
16
|
|
17
17
|
buffer = ::Zip::File.add_buffer do |zf|
|
18
|
-
zf.get_output_stream(
|
19
|
-
zf.mkdir(
|
18
|
+
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
19
|
+
zf.mkdir('dir1')
|
20
20
|
zf.comment = comment
|
21
21
|
end
|
22
22
|
|
@@ -27,12 +27,26 @@ class ZipFileTest < MiniTest::Test
|
|
27
27
|
assert_equal(2, zfRead.entries.length)
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
comment =
|
30
|
+
def test_create_from_scratch
|
31
|
+
comment = 'a short comment'
|
32
32
|
|
33
33
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
34
|
-
zf.get_output_stream(
|
35
|
-
zf.mkdir(
|
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')
|
36
50
|
zf.comment = comment
|
37
51
|
zf.close
|
38
52
|
|
@@ -41,31 +55,33 @@ class ZipFileTest < MiniTest::Test
|
|
41
55
|
assert_equal(2, zfRead.entries.length)
|
42
56
|
end
|
43
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
|
+
|
44
64
|
def test_get_output_stream
|
45
65
|
entryCount = nil
|
46
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
47
|
-
|zf|
|
66
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
48
67
|
entryCount = zf.size
|
49
|
-
zf.get_output_stream('newEntry.txt')
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
assert_equal(
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
os.write "Some data"
|
67
|
-
}
|
68
|
-
assert_equal(entryCount+2, 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)
|
69
85
|
entry = zf.get_entry('entry_with_custom_args.txt')
|
70
86
|
assert_equal(custom_entry_args[0], entry.comment)
|
71
87
|
assert_equal(custom_entry_args[2], entry.compressed_size)
|
@@ -74,26 +90,95 @@ class ZipFileTest < MiniTest::Test
|
|
74
90
|
assert_equal(custom_entry_args[5], entry.size)
|
75
91
|
assert_equal(custom_entry_args[6], entry.time)
|
76
92
|
|
77
|
-
zf.get_output_stream('entry.bin')
|
78
|
-
|os|
|
93
|
+
zf.get_output_stream('entry.bin') do |os|
|
79
94
|
os.write(::File.open('test/data/generated/5entry.zip', 'rb').read)
|
80
|
-
|
81
|
-
|
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
|
82
170
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
assert_equal("Putting stuff in data/generated/empty.txt", zf.read("test/data/generated/empty.txt"))
|
88
|
-
assert_equal(File.open('test/data/generated/5entry.zip', 'rb').read, zf.read("entry.bin"))
|
89
|
-
}
|
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')
|
90
175
|
end
|
91
176
|
|
92
177
|
def test_cleans_up_tempfiles_after_close
|
93
178
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
94
|
-
zf.get_output_stream(
|
179
|
+
zf.get_output_stream('myFile') do |os|
|
95
180
|
@tempfile_path = os.path
|
96
|
-
os.write
|
181
|
+
os.write 'myFile contains just this'
|
97
182
|
end
|
98
183
|
|
99
184
|
assert_equal(true, File.exist?(@tempfile_path))
|
@@ -104,15 +189,15 @@ class ZipFileTest < MiniTest::Test
|
|
104
189
|
end
|
105
190
|
|
106
191
|
def test_add
|
107
|
-
srcFile =
|
108
|
-
entryName =
|
192
|
+
srcFile = 'test/data/file2.txt'
|
193
|
+
entryName = 'newEntryName.rb'
|
109
194
|
assert(::File.exist?(srcFile))
|
110
195
|
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
111
196
|
zf.add(entryName, srcFile)
|
112
197
|
zf.close
|
113
198
|
|
114
199
|
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
115
|
-
assert_equal(
|
200
|
+
assert_equal('', zfRead.comment)
|
116
201
|
assert_equal(1, zfRead.entries.length)
|
117
202
|
assert_equal(entryName, zfRead.entries.first.name)
|
118
203
|
AssertEntry.assert_contents(srcFile,
|
@@ -121,51 +206,46 @@ class ZipFileTest < MiniTest::Test
|
|
121
206
|
|
122
207
|
def test_recover_permissions_after_add_files_to_archive
|
123
208
|
srcZip = TEST_ZIP.zip_name
|
124
|
-
::File.chmod(
|
125
|
-
srcFile =
|
126
|
-
entryName =
|
127
|
-
assert_equal(::File.stat(srcZip).mode,
|
209
|
+
::File.chmod(0o664, srcZip)
|
210
|
+
srcFile = 'test/data/file2.txt'
|
211
|
+
entryName = 'newEntryName.rb'
|
212
|
+
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
128
213
|
assert(::File.exist?(srcZip))
|
129
214
|
zf = ::Zip::File.new(srcZip, ::Zip::File::CREATE)
|
130
215
|
zf.add(entryName, srcFile)
|
131
216
|
zf.close
|
132
|
-
assert_equal(::File.stat(srcZip).mode,
|
217
|
+
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
133
218
|
end
|
134
219
|
|
135
|
-
def
|
136
|
-
assert_raises(::Zip::EntryExistsError)
|
137
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
}
|
220
|
+
def test_add_existing_entry_name
|
221
|
+
assert_raises(::Zip::EntryExistsError) do
|
222
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
223
|
+
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
224
|
+
end
|
225
|
+
end
|
142
226
|
end
|
143
227
|
|
144
|
-
def
|
228
|
+
def test_add_existing_entry_name_replace
|
145
229
|
gotCalled = false
|
146
230
|
replacedEntry = nil
|
147
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
148
|
-
|zf|
|
231
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
149
232
|
replacedEntry = zf.entries.first.name
|
150
|
-
zf.add(replacedEntry,
|
151
|
-
|
233
|
+
zf.add(replacedEntry, 'test/data/file2.txt') { gotCalled = true; true }
|
234
|
+
end
|
152
235
|
assert(gotCalled)
|
153
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
154
|
-
|
155
|
-
|
156
|
-
}
|
236
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
237
|
+
assert_contains(zf, replacedEntry, 'test/data/file2.txt')
|
238
|
+
end
|
157
239
|
end
|
158
240
|
|
159
|
-
def
|
160
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
161
|
-
|zf|
|
241
|
+
def test_add_directory
|
242
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
162
243
|
zf.add(TestFiles::EMPTY_TEST_DIR, TestFiles::EMPTY_TEST_DIR)
|
163
|
-
|
164
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
165
|
-
|
166
|
-
dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR+"/" }
|
244
|
+
end
|
245
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
246
|
+
dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR + '/' }
|
167
247
|
assert(dirEntry.directory?)
|
168
|
-
|
248
|
+
end
|
169
249
|
end
|
170
250
|
|
171
251
|
def test_remove
|
@@ -193,7 +273,7 @@ class ZipFileTest < MiniTest::Test
|
|
193
273
|
assert(zf.entries.map { |e| e.name }.include?(entryToRename))
|
194
274
|
|
195
275
|
contents = zf.read(entryToRename)
|
196
|
-
newName =
|
276
|
+
newName = 'changed entry name'
|
197
277
|
assert(!zf.entries.map { |e| e.name }.include?(newName))
|
198
278
|
|
199
279
|
zf.rename(entryToRename, newName)
|
@@ -211,16 +291,14 @@ class ZipFileTest < MiniTest::Test
|
|
211
291
|
|
212
292
|
def test_rename_with_each
|
213
293
|
zf_name = 'test_rename_zip.zip'
|
214
|
-
if ::File.exist?(zf_name)
|
215
|
-
::File.unlink(zf_name)
|
216
|
-
end
|
294
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
217
295
|
arr = []
|
218
296
|
arr_renamed = []
|
219
297
|
::Zip::File.open(zf_name, ::Zip::File::CREATE) do |zf|
|
220
298
|
zf.mkdir('test')
|
221
299
|
arr << 'test/'
|
222
300
|
arr_renamed << 'Ztest/'
|
223
|
-
%w
|
301
|
+
%w[a b c d].each do |f|
|
224
302
|
zf.get_output_stream("test/#{f}") { |file| file.puts 'aaaa' }
|
225
303
|
arr << "test/#{f}"
|
226
304
|
arr_renamed << "Ztest/#{f}"
|
@@ -229,7 +307,7 @@ class ZipFileTest < MiniTest::Test
|
|
229
307
|
zf = ::Zip::File.open(zf_name)
|
230
308
|
assert_equal(zf.entries.map(&:name), arr)
|
231
309
|
zf.close
|
232
|
-
Zip::File.open(zf_name,
|
310
|
+
Zip::File.open(zf_name, 'wb') do |z|
|
233
311
|
z.each do |f|
|
234
312
|
z.rename(f, "Z#{f.name}")
|
235
313
|
end
|
@@ -237,12 +315,10 @@ class ZipFileTest < MiniTest::Test
|
|
237
315
|
zf = ::Zip::File.open(zf_name)
|
238
316
|
assert_equal(zf.entries.map(&:name), arr_renamed)
|
239
317
|
zf.close
|
240
|
-
if ::File.exist?(zf_name)
|
241
|
-
::File.unlink(zf_name)
|
242
|
-
end
|
318
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
243
319
|
end
|
244
320
|
|
245
|
-
def
|
321
|
+
def test_rename_to_existing_entry
|
246
322
|
oldEntries = nil
|
247
323
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
248
324
|
|
@@ -257,7 +333,7 @@ class ZipFileTest < MiniTest::Test
|
|
257
333
|
end
|
258
334
|
end
|
259
335
|
|
260
|
-
def
|
336
|
+
def test_rename_to_existing_entry_overwrite
|
261
337
|
oldEntries = nil
|
262
338
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
263
339
|
|
@@ -276,61 +352,54 @@ class ZipFileTest < MiniTest::Test
|
|
276
352
|
end
|
277
353
|
end
|
278
354
|
|
279
|
-
def
|
280
|
-
nonEntry =
|
281
|
-
target_entry =
|
355
|
+
def test_rename_non_entry
|
356
|
+
nonEntry = 'bogusEntry'
|
357
|
+
target_entry = 'target_entryName'
|
282
358
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
283
359
|
assert(!zf.entries.include?(nonEntry))
|
284
|
-
assert_raises(Errno::ENOENT) {
|
285
|
-
zf.rename(nonEntry, target_entry)
|
286
|
-
}
|
360
|
+
assert_raises(Errno::ENOENT) { zf.rename(nonEntry, target_entry) }
|
287
361
|
zf.commit
|
288
362
|
assert(!zf.entries.include?(target_entry))
|
289
363
|
ensure
|
290
364
|
zf.close
|
291
365
|
end
|
292
366
|
|
293
|
-
def
|
367
|
+
def test_rename_entry_to_existing_entry
|
294
368
|
entry1, entry2, * = TEST_ZIP.entry_names
|
295
369
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
296
|
-
assert_raises(::Zip::EntryExistsError) {
|
297
|
-
zf.rename(entry1, entry2)
|
298
|
-
}
|
370
|
+
assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
|
299
371
|
ensure
|
300
372
|
zf.close
|
301
373
|
end
|
302
374
|
|
303
375
|
def test_replace
|
304
376
|
entryToReplace = TEST_ZIP.entry_names[2]
|
305
|
-
newEntrySrcFilename =
|
377
|
+
newEntrySrcFilename = 'test/data/file2.txt'
|
306
378
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
307
379
|
zf.replace(entryToReplace, newEntrySrcFilename)
|
308
380
|
|
309
381
|
zf.close
|
310
382
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
311
|
-
AssertEntry
|
312
|
-
|
313
|
-
AssertEntry
|
314
|
-
|
315
|
-
AssertEntry
|
316
|
-
|
317
|
-
AssertEntry
|
318
|
-
|
383
|
+
AssertEntry.assert_contents(newEntrySrcFilename,
|
384
|
+
zfRead.get_input_stream(entryToReplace) { |is| is.read })
|
385
|
+
AssertEntry.assert_contents(TEST_ZIP.entry_names[0],
|
386
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[0]) { |is| is.read })
|
387
|
+
AssertEntry.assert_contents(TEST_ZIP.entry_names[1],
|
388
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[1]) { |is| is.read })
|
389
|
+
AssertEntry.assert_contents(TEST_ZIP.entry_names[3],
|
390
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[3]) { |is| is.read })
|
319
391
|
zfRead.close
|
320
392
|
end
|
321
393
|
|
322
|
-
def
|
323
|
-
entryToReplace =
|
324
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
325
|
-
|
326
|
-
|
327
|
-
zf.replace(entryToReplace, "test/data/file2.txt")
|
328
|
-
}
|
329
|
-
}
|
394
|
+
def test_replace_non_entry
|
395
|
+
entryToReplace = 'nonExistingEntryname'
|
396
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
397
|
+
assert_raises(Errno::ENOENT) { zf.replace(entryToReplace, 'test/data/file2.txt') }
|
398
|
+
end
|
330
399
|
end
|
331
400
|
|
332
401
|
def test_commit
|
333
|
-
newName =
|
402
|
+
newName = 'renamedFirst'
|
334
403
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
335
404
|
oldName = zf.entries.first
|
336
405
|
zf.rename(oldName, newName)
|
@@ -338,11 +407,11 @@ class ZipFileTest < MiniTest::Test
|
|
338
407
|
|
339
408
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
340
409
|
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
341
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }
|
410
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
342
411
|
zfRead.close
|
343
412
|
|
344
413
|
zf.close
|
345
|
-
res = system("unzip -
|
414
|
+
res = system("unzip -tqq #{TEST_ZIP.zip_name}")
|
346
415
|
assert_equal(res, true)
|
347
416
|
end
|
348
417
|
|
@@ -356,9 +425,9 @@ class ZipFileTest < MiniTest::Test
|
|
356
425
|
zf.commit
|
357
426
|
zf.close
|
358
427
|
zf2 = ::Zip::File.open(filename)
|
359
|
-
assert(zf2.entries.detect {|e| e.name == 'test1.txt'} != nil
|
360
|
-
assert(zf2.entries.detect {|e| e.name == 'test2.txt'} != nil
|
361
|
-
res = system("unzip -
|
428
|
+
assert(zf2.entries.detect { |e| e.name == 'test1.txt' } != nil)
|
429
|
+
assert(zf2.entries.detect { |e| e.name == 'test2.txt' } != nil)
|
430
|
+
res = system("unzip -tqq #{filename}")
|
362
431
|
assert_equal(res, true)
|
363
432
|
end
|
364
433
|
|
@@ -368,7 +437,7 @@ class ZipFileTest < MiniTest::Test
|
|
368
437
|
end
|
369
438
|
|
370
439
|
def test_write_buffer
|
371
|
-
newName =
|
440
|
+
newName = 'renamedFirst'
|
372
441
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
373
442
|
oldName = zf.entries.first
|
374
443
|
zf.rename(oldName, newName)
|
@@ -377,7 +446,7 @@ class ZipFileTest < MiniTest::Test
|
|
377
446
|
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
|
378
447
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
379
448
|
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
380
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }
|
449
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
381
450
|
zfRead.close
|
382
451
|
|
383
452
|
zf.close
|
@@ -386,27 +455,26 @@ class ZipFileTest < MiniTest::Test
|
|
386
455
|
# This test tests that after commit, you
|
387
456
|
# can delete the file you used to add the entry to the zip file
|
388
457
|
# with
|
389
|
-
def
|
458
|
+
def test_commit_use_zip_entry
|
390
459
|
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
|
391
460
|
zf = ::Zip::File.open(TEST_ZIP.zip_name)
|
392
|
-
zf.add(
|
393
|
-
assert_contains(zf,
|
461
|
+
zf.add('okToDelete.txt', OK_DELETE_FILE)
|
462
|
+
assert_contains(zf, 'okToDelete.txt')
|
394
463
|
zf.commit
|
395
464
|
File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
|
396
|
-
assert_contains(zf,
|
465
|
+
assert_contains(zf, 'okToDelete.txt', OK_DELETE_MOVED_FILE)
|
397
466
|
end
|
398
467
|
|
399
|
-
# def test_close
|
400
|
-
# zf = ZipFile.new(TEST_ZIP.zip_name)
|
401
|
-
# zf.close
|
402
|
-
# assert_raises(IOError) {
|
403
|
-
# zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
|
404
|
-
# }
|
405
|
-
# end
|
468
|
+
# def test_close
|
469
|
+
# zf = ZipFile.new(TEST_ZIP.zip_name)
|
470
|
+
# zf.close
|
471
|
+
# assert_raises(IOError) {
|
472
|
+
# zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
|
473
|
+
# }
|
474
|
+
# end
|
406
475
|
|
407
476
|
def test_compound1
|
408
|
-
renamedName =
|
409
|
-
originalEntries = []
|
477
|
+
renamedName = 'renamedName'
|
410
478
|
filename_to_remove = ''
|
411
479
|
begin
|
412
480
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
@@ -421,17 +489,15 @@ class ZipFileTest < MiniTest::Test
|
|
421
489
|
zf.rename(entry_to_rename, renamedName)
|
422
490
|
assert_contains(zf, renamedName)
|
423
491
|
|
424
|
-
TestFiles::BINARY_TEST_FILES.each
|
425
|
-
|filename|
|
492
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
426
493
|
zf.add(filename, filename)
|
427
494
|
assert_contains(zf, filename)
|
428
|
-
|
495
|
+
end
|
429
496
|
|
430
497
|
assert_contains(zf, originalEntries.last.to_s)
|
431
498
|
filename_to_remove = originalEntries.map(&:to_s).find { |name| name.match('longBinary') }
|
432
499
|
zf.remove(filename_to_remove)
|
433
500
|
assert_not_contains(zf, filename_to_remove)
|
434
|
-
|
435
501
|
ensure
|
436
502
|
zf.close
|
437
503
|
end
|
@@ -439,10 +505,9 @@ class ZipFileTest < MiniTest::Test
|
|
439
505
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
440
506
|
assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
|
441
507
|
assert_contains(zfRead, renamedName)
|
442
|
-
TestFiles::BINARY_TEST_FILES.each
|
443
|
-
|filename|
|
508
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
444
509
|
assert_contains(zfRead, filename)
|
445
|
-
|
510
|
+
end
|
446
511
|
assert_not_contains(zfRead, filename_to_remove)
|
447
512
|
ensure
|
448
513
|
zfRead.close
|
@@ -454,23 +519,21 @@ class ZipFileTest < MiniTest::Test
|
|
454
519
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
455
520
|
originalEntries = zf.entries.dup
|
456
521
|
|
457
|
-
originalEntries.each
|
458
|
-
|entry|
|
522
|
+
originalEntries.each do |entry|
|
459
523
|
zf.remove(entry)
|
460
524
|
assert_not_contains(zf, entry)
|
461
|
-
|
525
|
+
end
|
462
526
|
assert(zf.entries.empty?)
|
463
527
|
|
464
|
-
TestFiles::ASCII_TEST_FILES.each
|
465
|
-
|filename|
|
528
|
+
TestFiles::ASCII_TEST_FILES.each do |filename|
|
466
529
|
zf.add(filename, filename)
|
467
530
|
assert_contains(zf, filename)
|
468
|
-
|
531
|
+
end
|
469
532
|
assert_equal(zf.entries.sort.map { |e| e.name }, TestFiles::ASCII_TEST_FILES)
|
470
533
|
|
471
|
-
zf.rename(TestFiles::ASCII_TEST_FILES[0],
|
534
|
+
zf.rename(TestFiles::ASCII_TEST_FILES[0], 'newName')
|
472
535
|
assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
|
473
|
-
assert_contains(zf,
|
536
|
+
assert_contains(zf, 'newName')
|
474
537
|
ensure
|
475
538
|
zf.close
|
476
539
|
end
|
@@ -478,56 +541,51 @@ class ZipFileTest < MiniTest::Test
|
|
478
541
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
479
542
|
asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
|
480
543
|
asciiTestFiles.shift
|
481
|
-
asciiTestFiles.each
|
482
|
-
|filename|
|
544
|
+
asciiTestFiles.each do |filename|
|
483
545
|
assert_contains(zf, filename)
|
484
|
-
|
546
|
+
end
|
485
547
|
|
486
|
-
assert_contains(zf,
|
548
|
+
assert_contains(zf, 'newName')
|
487
549
|
ensure
|
488
550
|
zfRead.close
|
489
551
|
end
|
490
552
|
end
|
491
553
|
|
492
|
-
def
|
554
|
+
def test_change_comment
|
493
555
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
494
|
-
zf.comment =
|
556
|
+
zf.comment = 'my changed comment'
|
495
557
|
end
|
496
558
|
zfRead = ::Zip::File.open(TEST_ZIP.zip_name)
|
497
|
-
assert_equal(
|
559
|
+
assert_equal('my changed comment', zfRead.comment)
|
498
560
|
end
|
499
561
|
|
500
562
|
def test_preserve_file_order
|
501
563
|
entryNames = nil
|
502
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
503
|
-
|zf|
|
564
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
504
565
|
entryNames = zf.entries.map { |e| e.to_s }
|
505
|
-
zf.get_output_stream(
|
506
|
-
zf.get_output_stream(
|
507
|
-
zf.get_output_stream(
|
508
|
-
entryNames <<
|
509
|
-
|
510
|
-
|
511
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
512
|
-
|zf|
|
566
|
+
zf.get_output_stream('a.txt') { |os| os.write 'this is a.txt' }
|
567
|
+
zf.get_output_stream('z.txt') { |os| os.write 'this is z.txt' }
|
568
|
+
zf.get_output_stream('k.txt') { |os| os.write 'this is k.txt' }
|
569
|
+
entryNames << 'a.txt' << 'z.txt' << 'k.txt'
|
570
|
+
end
|
571
|
+
|
572
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
513
573
|
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
514
574
|
entries = zf.entries.sort_by { |e| e.name }.reverse
|
515
|
-
entries.each
|
516
|
-
|e|
|
575
|
+
entries.each do |e|
|
517
576
|
zf.remove e
|
518
|
-
zf.get_output_stream(e) { |os| os.write
|
519
|
-
|
577
|
+
zf.get_output_stream(e) { |os| os.write 'foo' }
|
578
|
+
end
|
520
579
|
entryNames = entries.map { |e| e.to_s }
|
521
|
-
|
522
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
523
|
-
|zf|
|
580
|
+
end
|
581
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
524
582
|
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
525
|
-
|
583
|
+
end
|
526
584
|
end
|
527
585
|
|
528
586
|
def test_streaming
|
529
|
-
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)),
|
530
|
-
zname =
|
587
|
+
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '../README.md')
|
588
|
+
zname = 'test/data/generated/README.zip'
|
531
589
|
Zip::File.open(zname, Zip::File::CREATE) do |zipfile|
|
532
590
|
zipfile.get_output_stream(File.basename(fname)) do |f|
|
533
591
|
f.puts File.read(fname)
|
@@ -547,13 +605,42 @@ class ZipFileTest < MiniTest::Test
|
|
547
605
|
assert data =~ /Simonov/
|
548
606
|
end
|
549
607
|
|
608
|
+
def test_nonexistant_zip
|
609
|
+
assert_raises(::Zip::Error) do
|
610
|
+
::Zip::File.open('fake.zip')
|
611
|
+
end
|
612
|
+
end
|
613
|
+
|
614
|
+
def test_empty_zip
|
615
|
+
assert_raises(::Zip::Error) do
|
616
|
+
::Zip::File.open(TestFiles::NULL_FILE)
|
617
|
+
end
|
618
|
+
end
|
619
|
+
|
620
|
+
def test_odd_extra_field
|
621
|
+
entry_count = 0
|
622
|
+
File.open 'test/data/oddExtraField.zip', 'rb' do |zip_io|
|
623
|
+
Zip::File.open_buffer zip_io.read do |zip|
|
624
|
+
zip.each do |_zip_entry|
|
625
|
+
entry_count += 1
|
626
|
+
end
|
627
|
+
end
|
628
|
+
end
|
629
|
+
assert_equal 13, entry_count
|
630
|
+
end
|
631
|
+
|
632
|
+
def test_open_xls_does_not_raise_type_error
|
633
|
+
::Zip::File.open('test/data/test.xls')
|
634
|
+
end
|
635
|
+
|
550
636
|
private
|
637
|
+
|
551
638
|
def assert_contains(zf, entryName, filename = entryName)
|
552
639
|
assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
|
553
|
-
|
640
|
+
assert_entry_contents(zf, entryName, filename) if File.exist?(filename)
|
554
641
|
end
|
555
642
|
|
556
643
|
def assert_not_contains(zf, entryName)
|
557
|
-
assert(zf.entries.detect { |e| e.name == entryName }
|
644
|
+
assert(zf.entries.detect { |e| e.name == entryName }.nil?, "entry #{entryName} in #{zf.entries.join(', ')} in zip file #{zf}")
|
558
645
|
end
|
559
646
|
end
|