rubyzip 1.1.7 → 1.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +143 -54
- data/Rakefile +3 -4
- 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 +132 -128
- data/lib/zip/entry_set.rb +14 -14
- data/lib/zip/errors.rb +2 -0
- 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/extra_field.rb +8 -8
- data/lib/zip/file.rb +88 -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/abstract_input_stream.rb +6 -10
- data/lib/zip/ioextras/abstract_output_stream.rb +3 -5
- data/lib/zip/ioextras.rb +1 -3
- 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/lib/zip.rb +13 -5
- 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 +94 -39
- data/test/file_permissions_test.rb +65 -0
- data/test/file_split_test.rb +24 -27
- data/test/file_test.rb +286 -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 +100 -66
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
|
@@ -10,15 +10,17 @@ class ZipFileExtractTest < MiniTest::Test
|
|
10
10
|
::File.delete(EXTRACTED_FILENAME) if ::File.exist?(EXTRACTED_FILENAME)
|
11
11
|
end
|
12
12
|
|
13
|
+
def teardown
|
14
|
+
::Zip.reset!
|
15
|
+
end
|
16
|
+
|
13
17
|
def test_extract
|
14
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
15
|
-
|zf|
|
18
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
16
19
|
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
17
20
|
|
18
21
|
assert(File.exist?(EXTRACTED_FILENAME))
|
19
|
-
AssertEntry
|
20
|
-
|
21
|
-
|
22
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
23
|
+
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
22
24
|
|
23
25
|
::File.unlink(EXTRACTED_FILENAME)
|
24
26
|
|
@@ -26,65 +28,118 @@ class ZipFileExtractTest < MiniTest::Test
|
|
26
28
|
entry.extract(EXTRACTED_FILENAME)
|
27
29
|
|
28
30
|
assert(File.exist?(EXTRACTED_FILENAME))
|
29
|
-
AssertEntry
|
30
|
-
|
31
|
-
|
32
|
-
}
|
31
|
+
AssertEntry.assert_contents(EXTRACTED_FILENAME,
|
32
|
+
entry.get_input_stream { |is| is.read })
|
33
|
+
end
|
33
34
|
end
|
34
35
|
|
35
|
-
def
|
36
|
-
writtenText =
|
37
|
-
::File.open(EXTRACTED_FILENAME,
|
36
|
+
def test_extract_exists
|
37
|
+
writtenText = 'written text'
|
38
|
+
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
38
39
|
|
39
|
-
assert_raises(::Zip::DestinationFileExistsError)
|
40
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
40
|
+
assert_raises(::Zip::DestinationFileExistsError) do
|
41
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
41
42
|
zf.extract(zf.entries.first, EXTRACTED_FILENAME)
|
42
|
-
|
43
|
-
|
44
|
-
File.open(EXTRACTED_FILENAME,
|
43
|
+
end
|
44
|
+
end
|
45
|
+
File.open(EXTRACTED_FILENAME, 'r') do |f|
|
45
46
|
assert_equal(writtenText, f.read)
|
46
|
-
|
47
|
+
end
|
47
48
|
end
|
48
49
|
|
49
|
-
def
|
50
|
-
writtenText =
|
51
|
-
::File.open(EXTRACTED_FILENAME,
|
50
|
+
def test_extract_exists_overwrite
|
51
|
+
writtenText = 'written text'
|
52
|
+
::File.open(EXTRACTED_FILENAME, 'w') { |f| f.write(writtenText) }
|
52
53
|
|
53
54
|
gotCalledCorrectly = false
|
54
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
55
|
-
|
56
|
-
zf.extract(zf.entries.first, EXTRACTED_FILENAME) {
|
57
|
-
|entry, extractLoc|
|
55
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
56
|
+
zf.extract(zf.entries.first, EXTRACTED_FILENAME) do |entry, extractLoc|
|
58
57
|
gotCalledCorrectly = zf.entries.first == entry &&
|
59
|
-
|
58
|
+
extractLoc == EXTRACTED_FILENAME
|
60
59
|
true
|
61
|
-
|
62
|
-
|
60
|
+
end
|
61
|
+
end
|
63
62
|
|
64
63
|
assert(gotCalledCorrectly)
|
65
|
-
::File.open(EXTRACTED_FILENAME,
|
66
|
-
|f|
|
64
|
+
::File.open(EXTRACTED_FILENAME, 'r') do |f|
|
67
65
|
assert(writtenText != f.read)
|
68
|
-
|
66
|
+
end
|
69
67
|
end
|
70
68
|
|
71
|
-
def
|
69
|
+
def test_extract_non_entry
|
72
70
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
73
|
-
assert_raises(Errno::ENOENT) { zf.extract(
|
71
|
+
assert_raises(Errno::ENOENT) { zf.extract('nonExistingEntry', 'nonExistingEntry') }
|
74
72
|
ensure
|
75
73
|
zf.close if zf
|
76
74
|
end
|
77
75
|
|
78
|
-
def
|
79
|
-
outFile =
|
80
|
-
assert_raises(Errno::ENOENT)
|
76
|
+
def test_extract_non_entry_2
|
77
|
+
outFile = 'outfile'
|
78
|
+
assert_raises(Errno::ENOENT) do
|
81
79
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
82
|
-
nonEntry =
|
80
|
+
nonEntry = 'hotdog-diddelidoo'
|
83
81
|
assert(!zf.entries.include?(nonEntry))
|
84
82
|
zf.extract(nonEntry, outFile)
|
85
83
|
zf.close
|
86
|
-
|
84
|
+
end
|
87
85
|
assert(!File.exist?(outFile))
|
88
86
|
end
|
89
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
|
90
145
|
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
|