rubyzip 1.0.0 → 1.2.0
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 +6 -14
- data/README.md +173 -42
- data/Rakefile +10 -5
- data/TODO +0 -1
- data/lib/zip/central_directory.rb +55 -24
- data/lib/zip/compressor.rb +0 -0
- data/lib/zip/constants.rb +4 -2
- data/lib/zip/crypto/encryption.rb +11 -0
- data/lib/zip/crypto/null_encryption.rb +45 -0
- data/lib/zip/crypto/traditional_encryption.rb +99 -0
- data/lib/zip/decompressor.rb +2 -2
- data/lib/zip/deflater.rb +11 -6
- data/lib/zip/dos_time.rb +4 -5
- data/lib/zip/entry.rb +159 -97
- data/lib/zip/entry_set.rb +18 -18
- data/lib/zip/errors.rb +15 -6
- data/lib/zip/extra_field/generic.rb +8 -8
- data/lib/zip/extra_field/ntfs.rb +90 -0
- data/lib/zip/extra_field/old_unix.rb +44 -0
- 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 +44 -6
- data/lib/zip/extra_field/zip64_placeholder.rb +16 -0
- data/lib/zip/extra_field.rb +20 -8
- data/lib/zip/file.rb +126 -114
- data/lib/zip/filesystem.rb +140 -139
- data/lib/zip/inflater.rb +10 -9
- data/lib/zip/input_stream.rb +105 -80
- data/lib/zip/ioextras/abstract_input_stream.rb +15 -12
- data/lib/zip/ioextras/abstract_output_stream.rb +0 -2
- data/lib/zip/ioextras.rb +1 -3
- data/lib/zip/null_compressor.rb +2 -2
- data/lib/zip/null_decompressor.rb +4 -4
- data/lib/zip/null_input_stream.rb +2 -1
- data/lib/zip/output_stream.rb +57 -43
- data/lib/zip/pass_thru_compressor.rb +4 -4
- data/lib/zip/pass_thru_decompressor.rb +4 -5
- data/lib/zip/streamable_directory.rb +2 -2
- data/lib/zip/streamable_stream.rb +22 -13
- data/lib/zip/version.rb +1 -1
- data/lib/zip.rb +11 -2
- data/samples/example.rb +30 -40
- data/samples/example_filesystem.rb +16 -18
- data/samples/example_recursive.rb +35 -27
- data/samples/{gtkRubyzip.rb → gtk_ruby_zip.rb} +25 -27
- data/samples/qtzip.rb +19 -28
- data/samples/write_simple.rb +12 -13
- data/samples/zipfind.rb +29 -37
- data/test/basic_zip_file_test.rb +60 -0
- data/test/case_sensitivity_test.rb +69 -0
- data/test/central_directory_entry_test.rb +69 -0
- data/test/central_directory_test.rb +100 -0
- data/test/crypto/null_encryption_test.rb +53 -0
- data/test/crypto/traditional_encryption_test.rb +80 -0
- data/test/data/WarnInvalidDate.zip +0 -0
- data/test/data/file1.txt +46 -0
- data/test/data/file1.txt.deflatedData +0 -0
- data/test/data/file2.txt +1504 -0
- data/test/data/globTest/foo/bar/baz/foo.txt +0 -0
- data/test/data/globTest/foo.txt +0 -0
- data/test/data/globTest/food.txt +0 -0
- data/test/data/globTest.zip +0 -0
- data/test/data/mimetype +1 -0
- data/test/data/notzippedruby.rb +7 -0
- data/test/data/ntfs.zip +0 -0
- data/test/data/oddExtraField.zip +0 -0
- data/test/data/rubycode.zip +0 -0
- data/test/data/rubycode2.zip +0 -0
- data/test/data/test.xls +0 -0
- data/test/data/testDirectory.bin +0 -0
- data/test/data/zip64-sample.zip +0 -0
- data/test/data/zipWithDirs.zip +0 -0
- data/test/data/zipWithEncryption.zip +0 -0
- data/test/deflater_test.rb +65 -0
- data/test/encryption_test.rb +42 -0
- data/test/entry_set_test.rb +152 -0
- data/test/entry_test.rb +163 -0
- data/test/errors_test.rb +34 -0
- data/test/extra_field_test.rb +76 -0
- data/test/file_extract_directory_test.rb +54 -0
- data/test/file_extract_test.rb +83 -0
- data/test/file_permissions_test.rb +69 -0
- data/test/file_split_test.rb +57 -0
- data/test/file_test.rb +563 -0
- data/test/filesystem/dir_iterator_test.rb +58 -0
- data/test/filesystem/directory_test.rb +121 -0
- data/test/filesystem/file_mutating_test.rb +88 -0
- data/test/filesystem/file_nonmutating_test.rb +508 -0
- data/test/filesystem/file_stat_test.rb +64 -0
- data/test/gentestfiles.rb +122 -0
- data/test/inflater_test.rb +14 -0
- data/test/input_stream_test.rb +182 -0
- data/test/ioextras/abstract_input_stream_test.rb +102 -0
- data/test/ioextras/abstract_output_stream_test.rb +106 -0
- data/test/ioextras/fake_io_test.rb +18 -0
- data/test/local_entry_test.rb +154 -0
- data/test/output_stream_test.rb +128 -0
- data/test/pass_thru_compressor_test.rb +30 -0
- data/test/pass_thru_decompressor_test.rb +14 -0
- data/test/samples/example_recursive_test.rb +37 -0
- data/test/settings_test.rb +95 -0
- data/test/test_helper.rb +221 -0
- data/test/unicode_file_names_and_comments_test.rb +50 -0
- data/test/zip64_full_test.rb +51 -0
- data/test/zip64_support_test.rb +14 -0
- metadata +198 -22
- data/NEWS +0 -182
data/test/file_test.rb
ADDED
@@ -0,0 +1,563 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ZipFileTest < MiniTest::Test
|
4
|
+
include CommonZipFileFixture
|
5
|
+
|
6
|
+
OK_DELETE_FILE = 'test/data/generated/okToDelete.txt'
|
7
|
+
OK_DELETE_MOVED_FILE = 'test/data/generated/okToDeleteMoved.txt'
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
::Zip.write_zip64_support = false
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_create_from_scratch_to_buffer
|
14
|
+
comment = 'a short comment'
|
15
|
+
|
16
|
+
buffer = ::Zip::File.add_buffer do |zf|
|
17
|
+
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
18
|
+
zf.mkdir('dir1')
|
19
|
+
zf.comment = comment
|
20
|
+
end
|
21
|
+
|
22
|
+
::File.open(EMPTY_FILENAME, 'wb') { |file| file.write buffer.string }
|
23
|
+
|
24
|
+
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
25
|
+
assert_equal(comment, zfRead.comment)
|
26
|
+
assert_equal(2, zfRead.entries.length)
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_create_from_scratch
|
30
|
+
comment = 'a short comment'
|
31
|
+
|
32
|
+
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
33
|
+
zf.get_output_stream('myFile') { |os| os.write 'myFile contains just this' }
|
34
|
+
zf.mkdir('dir1')
|
35
|
+
zf.comment = comment
|
36
|
+
zf.close
|
37
|
+
|
38
|
+
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
39
|
+
assert_equal(comment, zfRead.comment)
|
40
|
+
assert_equal(2, zfRead.entries.length)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_get_output_stream
|
44
|
+
entryCount = nil
|
45
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
46
|
+
entryCount = zf.size
|
47
|
+
zf.get_output_stream('newEntry.txt') do |os|
|
48
|
+
os.write 'Putting stuff in newEntry.txt'
|
49
|
+
end
|
50
|
+
assert_equal(entryCount + 1, zf.size)
|
51
|
+
assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
|
52
|
+
|
53
|
+
zf.get_output_stream(zf.get_entry('test/data/generated/empty.txt')) do |os|
|
54
|
+
os.write 'Putting stuff in data/generated/empty.txt'
|
55
|
+
end
|
56
|
+
assert_equal(entryCount + 1, zf.size)
|
57
|
+
assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
|
58
|
+
|
59
|
+
custom_entry_args = [ZipEntryTest::TEST_COMMENT, ZipEntryTest::TEST_EXTRA, ZipEntryTest::TEST_COMPRESSED_SIZE, ZipEntryTest::TEST_CRC, ::Zip::Entry::STORED, ZipEntryTest::TEST_SIZE, ZipEntryTest::TEST_TIME]
|
60
|
+
zf.get_output_stream('entry_with_custom_args.txt', nil, *custom_entry_args) do |os|
|
61
|
+
os.write 'Some data'
|
62
|
+
end
|
63
|
+
assert_equal(entryCount + 2, zf.size)
|
64
|
+
entry = zf.get_entry('entry_with_custom_args.txt')
|
65
|
+
assert_equal(custom_entry_args[0], entry.comment)
|
66
|
+
assert_equal(custom_entry_args[2], entry.compressed_size)
|
67
|
+
assert_equal(custom_entry_args[3], entry.crc)
|
68
|
+
assert_equal(custom_entry_args[4], entry.compression_method)
|
69
|
+
assert_equal(custom_entry_args[5], entry.size)
|
70
|
+
assert_equal(custom_entry_args[6], entry.time)
|
71
|
+
|
72
|
+
zf.get_output_stream('entry.bin') do |os|
|
73
|
+
os.write(::File.open('test/data/generated/5entry.zip', 'rb').read)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
78
|
+
assert_equal(entryCount + 3, zf.size)
|
79
|
+
assert_equal('Putting stuff in newEntry.txt', zf.read('newEntry.txt'))
|
80
|
+
assert_equal('Putting stuff in data/generated/empty.txt', zf.read('test/data/generated/empty.txt'))
|
81
|
+
assert_equal(File.open('test/data/generated/5entry.zip', 'rb').read, zf.read('entry.bin'))
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_open_buffer_with_stringio
|
86
|
+
string_io = StringIO.new File.read('test/data/rubycode.zip')
|
87
|
+
::Zip::File.open_buffer string_io do |zf|
|
88
|
+
assert zf.entries.map { |e| e.name }.include?('zippedruby1.rb')
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_cleans_up_tempfiles_after_close
|
93
|
+
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
94
|
+
zf.get_output_stream('myFile') do |os|
|
95
|
+
@tempfile_path = os.path
|
96
|
+
os.write 'myFile contains just this'
|
97
|
+
end
|
98
|
+
|
99
|
+
assert_equal(true, File.exist?(@tempfile_path))
|
100
|
+
|
101
|
+
zf.close
|
102
|
+
|
103
|
+
assert_equal(false, File.exist?(@tempfile_path))
|
104
|
+
end
|
105
|
+
|
106
|
+
def test_add
|
107
|
+
srcFile = 'test/data/file2.txt'
|
108
|
+
entryName = 'newEntryName.rb'
|
109
|
+
assert(::File.exist?(srcFile))
|
110
|
+
zf = ::Zip::File.new(EMPTY_FILENAME, ::Zip::File::CREATE)
|
111
|
+
zf.add(entryName, srcFile)
|
112
|
+
zf.close
|
113
|
+
|
114
|
+
zfRead = ::Zip::File.new(EMPTY_FILENAME)
|
115
|
+
assert_equal('', zfRead.comment)
|
116
|
+
assert_equal(1, zfRead.entries.length)
|
117
|
+
assert_equal(entryName, zfRead.entries.first.name)
|
118
|
+
AssertEntry.assert_contents(srcFile,
|
119
|
+
zfRead.get_input_stream(entryName) { |zis| zis.read })
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_recover_permissions_after_add_files_to_archive
|
123
|
+
srcZip = TEST_ZIP.zip_name
|
124
|
+
::File.chmod(0664, srcZip)
|
125
|
+
srcFile = 'test/data/file2.txt'
|
126
|
+
entryName = 'newEntryName.rb'
|
127
|
+
assert_equal(::File.stat(srcZip).mode, 0100664)
|
128
|
+
assert(::File.exist?(srcZip))
|
129
|
+
zf = ::Zip::File.new(srcZip, ::Zip::File::CREATE)
|
130
|
+
zf.add(entryName, srcFile)
|
131
|
+
zf.close
|
132
|
+
assert_equal(::File.stat(srcZip).mode, 0100664)
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_add_existing_entry_name
|
136
|
+
assert_raises(::Zip::EntryExistsError) do
|
137
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
138
|
+
zf.add(zf.entries.first.name, 'test/data/file2.txt')
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_add_existing_entry_name_replace
|
144
|
+
gotCalled = false
|
145
|
+
replacedEntry = nil
|
146
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
147
|
+
replacedEntry = zf.entries.first.name
|
148
|
+
zf.add(replacedEntry, 'test/data/file2.txt') { gotCalled = true; true }
|
149
|
+
end
|
150
|
+
assert(gotCalled)
|
151
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
152
|
+
assert_contains(zf, replacedEntry, 'test/data/file2.txt')
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_add_directory
|
157
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
158
|
+
zf.add(TestFiles::EMPTY_TEST_DIR, TestFiles::EMPTY_TEST_DIR)
|
159
|
+
end
|
160
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
161
|
+
dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR + '/' }
|
162
|
+
assert(dirEntry.directory?)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_remove
|
167
|
+
entryToRemove, *remainingEntries = TEST_ZIP.entry_names
|
168
|
+
|
169
|
+
FileUtils.cp(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
170
|
+
|
171
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
172
|
+
assert(zf.entries.map { |e| e.name }.include?(entryToRemove))
|
173
|
+
zf.remove(entryToRemove)
|
174
|
+
assert(!zf.entries.map { |e| e.name }.include?(entryToRemove))
|
175
|
+
assert_equal(zf.entries.map { |x| x.name }.sort, remainingEntries.sort)
|
176
|
+
zf.close
|
177
|
+
|
178
|
+
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
179
|
+
assert(!zfRead.entries.map { |e| e.name }.include?(entryToRemove))
|
180
|
+
assert_equal(zfRead.entries.map { |x| x.name }.sort, remainingEntries.sort)
|
181
|
+
zfRead.close
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_rename
|
185
|
+
entryToRename, * = TEST_ZIP.entry_names
|
186
|
+
|
187
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
188
|
+
assert(zf.entries.map { |e| e.name }.include?(entryToRename))
|
189
|
+
|
190
|
+
contents = zf.read(entryToRename)
|
191
|
+
newName = 'changed entry name'
|
192
|
+
assert(!zf.entries.map { |e| e.name }.include?(newName))
|
193
|
+
|
194
|
+
zf.rename(entryToRename, newName)
|
195
|
+
assert(zf.entries.map { |e| e.name }.include?(newName))
|
196
|
+
|
197
|
+
assert_equal(contents, zf.read(newName))
|
198
|
+
|
199
|
+
zf.close
|
200
|
+
|
201
|
+
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
202
|
+
assert(zfRead.entries.map { |e| e.name }.include?(newName))
|
203
|
+
assert_equal(contents, zfRead.read(newName))
|
204
|
+
zfRead.close
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_rename_with_each
|
208
|
+
zf_name = 'test_rename_zip.zip'
|
209
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
210
|
+
arr = []
|
211
|
+
arr_renamed = []
|
212
|
+
::Zip::File.open(zf_name, ::Zip::File::CREATE) do |zf|
|
213
|
+
zf.mkdir('test')
|
214
|
+
arr << 'test/'
|
215
|
+
arr_renamed << 'Ztest/'
|
216
|
+
%w(a b c d).each do |f|
|
217
|
+
zf.get_output_stream("test/#{f}") { |file| file.puts 'aaaa' }
|
218
|
+
arr << "test/#{f}"
|
219
|
+
arr_renamed << "Ztest/#{f}"
|
220
|
+
end
|
221
|
+
end
|
222
|
+
zf = ::Zip::File.open(zf_name)
|
223
|
+
assert_equal(zf.entries.map(&:name), arr)
|
224
|
+
zf.close
|
225
|
+
Zip::File.open(zf_name, 'wb') do |z|
|
226
|
+
z.each do |f|
|
227
|
+
z.rename(f, "Z#{f.name}")
|
228
|
+
end
|
229
|
+
end
|
230
|
+
zf = ::Zip::File.open(zf_name)
|
231
|
+
assert_equal(zf.entries.map(&:name), arr_renamed)
|
232
|
+
zf.close
|
233
|
+
::File.unlink(zf_name) if ::File.exist?(zf_name)
|
234
|
+
end
|
235
|
+
|
236
|
+
def test_rename_to_existing_entry
|
237
|
+
oldEntries = nil
|
238
|
+
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
239
|
+
|
240
|
+
assert_raises(::Zip::EntryExistsError) do
|
241
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
242
|
+
zf.rename(zf.entries[0], zf.entries[1].name)
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
247
|
+
assert_equal(oldEntries.sort.map { |e| e.name }, zf.entries.sort.map { |e| e.name })
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
251
|
+
def test_rename_to_existing_entry_overwrite
|
252
|
+
oldEntries = nil
|
253
|
+
::Zip::File.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
254
|
+
|
255
|
+
gotCalled = false
|
256
|
+
renamedEntryName = nil
|
257
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
258
|
+
renamedEntryName = zf.entries[0].name
|
259
|
+
zf.rename(zf.entries[0], zf.entries[1].name) { gotCalled = true; true }
|
260
|
+
end
|
261
|
+
|
262
|
+
assert(gotCalled)
|
263
|
+
oldEntries.delete_if { |e| e.name == renamedEntryName }
|
264
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
265
|
+
assert_equal(oldEntries.sort.map { |e| e.name },
|
266
|
+
zf.entries.sort.map { |e| e.name })
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_rename_non_entry
|
271
|
+
nonEntry = 'bogusEntry'
|
272
|
+
target_entry = 'target_entryName'
|
273
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
274
|
+
assert(!zf.entries.include?(nonEntry))
|
275
|
+
assert_raises(Errno::ENOENT) { zf.rename(nonEntry, target_entry) }
|
276
|
+
zf.commit
|
277
|
+
assert(!zf.entries.include?(target_entry))
|
278
|
+
ensure
|
279
|
+
zf.close
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_rename_entry_to_existing_entry
|
283
|
+
entry1, entry2, * = TEST_ZIP.entry_names
|
284
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
285
|
+
assert_raises(::Zip::EntryExistsError) { zf.rename(entry1, entry2) }
|
286
|
+
ensure
|
287
|
+
zf.close
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_replace
|
291
|
+
entryToReplace = TEST_ZIP.entry_names[2]
|
292
|
+
newEntrySrcFilename = 'test/data/file2.txt'
|
293
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
294
|
+
zf.replace(entryToReplace, newEntrySrcFilename)
|
295
|
+
|
296
|
+
zf.close
|
297
|
+
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
298
|
+
AssertEntry.assert_contents(newEntrySrcFilename,
|
299
|
+
zfRead.get_input_stream(entryToReplace) { |is| is.read })
|
300
|
+
AssertEntry.assert_contents(TEST_ZIP.entry_names[0],
|
301
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[0]) { |is| is.read })
|
302
|
+
AssertEntry.assert_contents(TEST_ZIP.entry_names[1],
|
303
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[1]) { |is| is.read })
|
304
|
+
AssertEntry.assert_contents(TEST_ZIP.entry_names[3],
|
305
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[3]) { |is| is.read })
|
306
|
+
zfRead.close
|
307
|
+
end
|
308
|
+
|
309
|
+
def test_replace_non_entry
|
310
|
+
entryToReplace = 'nonExistingEntryname'
|
311
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
312
|
+
assert_raises(Errno::ENOENT) { zf.replace(entryToReplace, 'test/data/file2.txt') }
|
313
|
+
end
|
314
|
+
end
|
315
|
+
|
316
|
+
def test_commit
|
317
|
+
newName = 'renamedFirst'
|
318
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
319
|
+
oldName = zf.entries.first
|
320
|
+
zf.rename(oldName, newName)
|
321
|
+
zf.commit
|
322
|
+
|
323
|
+
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
324
|
+
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
325
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
326
|
+
zfRead.close
|
327
|
+
|
328
|
+
zf.close
|
329
|
+
res = system("unzip -t #{TEST_ZIP.zip_name}")
|
330
|
+
assert_equal(res, true)
|
331
|
+
end
|
332
|
+
|
333
|
+
def test_double_commit(filename = 'test/data/generated/double_commit_test.zip')
|
334
|
+
::FileUtils.touch('test/data/generated/test_double_commit1.txt')
|
335
|
+
::FileUtils.touch('test/data/generated/test_double_commit2.txt')
|
336
|
+
zf = ::Zip::File.open(filename, ::Zip::File::CREATE)
|
337
|
+
zf.add('test1.txt', 'test/data/generated/test_double_commit1.txt')
|
338
|
+
zf.commit
|
339
|
+
zf.add('test2.txt', 'test/data/generated/test_double_commit2.txt')
|
340
|
+
zf.commit
|
341
|
+
zf.close
|
342
|
+
zf2 = ::Zip::File.open(filename)
|
343
|
+
assert(zf2.entries.detect { |e| e.name == 'test1.txt' } != nil)
|
344
|
+
assert(zf2.entries.detect { |e| e.name == 'test2.txt' } != nil)
|
345
|
+
res = system("unzip -t #{filename}")
|
346
|
+
assert_equal(res, true)
|
347
|
+
end
|
348
|
+
|
349
|
+
def test_double_commit_zip64
|
350
|
+
::Zip.write_zip64_support = true
|
351
|
+
test_double_commit('test/data/generated/double_commit_test64.zip')
|
352
|
+
end
|
353
|
+
|
354
|
+
def test_write_buffer
|
355
|
+
newName = 'renamedFirst'
|
356
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
357
|
+
oldName = zf.entries.first
|
358
|
+
zf.rename(oldName, newName)
|
359
|
+
io = ::StringIO.new('')
|
360
|
+
buffer = zf.write_buffer(io)
|
361
|
+
File.open(TEST_ZIP.zip_name, 'wb') { |f| f.write buffer.string }
|
362
|
+
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
363
|
+
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
364
|
+
assert(zfRead.entries.detect { |e| e.name == oldName }.nil?)
|
365
|
+
zfRead.close
|
366
|
+
|
367
|
+
zf.close
|
368
|
+
end
|
369
|
+
|
370
|
+
# This test tests that after commit, you
|
371
|
+
# can delete the file you used to add the entry to the zip file
|
372
|
+
# with
|
373
|
+
def test_commit_use_zip_entry
|
374
|
+
FileUtils.cp(TestFiles::RANDOM_ASCII_FILE1, OK_DELETE_FILE)
|
375
|
+
zf = ::Zip::File.open(TEST_ZIP.zip_name)
|
376
|
+
zf.add('okToDelete.txt', OK_DELETE_FILE)
|
377
|
+
assert_contains(zf, 'okToDelete.txt')
|
378
|
+
zf.commit
|
379
|
+
File.rename(OK_DELETE_FILE, OK_DELETE_MOVED_FILE)
|
380
|
+
assert_contains(zf, 'okToDelete.txt', OK_DELETE_MOVED_FILE)
|
381
|
+
end
|
382
|
+
|
383
|
+
# def test_close
|
384
|
+
# zf = ZipFile.new(TEST_ZIP.zip_name)
|
385
|
+
# zf.close
|
386
|
+
# assert_raises(IOError) {
|
387
|
+
# zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
|
388
|
+
# }
|
389
|
+
# end
|
390
|
+
|
391
|
+
def test_compound1
|
392
|
+
renamedName = 'renamedName'
|
393
|
+
filename_to_remove = ''
|
394
|
+
begin
|
395
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
396
|
+
originalEntries = zf.entries.dup
|
397
|
+
|
398
|
+
assert_not_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
399
|
+
zf.add(TestFiles::RANDOM_ASCII_FILE1,
|
400
|
+
TestFiles::RANDOM_ASCII_FILE1)
|
401
|
+
assert_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
402
|
+
|
403
|
+
entry_to_rename = zf.entries.find { |entry| entry.name.match('longAscii') }
|
404
|
+
zf.rename(entry_to_rename, renamedName)
|
405
|
+
assert_contains(zf, renamedName)
|
406
|
+
|
407
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
408
|
+
zf.add(filename, filename)
|
409
|
+
assert_contains(zf, filename)
|
410
|
+
end
|
411
|
+
|
412
|
+
assert_contains(zf, originalEntries.last.to_s)
|
413
|
+
filename_to_remove = originalEntries.map(&:to_s).find { |name| name.match('longBinary') }
|
414
|
+
zf.remove(filename_to_remove)
|
415
|
+
assert_not_contains(zf, filename_to_remove)
|
416
|
+
|
417
|
+
ensure
|
418
|
+
zf.close
|
419
|
+
end
|
420
|
+
begin
|
421
|
+
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
422
|
+
assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
|
423
|
+
assert_contains(zfRead, renamedName)
|
424
|
+
TestFiles::BINARY_TEST_FILES.each do |filename|
|
425
|
+
assert_contains(zfRead, filename)
|
426
|
+
end
|
427
|
+
assert_not_contains(zfRead, filename_to_remove)
|
428
|
+
ensure
|
429
|
+
zfRead.close
|
430
|
+
end
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_compound2
|
434
|
+
begin
|
435
|
+
zf = ::Zip::File.new(TEST_ZIP.zip_name)
|
436
|
+
originalEntries = zf.entries.dup
|
437
|
+
|
438
|
+
originalEntries.each do |entry|
|
439
|
+
zf.remove(entry)
|
440
|
+
assert_not_contains(zf, entry)
|
441
|
+
end
|
442
|
+
assert(zf.entries.empty?)
|
443
|
+
|
444
|
+
TestFiles::ASCII_TEST_FILES.each do |filename|
|
445
|
+
zf.add(filename, filename)
|
446
|
+
assert_contains(zf, filename)
|
447
|
+
end
|
448
|
+
assert_equal(zf.entries.sort.map { |e| e.name }, TestFiles::ASCII_TEST_FILES)
|
449
|
+
|
450
|
+
zf.rename(TestFiles::ASCII_TEST_FILES[0], 'newName')
|
451
|
+
assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
|
452
|
+
assert_contains(zf, 'newName')
|
453
|
+
ensure
|
454
|
+
zf.close
|
455
|
+
end
|
456
|
+
begin
|
457
|
+
zfRead = ::Zip::File.new(TEST_ZIP.zip_name)
|
458
|
+
asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
|
459
|
+
asciiTestFiles.shift
|
460
|
+
asciiTestFiles.each do |filename|
|
461
|
+
assert_contains(zf, filename)
|
462
|
+
end
|
463
|
+
|
464
|
+
assert_contains(zf, 'newName')
|
465
|
+
ensure
|
466
|
+
zfRead.close
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
def test_change_comment
|
471
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
472
|
+
zf.comment = 'my changed comment'
|
473
|
+
end
|
474
|
+
zfRead = ::Zip::File.open(TEST_ZIP.zip_name)
|
475
|
+
assert_equal('my changed comment', zfRead.comment)
|
476
|
+
end
|
477
|
+
|
478
|
+
def test_preserve_file_order
|
479
|
+
entryNames = nil
|
480
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
481
|
+
entryNames = zf.entries.map { |e| e.to_s }
|
482
|
+
zf.get_output_stream('a.txt') { |os| os.write 'this is a.txt' }
|
483
|
+
zf.get_output_stream('z.txt') { |os| os.write 'this is z.txt' }
|
484
|
+
zf.get_output_stream('k.txt') { |os| os.write 'this is k.txt' }
|
485
|
+
entryNames << 'a.txt' << 'z.txt' << 'k.txt'
|
486
|
+
end
|
487
|
+
|
488
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
489
|
+
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
490
|
+
entries = zf.entries.sort_by { |e| e.name }.reverse
|
491
|
+
entries.each do |e|
|
492
|
+
zf.remove e
|
493
|
+
zf.get_output_stream(e) { |os| os.write 'foo' }
|
494
|
+
end
|
495
|
+
entryNames = entries.map { |e| e.to_s }
|
496
|
+
end
|
497
|
+
::Zip::File.open(TEST_ZIP.zip_name) do |zf|
|
498
|
+
assert_equal(entryNames, zf.entries.map { |e| e.to_s })
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
def test_streaming
|
503
|
+
fname = ::File.join(::File.expand_path(::File.dirname(__FILE__)), '../README.md')
|
504
|
+
zname = 'test/data/generated/README.zip'
|
505
|
+
Zip::File.open(zname, Zip::File::CREATE) do |zipfile|
|
506
|
+
zipfile.get_output_stream(File.basename(fname)) do |f|
|
507
|
+
f.puts File.read(fname)
|
508
|
+
end
|
509
|
+
end
|
510
|
+
|
511
|
+
data = nil
|
512
|
+
File.open(zname, 'rb') do |f|
|
513
|
+
Zip::File.open_buffer(f) do |zipfile|
|
514
|
+
zipfile.each do |entry|
|
515
|
+
next unless entry.name =~ /README.md/
|
516
|
+
data = zipfile.read(entry)
|
517
|
+
end
|
518
|
+
end
|
519
|
+
end
|
520
|
+
assert data
|
521
|
+
assert data =~ /Simonov/
|
522
|
+
end
|
523
|
+
|
524
|
+
def test_nonexistant_zip
|
525
|
+
assert_raises(::Zip::Error) do
|
526
|
+
::Zip::File.open('fake.zip')
|
527
|
+
end
|
528
|
+
end
|
529
|
+
|
530
|
+
def test_empty_zip
|
531
|
+
puts `touch empty.zip`
|
532
|
+
assert_raises(::Zip::Error) do
|
533
|
+
::Zip::File.open('empty.zip')
|
534
|
+
end
|
535
|
+
end
|
536
|
+
|
537
|
+
def test_odd_extra_field
|
538
|
+
entry_count = 0
|
539
|
+
File.open 'test/data/oddExtraField.zip', 'rb' do |zip_io|
|
540
|
+
Zip::File.open_buffer zip_io.read do |zip|
|
541
|
+
zip.each do |zip_entry|
|
542
|
+
entry_count += 1
|
543
|
+
end
|
544
|
+
end
|
545
|
+
end
|
546
|
+
assert_equal 13, entry_count
|
547
|
+
end
|
548
|
+
|
549
|
+
def test_open_xls_does_not_raise_type_error
|
550
|
+
::Zip::File.open('test/data/test.xls')
|
551
|
+
end
|
552
|
+
|
553
|
+
private
|
554
|
+
|
555
|
+
def assert_contains(zf, entryName, filename = entryName)
|
556
|
+
assert(zf.entries.detect { |e| e.name == entryName } != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
|
557
|
+
assert_entry_contents(zf, entryName, filename) if File.exist?(filename)
|
558
|
+
end
|
559
|
+
|
560
|
+
def assert_not_contains(zf, entryName)
|
561
|
+
assert(zf.entries.detect { |e| e.name == entryName }.nil?, "entry #{entryName} in #{zf.entries.join(', ')} in zip file #{zf}")
|
562
|
+
end
|
563
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'zip/filesystem'
|
3
|
+
|
4
|
+
class ZipFsDirIteratorTest < MiniTest::Test
|
5
|
+
FILENAME_ARRAY = %w(f1 f2 f3 f4 f5 f6)
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@dirIt = ::Zip::FileSystem::ZipFsDirIterator.new(FILENAME_ARRAY)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_close
|
12
|
+
@dirIt.close
|
13
|
+
assert_raises(IOError, 'closed directory') do
|
14
|
+
@dirIt.each { |e| p e }
|
15
|
+
end
|
16
|
+
assert_raises(IOError, 'closed directory') do
|
17
|
+
@dirIt.read
|
18
|
+
end
|
19
|
+
assert_raises(IOError, 'closed directory') do
|
20
|
+
@dirIt.rewind
|
21
|
+
end
|
22
|
+
assert_raises(IOError, 'closed directory') do
|
23
|
+
@dirIt.seek(0)
|
24
|
+
end
|
25
|
+
assert_raises(IOError, 'closed directory') do
|
26
|
+
@dirIt.tell
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_each
|
31
|
+
# Tested through Enumerable.entries
|
32
|
+
assert_equal(FILENAME_ARRAY, @dirIt.entries)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_read
|
36
|
+
FILENAME_ARRAY.size.times do |i|
|
37
|
+
assert_equal(FILENAME_ARRAY[i], @dirIt.read)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_rewind
|
42
|
+
@dirIt.read
|
43
|
+
@dirIt.read
|
44
|
+
assert_equal(FILENAME_ARRAY[2], @dirIt.read)
|
45
|
+
@dirIt.rewind
|
46
|
+
assert_equal(FILENAME_ARRAY[0], @dirIt.read)
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_tell_seek
|
50
|
+
@dirIt.read
|
51
|
+
@dirIt.read
|
52
|
+
pos = @dirIt.tell
|
53
|
+
valAtPos = @dirIt.read
|
54
|
+
@dirIt.read
|
55
|
+
@dirIt.seek(pos)
|
56
|
+
assert_equal(valAtPos, @dirIt.read)
|
57
|
+
end
|
58
|
+
end
|