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/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,12 @@ 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
36
|
zf.comment = comment
|
37
37
|
zf.close
|
38
38
|
|
@@ -41,31 +41,47 @@ class ZipFileTest < MiniTest::Test
|
|
41
41
|
assert_equal(2, zfRead.entries.length)
|
42
42
|
end
|
43
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
|
+
|
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
|
82
155
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
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')
|
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,68 +189,83 @@ 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,
|
119
204
|
zfRead.get_input_stream(entryName) { |zis| zis.read })
|
120
205
|
end
|
121
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
|
+
|
122
227
|
def test_recover_permissions_after_add_files_to_archive
|
123
228
|
srcZip = TEST_ZIP.zip_name
|
124
|
-
::File.chmod(
|
125
|
-
srcFile =
|
126
|
-
entryName =
|
127
|
-
assert_equal(::File.stat(srcZip).mode,
|
229
|
+
::File.chmod(0o664, srcZip)
|
230
|
+
srcFile = 'test/data/file2.txt'
|
231
|
+
entryName = 'newEntryName.rb'
|
232
|
+
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
128
233
|
assert(::File.exist?(srcZip))
|
129
234
|
zf = ::Zip::File.new(srcZip, ::Zip::File::CREATE)
|
130
235
|
zf.add(entryName, srcFile)
|
131
236
|
zf.close
|
132
|
-
assert_equal(::File.stat(srcZip).mode,
|
237
|
+
assert_equal(::File.stat(srcZip).mode, 0o100664)
|
133
238
|
end
|
134
239
|
|
135
|
-
def
|
136
|
-
assert_raises(::Zip::EntryExistsError)
|
137
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
}
|
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
|
142
246
|
end
|
143
247
|
|
144
|
-
def
|
248
|
+
def test_add_existing_entry_name_replace
|
145
249
|
gotCalled = false
|
146
250
|
replacedEntry = nil
|
147
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
148
|
-
|zf|
|
251
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
149
252
|
replacedEntry = zf.entries.first.name
|
150
|
-
zf.add(replacedEntry,
|
151
|
-
|
253
|
+
zf.add(replacedEntry, 'test/data/file2.txt') { gotCalled = true; true }
|
254
|
+
end
|
152
255
|
assert(gotCalled)
|
153
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
154
|
-
|
155
|
-
|
156
|
-
}
|
256
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
257
|
+
assert_contains(zf, replacedEntry, 'test/data/file2.txt')
|
258
|
+
end
|
157
259
|
end
|
158
260
|
|
159
|
-
def
|
160
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
161
|
-
|zf|
|
261
|
+
def test_add_directory
|
262
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
162
263
|
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+"/" }
|
264
|
+
end
|
265
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
266
|
+
dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR + '/' }
|
167
267
|
assert(dirEntry.directory?)
|
168
|
-
|
268
|
+
end
|
169
269
|
end
|
170
270
|
|
171
271
|
def test_remove
|
@@ -193,7 +293,7 @@ class ZipFileTest < MiniTest::Test
|
|
193
293
|
assert(zf.entries.map { |e| e.name }.include?(entryToRename))
|
194
294
|
|
195
295
|
contents = zf.read(entryToRename)
|
196
|
-
newName =
|
296
|
+
newName = 'changed entry name'
|
197
297
|
assert(!zf.entries.map { |e| e.name }.include?(newName))
|
198
298
|
|
199
299
|
zf.rename(entryToRename, newName)
|
@@ -211,16 +311,14 @@ class ZipFileTest < MiniTest::Test
|
|
211
311
|
|
212
312
|
def test_rename_with_each
|
213
313
|
zf_name = 'test_rename_zip.zip'
|
214
|
-
if ::File.exist?(zf_name)
|
215
|
-
::File.unlink(zf_name)
|
216
|
-
end
|
314
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
217
315
|
arr = []
|
218
316
|
arr_renamed = []
|
219
317
|
::Zip::File.open(zf_name, ::Zip::File::CREATE) do |zf|
|
220
318
|
zf.mkdir('test')
|
221
319
|
arr << 'test/'
|
222
320
|
arr_renamed << 'Ztest/'
|
223
|
-
%w
|
321
|
+
%w[a b c d].each do |f|
|
224
322
|
zf.get_output_stream("test/#{f}") { |file| file.puts 'aaaa' }
|
225
323
|
arr << "test/#{f}"
|
226
324
|
arr_renamed << "Ztest/#{f}"
|
@@ -229,7 +327,7 @@ class ZipFileTest < MiniTest::Test
|
|
229
327
|
zf = ::Zip::File.open(zf_name)
|
230
328
|
assert_equal(zf.entries.map(&:name), arr)
|
231
329
|
zf.close
|
232
|
-
Zip::File.open(zf_name,
|
330
|
+
Zip::File.open(zf_name, 'wb') do |z|
|
233
331
|
z.each do |f|
|
234
332
|
z.rename(f, "Z#{f.name}")
|
235
333
|
end
|
@@ -237,12 +335,10 @@ class ZipFileTest < MiniTest::Test
|
|
237
335
|
zf = ::Zip::File.open(zf_name)
|
238
336
|
assert_equal(zf.entries.map(&:name), arr_renamed)
|
239
337
|
zf.close
|
240
|
-
if ::File.exist?(zf_name)
|
241
|
-
::File.unlink(zf_name)
|
242
|
-
end
|
338
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
243
339
|
end
|
244
340
|
|
245
|
-
def
|
341
|
+
def test_rename_to_existing_entry
|
246
342
|
oldEntries = nil
|
247
343
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
248
344
|
|
@@ -257,7 +353,7 @@ class ZipFileTest < MiniTest::Test
|
|
257
353
|
end
|
258
354
|
end
|
259
355
|
|
260
|
-
def
|
356
|
+
def test_rename_to_existing_entry_overwrite
|
261
357
|
oldEntries = nil
|
262
358
|
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
263
359
|
|
@@ -276,61 +372,54 @@ class ZipFileTest < MiniTest::Test
|
|
276
372
|
end
|
277
373
|
end
|
278
374
|
|
279
|
-
def
|
280
|
-
nonEntry =
|
281
|
-
target_entry =
|
375
|
+
def test_rename_non_entry
|
376
|
+
nonEntry = 'bogusEntry'
|
377
|
+
target_entry = 'target_entryName'
|
282
378
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
283
379
|
assert(!zf.entries.include?(nonEntry))
|
284
|
-
assert_raises(Errno::ENOENT) {
|
285
|
-
zf.rename(nonEntry, target_entry)
|
286
|
-
}
|
380
|
+
assert_raises(Errno::ENOENT) { zf.rename(nonEntry, target_entry) }
|
287
381
|
zf.commit
|
288
382
|
assert(!zf.entries.include?(target_entry))
|
289
383
|
ensure
|
290
384
|
zf.close
|
291
385
|
end
|
292
386
|
|
293
|
-
def
|
387
|
+
def test_rename_entry_to_existing_entry
|
294
388
|
entry1, entry2, * = TEST_ZIP.entry_names
|
295
389
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
296
|
-
assert_raises(::Zip::EntryExistsError) {
|
297
|
-
zf.rename(entry1, entry2)
|
298
|
-
}
|
390
|
+
assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
|
299
391
|
ensure
|
300
392
|
zf.close
|
301
393
|
end
|
302
394
|
|
303
395
|
def test_replace
|
304
396
|
entryToReplace = TEST_ZIP.entry_names[2]
|
305
|
-
newEntrySrcFilename =
|
397
|
+
newEntrySrcFilename = 'test/data/file2.txt'
|
306
398
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
307
399
|
zf.replace(entryToReplace, newEntrySrcFilename)
|
308
400
|
|
309
401
|
zf.close
|
310
402
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
311
|
-
AssertEntry
|
312
|
-
|
313
|
-
AssertEntry
|
314
|
-
|
315
|
-
AssertEntry
|
316
|
-
|
317
|
-
AssertEntry
|
318
|
-
|
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 })
|
319
411
|
zfRead.close
|
320
412
|
end
|
321
413
|
|
322
|
-
def
|
323
|
-
entryToReplace =
|
324
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
325
|
-
|
326
|
-
|
327
|
-
zf.replace(entryToReplace, "test/data/file2.txt")
|
328
|
-
}
|
329
|
-
}
|
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
|
330
419
|
end
|
331
420
|
|
332
421
|
def test_commit
|
333
|
-
newName =
|
422
|
+
newName = 'renamedFirst'
|
334
423
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
335
424
|
oldName = zf.entries.first
|
336
425
|
zf.rename(oldName, newName)
|
@@ -338,11 +427,11 @@ class ZipFileTest < MiniTest::Test
|
|
338
427
|
|
339
428
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
340
429
|
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
341
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }
|
430
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
342
431
|
zfRead.close
|
343
432
|
|
344
433
|
zf.close
|
345
|
-
res = system("unzip -
|
434
|
+
res = system("unzip -tqq #{TEST_ZIP.zip_name}")
|
346
435
|
assert_equal(res, true)
|
347
436
|
end
|
348
437
|
|
@@ -356,9 +445,9 @@ class ZipFileTest < MiniTest::Test
|
|
356
445
|
zf.commit
|
357
446
|
zf.close
|
358
447
|
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 -
|
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}")
|
362
451
|
assert_equal(res, true)
|
363
452
|
end
|
364
453
|
|
@@ -368,7 +457,7 @@ class ZipFileTest < MiniTest::Test
|
|
368
457
|
end
|
369
458
|
|
370
459
|
def test_write_buffer
|
371
|
-
newName =
|
460
|
+
newName = 'renamedFirst'
|
372
461
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
373
462
|
oldName = zf.entries.first
|
374
463
|
zf.rename(oldName, newName)
|
@@ -377,7 +466,7 @@ class ZipFileTest < MiniTest::Test
|
|
377
466
|
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
|
378
467
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
379
468
|
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
380
|
-
assert(zfRead.entries.detect { |e| e.name == oldName }
|
469
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
381
470
|
zfRead.close
|
382
471
|
|
383
472
|
zf.close
|
@@ -386,27 +475,26 @@ class ZipFileTest < MiniTest::Test
|
|
386
475
|
# This test tests that after commit, you
|
387
476
|
# can delete the file you used to add the entry to the zip file
|
388
477
|
# with
|
389
|
-
def
|
478
|
+
def test_commit_use_zip_entry
|
390
479
|
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
|
391
480
|
zf = ::Zip::File.open(TEST_ZIP.zip_name)
|
392
|
-
zf.add(
|
393
|
-
assert_contains(zf,
|
481
|
+
zf.add('okToDelete.txt', OK_DELETE_FILE)
|
482
|
+
assert_contains(zf, 'okToDelete.txt')
|
394
483
|
zf.commit
|
395
484
|
File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
|
396
|
-
assert_contains(zf,
|
485
|
+
assert_contains(zf, 'okToDelete.txt', OK_DELETE_MOVED_FILE)
|
397
486
|
end
|
398
487
|
|
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
|
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
|
406
495
|
|
407
496
|
def test_compound1
|
408
|
-
renamedName =
|
409
|
-
originalEntries = []
|
497
|
+
renamedName = 'renamedName'
|
410
498
|
filename_to_remove = ''
|
411
499
|
begin
|
412
500
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
@@ -421,17 +509,15 @@ class ZipFileTest < MiniTest::Test
|
|
421
509
|
zf.rename(entry_to_rename, renamedName)
|
422
510
|
assert_contains(zf, renamedName)
|
423
511
|
|
424
|
-
TestFiles::BINARY_TEST_FILES.each
|
425
|
-
|filename|
|
512
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
426
513
|
zf.add(filename, filename)
|
427
514
|
assert_contains(zf, filename)
|
428
|
-
|
515
|
+
end
|
429
516
|
|
430
517
|
assert_contains(zf, originalEntries.last.to_s)
|
431
518
|
filename_to_remove = originalEntries.map(&:to_s).find { |name| name.match('longBinary') }
|
432
519
|
zf.remove(filename_to_remove)
|
433
520
|
assert_not_contains(zf, filename_to_remove)
|
434
|
-
|
435
521
|
ensure
|
436
522
|
zf.close
|
437
523
|
end
|
@@ -439,10 +525,9 @@ class ZipFileTest < MiniTest::Test
|
|
439
525
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
440
526
|
assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
|
441
527
|
assert_contains(zfRead, renamedName)
|
442
|
-
TestFiles::BINARY_TEST_FILES.each
|
443
|
-
|filename|
|
528
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
444
529
|
assert_contains(zfRead, filename)
|
445
|
-
|
530
|
+
end
|
446
531
|
assert_not_contains(zfRead, filename_to_remove)
|
447
532
|
ensure
|
448
533
|
zfRead.close
|
@@ -454,23 +539,21 @@ class ZipFileTest < MiniTest::Test
|
|
454
539
|
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
455
540
|
originalEntries = zf.entries.dup
|
456
541
|
|
457
|
-
originalEntries.each
|
458
|
-
|entry|
|
542
|
+
originalEntries.each do |entry|
|
459
543
|
zf.remove(entry)
|
460
544
|
assert_not_contains(zf, entry)
|
461
|
-
|
545
|
+
end
|
462
546
|
assert(zf.entries.empty?)
|
463
547
|
|
464
|
-
TestFiles::ASCII_TEST_FILES.each
|
465
|
-
|filename|
|
548
|
+
TestFiles::ASCII_TEST_FILES.each do |filename|
|
466
549
|
zf.add(filename, filename)
|
467
550
|
assert_contains(zf, filename)
|
468
|
-
|
551
|
+
end
|
469
552
|
assert_equal(zf.entries.sort.map { |e| e.name }, TestFiles::ASCII_TEST_FILES)
|
470
553
|
|
471
|
-
zf.rename(TestFiles::ASCII_TEST_FILES[0],
|
554
|
+
zf.rename(TestFiles::ASCII_TEST_FILES[0], 'newName')
|
472
555
|
assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
|
473
|
-
assert_contains(zf,
|
556
|
+
assert_contains(zf, 'newName')
|
474
557
|
ensure
|
475
558
|
zf.close
|
476
559
|
end
|
@@ -478,56 +561,51 @@ class ZipFileTest < MiniTest::Test
|
|
478
561
|
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
479
562
|
asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
|
480
563
|
asciiTestFiles.shift
|
481
|
-
asciiTestFiles.each
|
482
|
-
|filename|
|
564
|
+
asciiTestFiles.each do |filename|
|
483
565
|
assert_contains(zf, filename)
|
484
|
-
|
566
|
+
end
|
485
567
|
|
486
|
-
assert_contains(zf,
|
568
|
+
assert_contains(zf, 'newName')
|
487
569
|
ensure
|
488
570
|
zfRead.close
|
489
571
|
end
|
490
572
|
end
|
491
573
|
|
492
|
-
def
|
574
|
+
def test_change_comment
|
493
575
|
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
494
|
-
zf.comment =
|
576
|
+
zf.comment = 'my changed comment'
|
495
577
|
end
|
496
578
|
zfRead = ::Zip::File.open(TEST_ZIP.zip_name)
|
497
|
-
assert_equal(
|
579
|
+
assert_equal('my changed comment', zfRead.comment)
|
498
580
|
end
|
499
581
|
|
500
582
|
def test_preserve_file_order
|
501
583
|
entryNames = nil
|
502
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
503
|
-
|zf|
|
584
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
504
585
|
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|
|
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|
|
513
593
|
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
514
594
|
entries = zf.entries.sort_by { |e| e.name }.reverse
|
515
|
-
entries.each
|
516
|
-
|e|
|
595
|
+
entries.each do |e|
|
517
596
|
zf.remove e
|
518
|
-
zf.get_output_stream(e) { |os| os.write
|
519
|
-
|
597
|
+
zf.get_output_stream(e) { |os| os.write 'foo' }
|
598
|
+
end
|
520
599
|
entryNames = entries.map { |e| e.to_s }
|
521
|
-
|
522
|
-
::Zip::File.open(TEST_ZIP.zip_name)
|
523
|
-
|zf|
|
600
|
+
end
|
601
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
524
602
|
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
525
|
-
|
603
|
+
end
|
526
604
|
end
|
527
605
|
|
528
606
|
def test_streaming
|
529
|
-
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)),
|
530
|
-
zname =
|
607
|
+
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '../README.md')
|
608
|
+
zname = 'test/data/generated/README.zip'
|
531
609
|
Zip::File.open(zname, Zip::File::CREATE) do |zipfile|
|
532
610
|
zipfile.get_output_stream(File.basename(fname)) do |f|
|
533
611
|
f.puts File.read(fname)
|
@@ -547,13 +625,42 @@ class ZipFileTest < MiniTest::Test
|
|
547
625
|
assert data =~ /Simonov/
|
548
626
|
end
|
549
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
|
+
|
550
656
|
private
|
657
|
+
|
551
658
|
def assert_contains(zf, entryName, filename = entryName)
|
552
659
|
assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
|
553
|
-
|
660
|
+
assert_entry_contents(zf, entryName, filename) if File.exist?(filename)
|
554
661
|
end
|
555
662
|
|
556
663
|
def assert_not_contains(zf, entryName)
|
557
|
-
assert(zf.entries.detect { |e| e.name == entryName }
|
664
|
+
assert(zf.entries.detect { |e| e.name == entryName }.nil?, "entry #{entryName} in #{zf.entries.join(', ')} in zip file #{zf}")
|
558
665
|
end
|
559
666
|
end
|