rubyzip 0.5.7
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.
- data/ChangeLog +1265 -0
- data/NEWS +106 -0
- data/README +69 -0
- data/Rakefile +110 -0
- data/TODO +9 -0
- data/install.rb +22 -0
- data/lib/zip/ioextras.rb +117 -0
- data/lib/zip/stdrubyext.rb +111 -0
- data/lib/zip/tempfile_bugfixed.rb +195 -0
- data/lib/zip/zip.rb +1543 -0
- data/lib/zip/zipfilesystem.rb +609 -0
- data/lib/zip/ziprequire.rb +90 -0
- data/samples/example.rb +69 -0
- data/samples/example_filesystem.rb +34 -0
- data/samples/gtkRubyzip.rb +86 -0
- data/samples/write_simple.rb +13 -0
- data/samples/zipfind.rb +74 -0
- data/test/alltests.rb +9 -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/notzippedruby.rb +7 -0
- data/test/data/rubycode.zip +0 -0
- data/test/data/rubycode2.zip +0 -0
- data/test/data/testDirectory.bin +0 -0
- data/test/data/zipWithDirs.zip +0 -0
- data/test/gentestfiles.rb +155 -0
- data/test/ioextrastest.rb +208 -0
- data/test/stdrubyexttest.rb +52 -0
- data/test/zipfilesystemtest.rb +829 -0
- data/test/ziprequiretest.rb +43 -0
- data/test/ziptest.rb +1557 -0
- metadata +68 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$VERBOSE = true
|
4
|
+
|
5
|
+
$: << "../lib"
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
require 'zip/ziprequire'
|
9
|
+
|
10
|
+
$: << 'data/rubycode.zip' << 'data/rubycode2.zip'
|
11
|
+
|
12
|
+
class ZipRequireTest < Test::Unit::TestCase
|
13
|
+
def test_require
|
14
|
+
assert(require('data/notzippedruby'))
|
15
|
+
assert(!require('data/notzippedruby'))
|
16
|
+
|
17
|
+
assert(require('zippedruby1'))
|
18
|
+
assert(!require('zippedruby1'))
|
19
|
+
|
20
|
+
assert(require('zippedruby2'))
|
21
|
+
assert(!require('zippedruby2'))
|
22
|
+
|
23
|
+
assert(require('zippedruby3'))
|
24
|
+
assert(!require('zippedruby3'))
|
25
|
+
|
26
|
+
c1 = NotZippedRuby.new
|
27
|
+
assert(c1.returnTrue)
|
28
|
+
assert(ZippedRuby1.returnTrue)
|
29
|
+
assert(!ZippedRuby2.returnFalse)
|
30
|
+
assert_equal(4, ZippedRuby3.multiplyValues(2, 2))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_get_resource
|
34
|
+
get_resource("aResource.txt") {
|
35
|
+
|f|
|
36
|
+
assert_equal("Nothing exciting in this file!", f.read)
|
37
|
+
}
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Copyright (C) 2002 Thomas Sondergaard
|
42
|
+
# rubyzip is free software; you can redistribute it and/or
|
43
|
+
# modify it under the terms of the ruby license.
|
data/test/ziptest.rb
ADDED
@@ -0,0 +1,1557 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
$VERBOSE = true
|
4
|
+
|
5
|
+
$: << "../lib"
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
require 'zip/zip'
|
9
|
+
require 'gentestfiles'
|
10
|
+
|
11
|
+
include Zip
|
12
|
+
|
13
|
+
|
14
|
+
class ZipEntryTest < Test::Unit::TestCase
|
15
|
+
TEST_ZIPFILE = "someZipFile.zip"
|
16
|
+
TEST_COMMENT = "a comment"
|
17
|
+
TEST_COMPRESSED_SIZE = 1234
|
18
|
+
TEST_CRC = 325324
|
19
|
+
TEST_EXTRA = "Some data here"
|
20
|
+
TEST_COMPRESSIONMETHOD = ZipEntry::DEFLATED
|
21
|
+
TEST_NAME = "entry name"
|
22
|
+
TEST_SIZE = 8432
|
23
|
+
TEST_ISDIRECTORY = false
|
24
|
+
|
25
|
+
def test_constructorAndGetters
|
26
|
+
entry = ZipEntry.new(TEST_ZIPFILE,
|
27
|
+
TEST_NAME,
|
28
|
+
TEST_COMMENT,
|
29
|
+
TEST_EXTRA,
|
30
|
+
TEST_COMPRESSED_SIZE,
|
31
|
+
TEST_CRC,
|
32
|
+
TEST_COMPRESSIONMETHOD,
|
33
|
+
TEST_SIZE)
|
34
|
+
|
35
|
+
assert_equal(TEST_COMMENT, entry.comment)
|
36
|
+
assert_equal(TEST_COMPRESSED_SIZE, entry.compressed_size)
|
37
|
+
assert_equal(TEST_CRC, entry.crc)
|
38
|
+
assert_instance_of(Zip::ZipExtraField, entry.extra)
|
39
|
+
assert_equal(TEST_COMPRESSIONMETHOD, entry.compression_method)
|
40
|
+
assert_equal(TEST_NAME, entry.name)
|
41
|
+
assert_equal(TEST_SIZE, entry.size)
|
42
|
+
assert_equal(TEST_ISDIRECTORY, entry.is_directory)
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_is_directoryAndIsFile
|
46
|
+
assert(ZipEntry.new(TEST_ZIPFILE, "hello").file?)
|
47
|
+
assert(! ZipEntry.new(TEST_ZIPFILE, "hello").directory?)
|
48
|
+
|
49
|
+
assert(ZipEntry.new(TEST_ZIPFILE, "dir/hello").file?)
|
50
|
+
assert(! ZipEntry.new(TEST_ZIPFILE, "dir/hello").directory?)
|
51
|
+
|
52
|
+
assert(ZipEntry.new(TEST_ZIPFILE, "hello/").directory?)
|
53
|
+
assert(! ZipEntry.new(TEST_ZIPFILE, "hello/").file?)
|
54
|
+
|
55
|
+
assert(ZipEntry.new(TEST_ZIPFILE, "dir/hello/").directory?)
|
56
|
+
assert(! ZipEntry.new(TEST_ZIPFILE, "dir/hello/").file?)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_equality
|
60
|
+
entry1 = ZipEntry.new("file.zip", "name", "isNotCompared",
|
61
|
+
"something extra", 123, 1234,
|
62
|
+
ZipEntry::DEFLATED, 10000)
|
63
|
+
entry2 = ZipEntry.new("file.zip", "name", "isNotComparedXXX",
|
64
|
+
"something extra", 123, 1234,
|
65
|
+
ZipEntry::DEFLATED, 10000)
|
66
|
+
entry3 = ZipEntry.new("file.zip", "name2", "isNotComparedXXX",
|
67
|
+
"something extra", 123, 1234,
|
68
|
+
ZipEntry::DEFLATED, 10000)
|
69
|
+
entry4 = ZipEntry.new("file.zip", "name2", "isNotComparedXXX",
|
70
|
+
"something extraXX", 123, 1234,
|
71
|
+
ZipEntry::DEFLATED, 10000)
|
72
|
+
entry5 = ZipEntry.new("file.zip", "name2", "isNotComparedXXX",
|
73
|
+
"something extraXX", 12, 1234,
|
74
|
+
ZipEntry::DEFLATED, 10000)
|
75
|
+
entry6 = ZipEntry.new("file.zip", "name2", "isNotComparedXXX",
|
76
|
+
"something extraXX", 12, 123,
|
77
|
+
ZipEntry::DEFLATED, 10000)
|
78
|
+
entry7 = ZipEntry.new("file.zip", "name2", "isNotComparedXXX",
|
79
|
+
"something extraXX", 12, 123,
|
80
|
+
ZipEntry::STORED, 10000)
|
81
|
+
entry8 = ZipEntry.new("file.zip", "name2", "isNotComparedXXX",
|
82
|
+
"something extraXX", 12, 123,
|
83
|
+
ZipEntry::STORED, 100000)
|
84
|
+
|
85
|
+
assert_equal(entry1, entry1)
|
86
|
+
assert_equal(entry1, entry2)
|
87
|
+
|
88
|
+
assert(entry2 != entry3)
|
89
|
+
assert(entry3 != entry4)
|
90
|
+
assert(entry4 != entry5)
|
91
|
+
assert(entry5 != entry6)
|
92
|
+
assert(entry6 != entry7)
|
93
|
+
assert(entry7 != entry8)
|
94
|
+
|
95
|
+
assert(entry7 != "hello")
|
96
|
+
assert(entry7 != 12)
|
97
|
+
|
98
|
+
assert(entry7 != ZipStreamableFile.new(entry7, "aPath"))
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_compare
|
102
|
+
assert_equal(0, (ZipEntry.new("zf.zip", "a") <=> ZipEntry.new("zf.zip", "a")))
|
103
|
+
assert_equal(1, (ZipEntry.new("zf.zip", "b") <=> ZipEntry.new("zf.zip", "a")))
|
104
|
+
assert_equal(-1, (ZipEntry.new("zf.zip", "a") <=> ZipEntry.new("zf.zip", "b")))
|
105
|
+
|
106
|
+
entries = [
|
107
|
+
ZipEntry.new("zf.zip", "5"),
|
108
|
+
ZipEntry.new("zf.zip", "1"),
|
109
|
+
ZipEntry.new("zf.zip", "3"),
|
110
|
+
ZipEntry.new("zf.zip", "4"),
|
111
|
+
ZipEntry.new("zf.zip", "0"),
|
112
|
+
ZipEntry.new("zf.zip", "2")
|
113
|
+
]
|
114
|
+
|
115
|
+
entries.sort!
|
116
|
+
assert_equal("0", entries[0].to_s)
|
117
|
+
assert_equal("1", entries[1].to_s)
|
118
|
+
assert_equal("2", entries[2].to_s)
|
119
|
+
assert_equal("3", entries[3].to_s)
|
120
|
+
assert_equal("4", entries[4].to_s)
|
121
|
+
assert_equal("5", entries[5].to_s)
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_parentAsString
|
125
|
+
entry1 = ZipEntry.new("zf.zip", "aa")
|
126
|
+
entry2 = ZipEntry.new("zf.zip", "aa/")
|
127
|
+
entry3 = ZipEntry.new("zf.zip", "aa/bb")
|
128
|
+
entry4 = ZipEntry.new("zf.zip", "aa/bb/")
|
129
|
+
entry5 = ZipEntry.new("zf.zip", "aa/bb/cc")
|
130
|
+
entry6 = ZipEntry.new("zf.zip", "aa/bb/cc/")
|
131
|
+
|
132
|
+
assert_equal(nil, entry1.parent_as_string)
|
133
|
+
assert_equal(nil, entry2.parent_as_string)
|
134
|
+
assert_equal("aa/", entry3.parent_as_string)
|
135
|
+
assert_equal("aa/", entry4.parent_as_string)
|
136
|
+
assert_equal("aa/bb/", entry5.parent_as_string)
|
137
|
+
assert_equal("aa/bb/", entry6.parent_as_string)
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_entry_name_cannot_start_with_slash
|
141
|
+
assert_raise(ZipEntryNameError) { ZipEntry.new("zf.zip", "/hej/der") }
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
module IOizeString
|
146
|
+
attr_reader :tell
|
147
|
+
|
148
|
+
def read(count = nil)
|
149
|
+
@tell ||= 0
|
150
|
+
count = size unless count
|
151
|
+
retVal = slice(@tell, count)
|
152
|
+
@tell += count
|
153
|
+
return retVal
|
154
|
+
end
|
155
|
+
|
156
|
+
def seek(index, offset)
|
157
|
+
@tell ||= 0
|
158
|
+
case offset
|
159
|
+
when IO::SEEK_END
|
160
|
+
newPos = size + index
|
161
|
+
when IO::SEEK_SET
|
162
|
+
newPos = index
|
163
|
+
when IO::SEEK_CUR
|
164
|
+
newPos = @tell + index
|
165
|
+
else
|
166
|
+
raise "Error in test method IOizeString::seek"
|
167
|
+
end
|
168
|
+
if (newPos < 0 || newPos >= size)
|
169
|
+
raise Errno::EINVAL
|
170
|
+
else
|
171
|
+
@tell=newPos
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def reset
|
176
|
+
@tell = 0
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
class ZipLocalEntryTest < Test::Unit::TestCase
|
181
|
+
def test_read_local_entryHeaderOfFirstTestZipEntry
|
182
|
+
File.open(TestZipFile::TEST_ZIP3.zip_name, "rb") {
|
183
|
+
|file|
|
184
|
+
entry = ZipEntry.read_local_entry(file)
|
185
|
+
|
186
|
+
assert_equal("", entry.comment)
|
187
|
+
# Differs from windows and unix because of CR LF
|
188
|
+
# assert_equal(480, entry.compressed_size)
|
189
|
+
# assert_equal(0x2a27930f, entry.crc)
|
190
|
+
# extra field is 21 bytes long
|
191
|
+
# probably contains some unix attrutes or something
|
192
|
+
# disabled: assert_equal(nil, entry.extra)
|
193
|
+
assert_equal(ZipEntry::DEFLATED, entry.compression_method)
|
194
|
+
assert_equal(TestZipFile::TEST_ZIP3.entry_names[0], entry.name)
|
195
|
+
assert_equal(File.size(TestZipFile::TEST_ZIP3.entry_names[0]), entry.size)
|
196
|
+
assert(! entry.is_directory)
|
197
|
+
}
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_readDateTime
|
201
|
+
File.open("data/rubycode.zip", "rb") {
|
202
|
+
|file|
|
203
|
+
entry = ZipEntry.read_local_entry(file)
|
204
|
+
assert_equal("zippedruby1.rb", entry.name)
|
205
|
+
assert_equal(Time.at(1019261638), entry.time)
|
206
|
+
}
|
207
|
+
end
|
208
|
+
|
209
|
+
def test_read_local_entryFromNonZipFile
|
210
|
+
File.open("data/file2.txt") {
|
211
|
+
|file|
|
212
|
+
assert_equal(nil, ZipEntry.read_local_entry(file))
|
213
|
+
}
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_read_local_entryFromTruncatedZipFile
|
217
|
+
zipFragment=""
|
218
|
+
File.open(TestZipFile::TEST_ZIP2.zip_name) { |f| zipFragment = f.read(12) } # local header is at least 30 bytes
|
219
|
+
zipFragment.extend(IOizeString).reset
|
220
|
+
entry = ZipEntry.new
|
221
|
+
entry.read_local_entry(zipFragment)
|
222
|
+
fail "ZipError expected"
|
223
|
+
rescue ZipError
|
224
|
+
end
|
225
|
+
|
226
|
+
def test_writeEntry
|
227
|
+
entry = ZipEntry.new("file.zip", "entryName", "my little comment",
|
228
|
+
"thisIsSomeExtraInformation", 100, 987654,
|
229
|
+
ZipEntry::DEFLATED, 400)
|
230
|
+
write_to_file("localEntryHeader.bin", "centralEntryHeader.bin", entry)
|
231
|
+
entryReadLocal, entryReadCentral = read_from_file("localEntryHeader.bin", "centralEntryHeader.bin")
|
232
|
+
compare_local_entry_headers(entry, entryReadLocal)
|
233
|
+
compare_c_dir_entry_headers(entry, entryReadCentral)
|
234
|
+
end
|
235
|
+
|
236
|
+
private
|
237
|
+
def compare_local_entry_headers(entry1, entry2)
|
238
|
+
assert_equal(entry1.compressed_size , entry2.compressed_size)
|
239
|
+
assert_equal(entry1.crc , entry2.crc)
|
240
|
+
assert_equal(entry1.extra , entry2.extra)
|
241
|
+
assert_equal(entry1.compression_method, entry2.compression_method)
|
242
|
+
assert_equal(entry1.name , entry2.name)
|
243
|
+
assert_equal(entry1.size , entry2.size)
|
244
|
+
assert_equal(entry1.localHeaderOffset, entry2.localHeaderOffset)
|
245
|
+
end
|
246
|
+
|
247
|
+
def compare_c_dir_entry_headers(entry1, entry2)
|
248
|
+
compare_local_entry_headers(entry1, entry2)
|
249
|
+
assert_equal(entry1.comment, entry2.comment)
|
250
|
+
end
|
251
|
+
|
252
|
+
def write_to_file(localFileName, centralFileName, entry)
|
253
|
+
File.open(localFileName, "wb") { |f| entry.write_local_entry(f) }
|
254
|
+
File.open(centralFileName, "wb") { |f| entry.write_c_dir_entry(f) }
|
255
|
+
end
|
256
|
+
|
257
|
+
def read_from_file(localFileName, centralFileName)
|
258
|
+
localEntry = nil
|
259
|
+
cdirEntry = nil
|
260
|
+
File.open(localFileName, "rb") { |f| localEntry = ZipEntry.read_local_entry(f) }
|
261
|
+
File.open(centralFileName, "rb") { |f| cdirEntry = ZipEntry.read_c_dir_entry(f) }
|
262
|
+
return [localEntry, cdirEntry]
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
|
267
|
+
module DecompressorTests
|
268
|
+
# expects @refText, @refLines and @decompressor
|
269
|
+
|
270
|
+
TEST_FILE="data/file1.txt"
|
271
|
+
|
272
|
+
def setup
|
273
|
+
@refText=""
|
274
|
+
File.open(TEST_FILE) { |f| @refText = f.read }
|
275
|
+
@refLines = @refText.split($/)
|
276
|
+
end
|
277
|
+
|
278
|
+
def test_readEverything
|
279
|
+
assert_equal(@refText, @decompressor.read)
|
280
|
+
end
|
281
|
+
|
282
|
+
def test_readInChunks
|
283
|
+
chunkSize = 5
|
284
|
+
while (decompressedChunk = @decompressor.read(chunkSize))
|
285
|
+
assert_equal(@refText.slice!(0, chunkSize), decompressedChunk)
|
286
|
+
end
|
287
|
+
assert_equal(0, @refText.size)
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_mixingReadsAndProduceInput
|
291
|
+
# Just some preconditions to make sure we have enough data for this test
|
292
|
+
assert(@refText.length > 1000)
|
293
|
+
assert(@refLines.length > 40)
|
294
|
+
|
295
|
+
|
296
|
+
assert_equal(@refText[0...100], @decompressor.read(100))
|
297
|
+
|
298
|
+
assert(! @decompressor.input_finished?)
|
299
|
+
buf = @decompressor.produce_input
|
300
|
+
assert_equal(@refText[100...(100+buf.length)], buf)
|
301
|
+
end
|
302
|
+
end
|
303
|
+
|
304
|
+
class InflaterTest < Test::Unit::TestCase
|
305
|
+
include DecompressorTests
|
306
|
+
|
307
|
+
def setup
|
308
|
+
super
|
309
|
+
@file = File.new("data/file1.txt.deflatedData", "rb")
|
310
|
+
@decompressor = Inflater.new(@file)
|
311
|
+
end
|
312
|
+
|
313
|
+
def teardown
|
314
|
+
@file.close
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
|
319
|
+
class PassThruDecompressorTest < Test::Unit::TestCase
|
320
|
+
include DecompressorTests
|
321
|
+
def setup
|
322
|
+
super
|
323
|
+
@file = File.new(TEST_FILE)
|
324
|
+
@decompressor = PassThruDecompressor.new(@file, File.size(TEST_FILE))
|
325
|
+
end
|
326
|
+
|
327
|
+
def teardown
|
328
|
+
@file.close
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
|
333
|
+
module AssertEntry
|
334
|
+
def assert_next_entry(filename, zis)
|
335
|
+
assert_entry(filename, zis, zis.get_next_entry.name)
|
336
|
+
end
|
337
|
+
|
338
|
+
def assert_entry(filename, zis, entryName)
|
339
|
+
assert_equal(filename, entryName)
|
340
|
+
assert_entryContentsForStream(filename, zis, entryName)
|
341
|
+
end
|
342
|
+
|
343
|
+
def assert_entryContentsForStream(filename, zis, entryName)
|
344
|
+
File.open(filename, "rb") {
|
345
|
+
|file|
|
346
|
+
expected = file.read
|
347
|
+
actual = zis.read
|
348
|
+
if (expected != actual)
|
349
|
+
if ((expected && actual) && (expected.length > 400 || actual.length > 400))
|
350
|
+
zipEntryFilename=entryName+".zipEntry"
|
351
|
+
File.open(zipEntryFilename, "wb") { |file| file << actual }
|
352
|
+
fail("File '#{filename}' is different from '#{zipEntryFilename}'")
|
353
|
+
else
|
354
|
+
assert_equal(expected, actual)
|
355
|
+
end
|
356
|
+
end
|
357
|
+
}
|
358
|
+
end
|
359
|
+
|
360
|
+
def AssertEntry.assert_contents(filename, aString)
|
361
|
+
fileContents = ""
|
362
|
+
File.open(filename, "rb") { |f| fileContents = f.read }
|
363
|
+
if (fileContents != aString)
|
364
|
+
if (fileContents.length > 400 || aString.length > 400)
|
365
|
+
stringFile = filename + ".other"
|
366
|
+
File.open(stringFile, "wb") { |f| f << aString }
|
367
|
+
fail("File '#{filename}' is different from contents of string stored in '#{stringFile}'")
|
368
|
+
else
|
369
|
+
assert_equal(fileContents, aString)
|
370
|
+
end
|
371
|
+
end
|
372
|
+
end
|
373
|
+
|
374
|
+
def assert_stream_contents(zis, testZipFile)
|
375
|
+
assert(zis != nil)
|
376
|
+
testZipFile.entry_names.each {
|
377
|
+
|entryName|
|
378
|
+
assert_next_entry(entryName, zis)
|
379
|
+
}
|
380
|
+
assert_equal(nil, zis.get_next_entry)
|
381
|
+
end
|
382
|
+
|
383
|
+
def assert_test_zip_contents(testZipFile)
|
384
|
+
ZipInputStream.open(testZipFile.zip_name) {
|
385
|
+
|zis|
|
386
|
+
assert_stream_contents(zis, testZipFile)
|
387
|
+
}
|
388
|
+
end
|
389
|
+
|
390
|
+
def assert_entryContents(zipFile, entryName, filename = entryName.to_s)
|
391
|
+
zis = zipFile.get_input_stream(entryName)
|
392
|
+
assert_entryContentsForStream(filename, zis, entryName)
|
393
|
+
ensure
|
394
|
+
zis.close if zis
|
395
|
+
end
|
396
|
+
end
|
397
|
+
|
398
|
+
|
399
|
+
|
400
|
+
class ZipInputStreamTest < Test::Unit::TestCase
|
401
|
+
include AssertEntry
|
402
|
+
|
403
|
+
def test_new
|
404
|
+
zis = ZipInputStream.new(TestZipFile::TEST_ZIP2.zip_name)
|
405
|
+
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
406
|
+
zis.close
|
407
|
+
end
|
408
|
+
|
409
|
+
def test_openWithBlock
|
410
|
+
ZipInputStream.open(TestZipFile::TEST_ZIP2.zip_name) {
|
411
|
+
|zis|
|
412
|
+
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
413
|
+
}
|
414
|
+
end
|
415
|
+
|
416
|
+
def test_openWithoutBlock
|
417
|
+
zis = ZipInputStream.open(TestZipFile::TEST_ZIP2.zip_name)
|
418
|
+
assert_stream_contents(zis, TestZipFile::TEST_ZIP2)
|
419
|
+
end
|
420
|
+
|
421
|
+
def test_incompleteReads
|
422
|
+
ZipInputStream.open(TestZipFile::TEST_ZIP2.zip_name) {
|
423
|
+
|zis|
|
424
|
+
entry = zis.get_next_entry
|
425
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], entry.name)
|
426
|
+
assert zis.gets.length > 0
|
427
|
+
entry = zis.get_next_entry
|
428
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names[1], entry.name)
|
429
|
+
assert_equal(0, entry.size)
|
430
|
+
assert_equal(nil, zis.gets)
|
431
|
+
entry = zis.get_next_entry
|
432
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names[2], entry.name)
|
433
|
+
assert zis.gets.length > 0
|
434
|
+
entry = zis.get_next_entry
|
435
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names[3], entry.name)
|
436
|
+
assert zis.gets.length > 0
|
437
|
+
}
|
438
|
+
end
|
439
|
+
|
440
|
+
def test_rewind
|
441
|
+
ZipInputStream.open(TestZipFile::TEST_ZIP2.zip_name) {
|
442
|
+
|zis|
|
443
|
+
e = zis.get_next_entry
|
444
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names[0], e.name)
|
445
|
+
|
446
|
+
# Do a little reading
|
447
|
+
buf = ""
|
448
|
+
buf << zis.read(100)
|
449
|
+
buf << (zis.gets || "")
|
450
|
+
buf << (zis.gets || "")
|
451
|
+
|
452
|
+
zis.rewind
|
453
|
+
|
454
|
+
buf2 = ""
|
455
|
+
buf2 << zis.read(100)
|
456
|
+
buf2 << (zis.gets || "")
|
457
|
+
buf2 << (zis.gets || "")
|
458
|
+
|
459
|
+
assert_equal(buf, buf2)
|
460
|
+
|
461
|
+
zis.rewind
|
462
|
+
|
463
|
+
assert_entry(e.name, zis, e.name)
|
464
|
+
}
|
465
|
+
end
|
466
|
+
|
467
|
+
end
|
468
|
+
|
469
|
+
|
470
|
+
module CrcTest
|
471
|
+
|
472
|
+
class TestOutputStream
|
473
|
+
include IOExtras::AbstractOutputStream
|
474
|
+
|
475
|
+
attr_accessor :buffer
|
476
|
+
|
477
|
+
def initialize
|
478
|
+
@buffer = ""
|
479
|
+
end
|
480
|
+
|
481
|
+
def << (data)
|
482
|
+
@buffer << data
|
483
|
+
self
|
484
|
+
end
|
485
|
+
end
|
486
|
+
|
487
|
+
def run_crc_test(compressorClass)
|
488
|
+
str = "Here's a nice little text to compute the crc for! Ho hum, it is nice nice nice nice indeed."
|
489
|
+
fakeOut = TestOutputStream.new
|
490
|
+
|
491
|
+
deflater = compressorClass.new(fakeOut)
|
492
|
+
deflater << str
|
493
|
+
assert_equal(0x919920fc, deflater.crc)
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
class PassThruCompressorTest < Test::Unit::TestCase
|
500
|
+
include CrcTest
|
501
|
+
|
502
|
+
def test_size
|
503
|
+
File.open("dummy.txt", "wb") {
|
504
|
+
|file|
|
505
|
+
compressor = PassThruCompressor.new(file)
|
506
|
+
|
507
|
+
assert_equal(0, compressor.size)
|
508
|
+
|
509
|
+
t1 = "hello world"
|
510
|
+
t2 = ""
|
511
|
+
t3 = "bingo"
|
512
|
+
|
513
|
+
compressor << t1
|
514
|
+
assert_equal(compressor.size, t1.size)
|
515
|
+
|
516
|
+
compressor << t2
|
517
|
+
assert_equal(compressor.size, t1.size + t2.size)
|
518
|
+
|
519
|
+
compressor << t3
|
520
|
+
assert_equal(compressor.size, t1.size + t2.size + t3.size)
|
521
|
+
}
|
522
|
+
end
|
523
|
+
|
524
|
+
def test_crc
|
525
|
+
run_crc_test(PassThruCompressor)
|
526
|
+
end
|
527
|
+
end
|
528
|
+
|
529
|
+
class DeflaterTest < Test::Unit::TestCase
|
530
|
+
include CrcTest
|
531
|
+
|
532
|
+
def test_outputOperator
|
533
|
+
txt = load_file("data/file2.txt")
|
534
|
+
deflate(txt, "deflatertest.bin")
|
535
|
+
inflatedTxt = inflate("deflatertest.bin")
|
536
|
+
assert_equal(txt, inflatedTxt)
|
537
|
+
end
|
538
|
+
|
539
|
+
private
|
540
|
+
def load_file(fileName)
|
541
|
+
txt = nil
|
542
|
+
File.open(fileName, "rb") { |f| txt = f.read }
|
543
|
+
end
|
544
|
+
|
545
|
+
def deflate(data, fileName)
|
546
|
+
File.open(fileName, "wb") {
|
547
|
+
|file|
|
548
|
+
deflater = Deflater.new(file)
|
549
|
+
deflater << data
|
550
|
+
deflater.finish
|
551
|
+
assert_equal(deflater.size, data.size)
|
552
|
+
file << "trailing data for zlib with -MAX_WBITS"
|
553
|
+
}
|
554
|
+
end
|
555
|
+
|
556
|
+
def inflate(fileName)
|
557
|
+
txt = nil
|
558
|
+
File.open(fileName, "rb") {
|
559
|
+
|file|
|
560
|
+
inflater = Inflater.new(file)
|
561
|
+
txt = inflater.read
|
562
|
+
}
|
563
|
+
end
|
564
|
+
|
565
|
+
def test_crc
|
566
|
+
run_crc_test(Deflater)
|
567
|
+
end
|
568
|
+
end
|
569
|
+
|
570
|
+
class ZipOutputStreamTest < Test::Unit::TestCase
|
571
|
+
include AssertEntry
|
572
|
+
|
573
|
+
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
574
|
+
TEST_ZIP.zip_name = "output.zip"
|
575
|
+
|
576
|
+
def test_new
|
577
|
+
zos = ZipOutputStream.new(TEST_ZIP.zip_name)
|
578
|
+
zos.comment = TEST_ZIP.comment
|
579
|
+
write_test_zip(zos)
|
580
|
+
zos.close
|
581
|
+
assert_test_zip_contents(TEST_ZIP)
|
582
|
+
end
|
583
|
+
|
584
|
+
def test_open
|
585
|
+
ZipOutputStream.open(TEST_ZIP.zip_name) {
|
586
|
+
|zos|
|
587
|
+
zos.comment = TEST_ZIP.comment
|
588
|
+
write_test_zip(zos)
|
589
|
+
}
|
590
|
+
assert_test_zip_contents(TEST_ZIP)
|
591
|
+
end
|
592
|
+
|
593
|
+
def test_writingToClosedStream
|
594
|
+
assert_i_o_error_in_closed_stream { |zos| zos << "hello world" }
|
595
|
+
assert_i_o_error_in_closed_stream { |zos| zos.puts "hello world" }
|
596
|
+
assert_i_o_error_in_closed_stream { |zos| zos.write "hello world" }
|
597
|
+
end
|
598
|
+
|
599
|
+
def test_cannotOpenFile
|
600
|
+
name = TestFiles::EMPTY_TEST_DIR
|
601
|
+
begin
|
602
|
+
zos = ZipOutputStream.open(name)
|
603
|
+
rescue Exception
|
604
|
+
assert($!.kind_of?(Errno::EISDIR) || # Linux
|
605
|
+
$!.kind_of?(Errno::EEXIST) || # Windows/cygwin
|
606
|
+
$!.kind_of?(Errno::EACCES), # Windows
|
607
|
+
"Expected Errno::EISDIR (or on win/cygwin: Errno::EEXIST), but was: #{$!.class}")
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
611
|
+
def assert_i_o_error_in_closed_stream
|
612
|
+
assert_raise(IOError) {
|
613
|
+
zos = ZipOutputStream.new("test_putOnClosedStream.zip")
|
614
|
+
zos.close
|
615
|
+
yield zos
|
616
|
+
}
|
617
|
+
end
|
618
|
+
|
619
|
+
def write_test_zip(zos)
|
620
|
+
TEST_ZIP.entry_names.each {
|
621
|
+
|entryName|
|
622
|
+
zos.put_next_entry(entryName)
|
623
|
+
File.open(entryName, "rb") { |f| zos.write(f.read) }
|
624
|
+
}
|
625
|
+
end
|
626
|
+
end
|
627
|
+
|
628
|
+
|
629
|
+
|
630
|
+
module Enumerable
|
631
|
+
def compare_enumerables(otherEnumerable)
|
632
|
+
otherAsArray = otherEnumerable.to_a
|
633
|
+
index=0
|
634
|
+
each_with_index {
|
635
|
+
|element, index|
|
636
|
+
return false unless yield(element, otherAsArray[index])
|
637
|
+
}
|
638
|
+
return index+1 == otherAsArray.size
|
639
|
+
end
|
640
|
+
end
|
641
|
+
|
642
|
+
|
643
|
+
class ZipCentralDirectoryEntryTest < Test::Unit::TestCase
|
644
|
+
|
645
|
+
def test_read_from_stream
|
646
|
+
File.open("data/testDirectory.bin", "rb") {
|
647
|
+
|file|
|
648
|
+
entry = ZipEntry.read_c_dir_entry(file)
|
649
|
+
|
650
|
+
assert_equal("longAscii.txt", entry.name)
|
651
|
+
assert_equal(ZipEntry::DEFLATED, entry.compression_method)
|
652
|
+
assert_equal(106490, entry.size)
|
653
|
+
assert_equal(3784, entry.compressed_size)
|
654
|
+
assert_equal(0xfcd1799c, entry.crc)
|
655
|
+
assert_equal("", entry.comment)
|
656
|
+
|
657
|
+
entry = ZipEntry.read_c_dir_entry(file)
|
658
|
+
assert_equal("empty.txt", entry.name)
|
659
|
+
assert_equal(ZipEntry::STORED, entry.compression_method)
|
660
|
+
assert_equal(0, entry.size)
|
661
|
+
assert_equal(0, entry.compressed_size)
|
662
|
+
assert_equal(0x0, entry.crc)
|
663
|
+
assert_equal("", entry.comment)
|
664
|
+
|
665
|
+
entry = ZipEntry.read_c_dir_entry(file)
|
666
|
+
assert_equal("short.txt", entry.name)
|
667
|
+
assert_equal(ZipEntry::STORED, entry.compression_method)
|
668
|
+
assert_equal(6, entry.size)
|
669
|
+
assert_equal(6, entry.compressed_size)
|
670
|
+
assert_equal(0xbb76fe69, entry.crc)
|
671
|
+
assert_equal("", entry.comment)
|
672
|
+
|
673
|
+
entry = ZipEntry.read_c_dir_entry(file)
|
674
|
+
assert_equal("longBinary.bin", entry.name)
|
675
|
+
assert_equal(ZipEntry::DEFLATED, entry.compression_method)
|
676
|
+
assert_equal(1000024, entry.size)
|
677
|
+
assert_equal(70847, entry.compressed_size)
|
678
|
+
assert_equal(0x10da7d59, entry.crc)
|
679
|
+
assert_equal("", entry.comment)
|
680
|
+
|
681
|
+
entry = ZipEntry.read_c_dir_entry(file)
|
682
|
+
assert_equal(nil, entry)
|
683
|
+
# Fields that are not check by this test:
|
684
|
+
# version made by 2 bytes
|
685
|
+
# version needed to extract 2 bytes
|
686
|
+
# general purpose bit flag 2 bytes
|
687
|
+
# last mod file time 2 bytes
|
688
|
+
# last mod file date 2 bytes
|
689
|
+
# compressed size 4 bytes
|
690
|
+
# uncompressed size 4 bytes
|
691
|
+
# disk number start 2 bytes
|
692
|
+
# internal file attributes 2 bytes
|
693
|
+
# external file attributes 4 bytes
|
694
|
+
# relative offset of local header 4 bytes
|
695
|
+
|
696
|
+
# file name (variable size)
|
697
|
+
# extra field (variable size)
|
698
|
+
# file comment (variable size)
|
699
|
+
|
700
|
+
}
|
701
|
+
end
|
702
|
+
|
703
|
+
def test_ReadEntryFromTruncatedZipFile
|
704
|
+
fragment=""
|
705
|
+
File.open("data/testDirectory.bin") { |f| fragment = f.read(12) } # cdir entry header is at least 46 bytes
|
706
|
+
fragment.extend(IOizeString)
|
707
|
+
entry = ZipEntry.new
|
708
|
+
entry.read_c_dir_entry(fragment)
|
709
|
+
fail "ZipError expected"
|
710
|
+
rescue ZipError
|
711
|
+
end
|
712
|
+
|
713
|
+
end
|
714
|
+
|
715
|
+
|
716
|
+
class ZipEntrySetTest < Test::Unit::TestCase
|
717
|
+
ZIP_ENTRIES = [
|
718
|
+
ZipEntry.new("zipfile.zip", "name1", "comment1"),
|
719
|
+
ZipEntry.new("zipfile.zip", "name2", "comment1"),
|
720
|
+
ZipEntry.new("zipfile.zip", "name3", "comment1"),
|
721
|
+
ZipEntry.new("zipfile.zip", "name4", "comment1"),
|
722
|
+
ZipEntry.new("zipfile.zip", "name5", "comment1"),
|
723
|
+
ZipEntry.new("zipfile.zip", "name6", "comment1")
|
724
|
+
]
|
725
|
+
|
726
|
+
def setup
|
727
|
+
@zipEntrySet = ZipEntrySet.new(ZIP_ENTRIES)
|
728
|
+
end
|
729
|
+
|
730
|
+
def test_include
|
731
|
+
assert(@zipEntrySet.include?(ZIP_ENTRIES.first))
|
732
|
+
assert(! @zipEntrySet.include?(ZipEntry.new("different.zip", "different", "aComment")))
|
733
|
+
end
|
734
|
+
|
735
|
+
def test_size
|
736
|
+
assert_equal(ZIP_ENTRIES.size, @zipEntrySet.size)
|
737
|
+
assert_equal(ZIP_ENTRIES.size, @zipEntrySet.length)
|
738
|
+
@zipEntrySet << ZipEntry.new("a", "b", "c")
|
739
|
+
assert_equal(ZIP_ENTRIES.size + 1, @zipEntrySet.length)
|
740
|
+
end
|
741
|
+
|
742
|
+
def test_add
|
743
|
+
zes = ZipEntrySet.new
|
744
|
+
entry1 = ZipEntry.new("zf.zip", "name1")
|
745
|
+
entry2 = ZipEntry.new("zf.zip", "name2")
|
746
|
+
zes << entry1
|
747
|
+
assert(zes.include?(entry1))
|
748
|
+
zes.push(entry2)
|
749
|
+
assert(zes.include?(entry2))
|
750
|
+
end
|
751
|
+
|
752
|
+
def test_delete
|
753
|
+
assert_equal(ZIP_ENTRIES.size, @zipEntrySet.size)
|
754
|
+
entry = @zipEntrySet.delete(ZIP_ENTRIES.first)
|
755
|
+
assert_equal(ZIP_ENTRIES.size - 1, @zipEntrySet.size)
|
756
|
+
assert_equal(ZIP_ENTRIES.first, entry)
|
757
|
+
|
758
|
+
entry = @zipEntrySet.delete(ZIP_ENTRIES.first)
|
759
|
+
assert_equal(ZIP_ENTRIES.size - 1, @zipEntrySet.size)
|
760
|
+
assert_nil(entry)
|
761
|
+
end
|
762
|
+
|
763
|
+
def test_each
|
764
|
+
# Tested indirectly via each_with_index
|
765
|
+
count = 0
|
766
|
+
@zipEntrySet.each_with_index {
|
767
|
+
|entry, index|
|
768
|
+
assert(ZIP_ENTRIES.include?(entry))
|
769
|
+
count = count.succ
|
770
|
+
}
|
771
|
+
assert_equal(ZIP_ENTRIES.size, count)
|
772
|
+
end
|
773
|
+
|
774
|
+
def test_entries
|
775
|
+
assert_equal(ZIP_ENTRIES.sort, @zipEntrySet.entries.sort)
|
776
|
+
end
|
777
|
+
|
778
|
+
def test_compound
|
779
|
+
newEntry = ZipEntry.new("zf.zip", "new entry", "new entry's comment")
|
780
|
+
assert_equal(ZIP_ENTRIES.size, @zipEntrySet.size)
|
781
|
+
@zipEntrySet << newEntry
|
782
|
+
assert_equal(ZIP_ENTRIES.size + 1, @zipEntrySet.size)
|
783
|
+
assert(@zipEntrySet.include?(newEntry))
|
784
|
+
|
785
|
+
@zipEntrySet.delete(newEntry)
|
786
|
+
assert_equal(ZIP_ENTRIES.size, @zipEntrySet.size)
|
787
|
+
end
|
788
|
+
|
789
|
+
def test_dup
|
790
|
+
copy = @zipEntrySet.dup
|
791
|
+
assert_equal(@zipEntrySet, copy)
|
792
|
+
|
793
|
+
# demonstrate that this is a deep copy
|
794
|
+
copy.entries[0].name = "a totally different name"
|
795
|
+
assert(@zipEntrySet != copy)
|
796
|
+
end
|
797
|
+
|
798
|
+
def test_parent
|
799
|
+
entries = [
|
800
|
+
ZipEntry.new("zf.zip", "a"),
|
801
|
+
ZipEntry.new("zf.zip", "a/"),
|
802
|
+
ZipEntry.new("zf.zip", "a/b"),
|
803
|
+
ZipEntry.new("zf.zip", "a/b/"),
|
804
|
+
ZipEntry.new("zf.zip", "a/b/c"),
|
805
|
+
ZipEntry.new("zf.zip", "a/b/c/")
|
806
|
+
]
|
807
|
+
entrySet = ZipEntrySet.new(entries)
|
808
|
+
|
809
|
+
assert_equal(nil, entrySet.parent(entries[0]))
|
810
|
+
assert_equal(nil, entrySet.parent(entries[1]))
|
811
|
+
assert_equal(entries[1], entrySet.parent(entries[2]))
|
812
|
+
assert_equal(entries[1], entrySet.parent(entries[3]))
|
813
|
+
assert_equal(entries[3], entrySet.parent(entries[4]))
|
814
|
+
assert_equal(entries[3], entrySet.parent(entries[5]))
|
815
|
+
end
|
816
|
+
end
|
817
|
+
|
818
|
+
|
819
|
+
class ZipCentralDirectoryTest < Test::Unit::TestCase
|
820
|
+
|
821
|
+
def test_read_from_stream
|
822
|
+
File.open(TestZipFile::TEST_ZIP2.zip_name, "rb") {
|
823
|
+
|zipFile|
|
824
|
+
cdir = ZipCentralDirectory.read_from_stream(zipFile)
|
825
|
+
|
826
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names.size, cdir.size)
|
827
|
+
assert(cdir.entries.sort.compare_enumerables(TestZipFile::TEST_ZIP2.entry_names.sort) {
|
828
|
+
|cdirEntry, testEntryName|
|
829
|
+
cdirEntry.name == testEntryName
|
830
|
+
})
|
831
|
+
assert_equal(TestZipFile::TEST_ZIP2.comment, cdir.comment)
|
832
|
+
}
|
833
|
+
end
|
834
|
+
|
835
|
+
def test_readFromInvalidStream
|
836
|
+
File.open("data/file2.txt", "rb") {
|
837
|
+
|zipFile|
|
838
|
+
cdir = ZipCentralDirectory.new
|
839
|
+
cdir.read_from_stream(zipFile)
|
840
|
+
}
|
841
|
+
fail "ZipError expected!"
|
842
|
+
rescue ZipError
|
843
|
+
end
|
844
|
+
|
845
|
+
def test_ReadFromTruncatedZipFile
|
846
|
+
fragment=""
|
847
|
+
File.open("data/testDirectory.bin") { |f| fragment = f.read }
|
848
|
+
fragment.slice!(12) # removed part of first cdir entry. eocd structure still complete
|
849
|
+
fragment.extend(IOizeString)
|
850
|
+
entry = ZipCentralDirectory.new
|
851
|
+
entry.read_from_stream(fragment)
|
852
|
+
fail "ZipError expected"
|
853
|
+
rescue ZipError
|
854
|
+
end
|
855
|
+
|
856
|
+
def test_write_to_stream
|
857
|
+
entries = [ ZipEntry.new("file.zip", "flimse", "myComment", "somethingExtra"),
|
858
|
+
ZipEntry.new("file.zip", "secondEntryName"),
|
859
|
+
ZipEntry.new("file.zip", "lastEntry.txt", "Has a comment too") ]
|
860
|
+
cdir = ZipCentralDirectory.new(entries, "my zip comment")
|
861
|
+
File.open("cdirtest.bin", "wb") { |f| cdir.write_to_stream(f) }
|
862
|
+
cdirReadback = ZipCentralDirectory.new
|
863
|
+
File.open("cdirtest.bin", "rb") { |f| cdirReadback.read_from_stream(f) }
|
864
|
+
|
865
|
+
assert_equal(cdir.entries.sort, cdirReadback.entries.sort)
|
866
|
+
end
|
867
|
+
|
868
|
+
def test_equality
|
869
|
+
cdir1 = ZipCentralDirectory.new([ ZipEntry.new("file.zip", "flimse", nil,
|
870
|
+
"somethingExtra"),
|
871
|
+
ZipEntry.new("file.zip", "secondEntryName"),
|
872
|
+
ZipEntry.new("file.zip", "lastEntry.txt") ],
|
873
|
+
"my zip comment")
|
874
|
+
cdir2 = ZipCentralDirectory.new([ ZipEntry.new("file.zip", "flimse", nil,
|
875
|
+
"somethingExtra"),
|
876
|
+
ZipEntry.new("file.zip", "secondEntryName"),
|
877
|
+
ZipEntry.new("file.zip", "lastEntry.txt") ],
|
878
|
+
"my zip comment")
|
879
|
+
cdir3 = ZipCentralDirectory.new([ ZipEntry.new("file.zip", "flimse", nil,
|
880
|
+
"somethingExtra"),
|
881
|
+
ZipEntry.new("file.zip", "secondEntryName"),
|
882
|
+
ZipEntry.new("file.zip", "lastEntry.txt") ],
|
883
|
+
"comment?")
|
884
|
+
cdir4 = ZipCentralDirectory.new([ ZipEntry.new("file.zip", "flimse", nil,
|
885
|
+
"somethingExtra"),
|
886
|
+
ZipEntry.new("file.zip", "lastEntry.txt") ],
|
887
|
+
"comment?")
|
888
|
+
assert_equal(cdir1, cdir1)
|
889
|
+
assert_equal(cdir1, cdir2)
|
890
|
+
|
891
|
+
assert(cdir1 != cdir3)
|
892
|
+
assert(cdir2 != cdir3)
|
893
|
+
assert(cdir2 != cdir3)
|
894
|
+
assert(cdir3 != cdir4)
|
895
|
+
|
896
|
+
assert(cdir3 != "hello")
|
897
|
+
end
|
898
|
+
end
|
899
|
+
|
900
|
+
|
901
|
+
class BasicZipFileTest < Test::Unit::TestCase
|
902
|
+
include AssertEntry
|
903
|
+
|
904
|
+
def setup
|
905
|
+
@zipFile = ZipFile.new(TestZipFile::TEST_ZIP2.zip_name)
|
906
|
+
@testEntryNameIndex=0
|
907
|
+
end
|
908
|
+
|
909
|
+
def test_entries
|
910
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names.sort,
|
911
|
+
@zipFile.entries.entries.sort.map {|e| e.name} )
|
912
|
+
end
|
913
|
+
|
914
|
+
def test_each
|
915
|
+
count = 0
|
916
|
+
visited = {}
|
917
|
+
@zipFile.each {
|
918
|
+
|entry|
|
919
|
+
assert(TestZipFile::TEST_ZIP2.entry_names.include?(entry.name))
|
920
|
+
assert(! visited.include?(entry.name))
|
921
|
+
visited[entry.name] = nil
|
922
|
+
count = count.succ
|
923
|
+
}
|
924
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names.length, count)
|
925
|
+
end
|
926
|
+
|
927
|
+
def test_foreach
|
928
|
+
count = 0
|
929
|
+
visited = {}
|
930
|
+
ZipFile.foreach(TestZipFile::TEST_ZIP2.zip_name) {
|
931
|
+
|entry|
|
932
|
+
assert(TestZipFile::TEST_ZIP2.entry_names.include?(entry.name))
|
933
|
+
assert(! visited.include?(entry.name))
|
934
|
+
visited[entry.name] = nil
|
935
|
+
count = count.succ
|
936
|
+
}
|
937
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names.length, count)
|
938
|
+
end
|
939
|
+
|
940
|
+
def test_get_input_stream
|
941
|
+
count = 0
|
942
|
+
visited = {}
|
943
|
+
@zipFile.each {
|
944
|
+
|entry|
|
945
|
+
assert_entry(entry.name, @zipFile.get_input_stream(entry), entry.name)
|
946
|
+
assert(! visited.include?(entry.name))
|
947
|
+
visited[entry.name] = nil
|
948
|
+
count = count.succ
|
949
|
+
}
|
950
|
+
assert_equal(TestZipFile::TEST_ZIP2.entry_names.length, count)
|
951
|
+
end
|
952
|
+
|
953
|
+
def test_get_input_streamBlock
|
954
|
+
fileAndEntryName = @zipFile.entries.first.name
|
955
|
+
@zipFile.get_input_stream(fileAndEntryName) {
|
956
|
+
|zis|
|
957
|
+
assert_entryContentsForStream(fileAndEntryName,
|
958
|
+
zis,
|
959
|
+
fileAndEntryName)
|
960
|
+
}
|
961
|
+
end
|
962
|
+
end
|
963
|
+
|
964
|
+
module CommonZipFileFixture
|
965
|
+
include AssertEntry
|
966
|
+
|
967
|
+
EMPTY_FILENAME = "emptyZipFile.zip"
|
968
|
+
|
969
|
+
TEST_ZIP = TestZipFile::TEST_ZIP2.clone
|
970
|
+
TEST_ZIP.zip_name = "4entry_copy.zip"
|
971
|
+
|
972
|
+
def setup
|
973
|
+
File.delete(EMPTY_FILENAME) if File.exists?(EMPTY_FILENAME)
|
974
|
+
File.copy(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
975
|
+
end
|
976
|
+
end
|
977
|
+
|
978
|
+
class ZipFileTest < Test::Unit::TestCase
|
979
|
+
include CommonZipFileFixture
|
980
|
+
|
981
|
+
def test_createFromScratch
|
982
|
+
comment = "a short comment"
|
983
|
+
|
984
|
+
zf = ZipFile.new(EMPTY_FILENAME, ZipFile::CREATE)
|
985
|
+
zf.get_output_stream("myFile") { |os| os.write "myFile contains just this" }
|
986
|
+
zf.mkdir("dir1")
|
987
|
+
zf.comment = comment
|
988
|
+
zf.close
|
989
|
+
|
990
|
+
zfRead = ZipFile.new(EMPTY_FILENAME)
|
991
|
+
assert_equal(comment, zfRead.comment)
|
992
|
+
assert_equal(2, zfRead.entries.length)
|
993
|
+
end
|
994
|
+
|
995
|
+
def test_get_output_stream
|
996
|
+
entryCount = nil
|
997
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
998
|
+
|zf|
|
999
|
+
entryCount = zf.size
|
1000
|
+
zf.get_output_stream('newEntry.txt') {
|
1001
|
+
|os|
|
1002
|
+
os.write "Putting stuff in newEntry.txt"
|
1003
|
+
}
|
1004
|
+
assert_equal(entryCount+1, zf.size)
|
1005
|
+
assert_equal("Putting stuff in newEntry.txt", zf.read("newEntry.txt"))
|
1006
|
+
|
1007
|
+
zf.get_output_stream(zf.get_entry('data/generated/empty.txt')) {
|
1008
|
+
|os|
|
1009
|
+
os.write "Putting stuff in data/generated/empty.txt"
|
1010
|
+
}
|
1011
|
+
assert_equal(entryCount+1, zf.size)
|
1012
|
+
assert_equal("Putting stuff in data/generated/empty.txt", zf.read("data/generated/empty.txt"))
|
1013
|
+
|
1014
|
+
}
|
1015
|
+
|
1016
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1017
|
+
|zf|
|
1018
|
+
assert_equal(entryCount+1, zf.size)
|
1019
|
+
assert_equal("Putting stuff in newEntry.txt", zf.read("newEntry.txt"))
|
1020
|
+
assert_equal("Putting stuff in data/generated/empty.txt", zf.read("data/generated/empty.txt"))
|
1021
|
+
}
|
1022
|
+
end
|
1023
|
+
|
1024
|
+
def test_add
|
1025
|
+
srcFile = "data/file2.txt"
|
1026
|
+
entryName = "newEntryName.rb"
|
1027
|
+
assert(File.exists?(srcFile))
|
1028
|
+
zf = ZipFile.new(EMPTY_FILENAME, ZipFile::CREATE)
|
1029
|
+
zf.add(entryName, srcFile)
|
1030
|
+
zf.close
|
1031
|
+
|
1032
|
+
zfRead = ZipFile.new(EMPTY_FILENAME)
|
1033
|
+
assert_equal("", zfRead.comment)
|
1034
|
+
assert_equal(1, zfRead.entries.length)
|
1035
|
+
assert_equal(entryName, zfRead.entries.first.name)
|
1036
|
+
AssertEntry.assert_contents(srcFile,
|
1037
|
+
zfRead.get_input_stream(entryName) { |zis| zis.read })
|
1038
|
+
end
|
1039
|
+
|
1040
|
+
def test_addExistingEntryName
|
1041
|
+
assert_raise(ZipEntryExistsError) {
|
1042
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1043
|
+
|zf|
|
1044
|
+
zf.add(zf.entries.first.name, "data/file2.txt")
|
1045
|
+
}
|
1046
|
+
}
|
1047
|
+
end
|
1048
|
+
|
1049
|
+
def test_addExistingEntryNameReplace
|
1050
|
+
gotCalled = false
|
1051
|
+
replacedEntry = nil
|
1052
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1053
|
+
|zf|
|
1054
|
+
replacedEntry = zf.entries.first.name
|
1055
|
+
zf.add(replacedEntry, "data/file2.txt") { gotCalled = true; true }
|
1056
|
+
}
|
1057
|
+
assert(gotCalled)
|
1058
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1059
|
+
|zf|
|
1060
|
+
assert_contains(zf, replacedEntry, "data/file2.txt")
|
1061
|
+
}
|
1062
|
+
end
|
1063
|
+
|
1064
|
+
def test_addDirectory
|
1065
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1066
|
+
|zf|
|
1067
|
+
zf.add(TestFiles::EMPTY_TEST_DIR, TestFiles::EMPTY_TEST_DIR)
|
1068
|
+
}
|
1069
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1070
|
+
|zf|
|
1071
|
+
dirEntry = zf.entries.detect { |e| e.name == TestFiles::EMPTY_TEST_DIR+"/" }
|
1072
|
+
assert(dirEntry.is_directory)
|
1073
|
+
}
|
1074
|
+
end
|
1075
|
+
|
1076
|
+
def test_remove
|
1077
|
+
entryToRemove, *remainingEntries = TEST_ZIP.entry_names
|
1078
|
+
|
1079
|
+
File.copy(TestZipFile::TEST_ZIP2.zip_name, TEST_ZIP.zip_name)
|
1080
|
+
|
1081
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1082
|
+
assert(zf.entries.map { |e| e.name }.include?(entryToRemove))
|
1083
|
+
zf.remove(entryToRemove)
|
1084
|
+
assert(! zf.entries.map { |e| e.name }.include?(entryToRemove))
|
1085
|
+
assert_equal(zf.entries.map {|x| x.name }.sort, remainingEntries.sort)
|
1086
|
+
zf.close
|
1087
|
+
|
1088
|
+
zfRead = ZipFile.new(TEST_ZIP.zip_name)
|
1089
|
+
assert(! zfRead.entries.map { |e| e.name }.include?(entryToRemove))
|
1090
|
+
assert_equal(zfRead.entries.map {|x| x.name }.sort, remainingEntries.sort)
|
1091
|
+
zfRead.close
|
1092
|
+
end
|
1093
|
+
|
1094
|
+
|
1095
|
+
def test_rename
|
1096
|
+
entryToRename, *remainingEntries = TEST_ZIP.entry_names
|
1097
|
+
|
1098
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1099
|
+
assert(zf.entries.map { |e| e.name }.include?(entryToRename))
|
1100
|
+
|
1101
|
+
newName = "changed name"
|
1102
|
+
assert(! zf.entries.map { |e| e.name }.include?(newName))
|
1103
|
+
|
1104
|
+
zf.rename(entryToRename, newName)
|
1105
|
+
assert(zf.entries.map { |e| e.name }.include?(newName))
|
1106
|
+
|
1107
|
+
zf.close
|
1108
|
+
|
1109
|
+
zfRead = ZipFile.new(TEST_ZIP.zip_name)
|
1110
|
+
assert(zfRead.entries.map { |e| e.name }.include?(newName))
|
1111
|
+
zfRead.close
|
1112
|
+
end
|
1113
|
+
|
1114
|
+
def test_renameToExistingEntry
|
1115
|
+
oldEntries = nil
|
1116
|
+
ZipFile.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
1117
|
+
|
1118
|
+
assert_raise(ZipEntryExistsError) {
|
1119
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1120
|
+
|zf|
|
1121
|
+
zf.rename(zf.entries[0], zf.entries[1].name)
|
1122
|
+
}
|
1123
|
+
}
|
1124
|
+
|
1125
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1126
|
+
|zf|
|
1127
|
+
assert_equal(oldEntries.sort.map{ |e| e.name }, zf.entries.sort.map{ |e| e.name })
|
1128
|
+
}
|
1129
|
+
end
|
1130
|
+
|
1131
|
+
def test_renameToExistingEntryOverwrite
|
1132
|
+
oldEntries = nil
|
1133
|
+
ZipFile.open(TEST_ZIP.zip_name) { |zf| oldEntries = zf.entries }
|
1134
|
+
|
1135
|
+
gotCalled = false
|
1136
|
+
renamedEntryName = nil
|
1137
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1138
|
+
|zf|
|
1139
|
+
renamedEntryName = zf.entries[0].name
|
1140
|
+
zf.rename(zf.entries[0], zf.entries[1].name) { gotCalled = true; true }
|
1141
|
+
}
|
1142
|
+
|
1143
|
+
assert(gotCalled)
|
1144
|
+
oldEntries.delete_if { |e| e.name == renamedEntryName }
|
1145
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1146
|
+
|zf|
|
1147
|
+
assert_equal(oldEntries.sort.map{ |e| e.name },
|
1148
|
+
zf.entries.sort.map{ |e| e.name })
|
1149
|
+
}
|
1150
|
+
end
|
1151
|
+
|
1152
|
+
def test_renameNonEntry
|
1153
|
+
nonEntry = "bogusEntry"
|
1154
|
+
target_entry = "target_entryName"
|
1155
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1156
|
+
assert(! zf.entries.include?(nonEntry))
|
1157
|
+
assert_raise(Errno::ENOENT) {
|
1158
|
+
zf.rename(nonEntry, target_entry)
|
1159
|
+
}
|
1160
|
+
zf.commit
|
1161
|
+
assert(! zf.entries.include?(target_entry))
|
1162
|
+
ensure
|
1163
|
+
zf.close
|
1164
|
+
end
|
1165
|
+
|
1166
|
+
def test_renameEntryToExistingEntry
|
1167
|
+
entry1, entry2, *remaining = TEST_ZIP.entry_names
|
1168
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1169
|
+
assert_raise(ZipEntryExistsError) {
|
1170
|
+
zf.rename(entry1, entry2)
|
1171
|
+
}
|
1172
|
+
ensure
|
1173
|
+
zf.close
|
1174
|
+
end
|
1175
|
+
|
1176
|
+
def test_replace
|
1177
|
+
entryToReplace = TEST_ZIP.entry_names[2]
|
1178
|
+
newEntrySrcFilename = "data/file2.txt"
|
1179
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1180
|
+
zf.replace(entryToReplace, newEntrySrcFilename)
|
1181
|
+
|
1182
|
+
zf.close
|
1183
|
+
zfRead = ZipFile.new(TEST_ZIP.zip_name)
|
1184
|
+
AssertEntry::assert_contents(newEntrySrcFilename,
|
1185
|
+
zfRead.get_input_stream(entryToReplace) { |is| is.read })
|
1186
|
+
AssertEntry::assert_contents(TEST_ZIP.entry_names[0],
|
1187
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[0]) { |is| is.read })
|
1188
|
+
AssertEntry::assert_contents(TEST_ZIP.entry_names[1],
|
1189
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[1]) { |is| is.read })
|
1190
|
+
AssertEntry::assert_contents(TEST_ZIP.entry_names[3],
|
1191
|
+
zfRead.get_input_stream(TEST_ZIP.entry_names[3]) { |is| is.read })
|
1192
|
+
zfRead.close
|
1193
|
+
end
|
1194
|
+
|
1195
|
+
def test_replaceNonEntry
|
1196
|
+
entryToReplace = "nonExistingEntryname"
|
1197
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1198
|
+
|zf|
|
1199
|
+
assert_raise(Errno::ENOENT) {
|
1200
|
+
zf.replace(entryToReplace, "data/file2.txt")
|
1201
|
+
}
|
1202
|
+
}
|
1203
|
+
end
|
1204
|
+
|
1205
|
+
def test_commit
|
1206
|
+
newName = "renamedFirst"
|
1207
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1208
|
+
oldName = zf.entries.first
|
1209
|
+
zf.rename(oldName, newName)
|
1210
|
+
zf.commit
|
1211
|
+
|
1212
|
+
zfRead = ZipFile.new(TEST_ZIP.zip_name)
|
1213
|
+
assert(zfRead.entries.detect { |e| e.name == newName } != nil)
|
1214
|
+
assert(zfRead.entries.detect { |e| e.name == oldName } == nil)
|
1215
|
+
zfRead.close
|
1216
|
+
|
1217
|
+
zf.close
|
1218
|
+
end
|
1219
|
+
|
1220
|
+
# This test tests that after commit, you
|
1221
|
+
# can delete the file you used to add the entry to the zip file
|
1222
|
+
# with
|
1223
|
+
def test_commitUseZipEntry
|
1224
|
+
File.copy(TestFiles::RANDOM_ASCII_FILE1, "okToDelete.txt")
|
1225
|
+
zf = ZipFile.open(TEST_ZIP.zip_name)
|
1226
|
+
zf.add("okToDelete.txt", "okToDelete.txt")
|
1227
|
+
assert_contains(zf, "okToDelete.txt")
|
1228
|
+
zf.commit
|
1229
|
+
File.move("okToDelete.txt", "okToDeleteMoved.txt")
|
1230
|
+
assert_contains(zf, "okToDelete.txt", "okToDeleteMoved.txt")
|
1231
|
+
end
|
1232
|
+
|
1233
|
+
# def test_close
|
1234
|
+
# zf = ZipFile.new(TEST_ZIP.zip_name)
|
1235
|
+
# zf.close
|
1236
|
+
# assert_raise(IOError) {
|
1237
|
+
# zf.extract(TEST_ZIP.entry_names.first, "hullubullu")
|
1238
|
+
# }
|
1239
|
+
# end
|
1240
|
+
|
1241
|
+
def test_compound1
|
1242
|
+
renamedName = "renamedName"
|
1243
|
+
originalEntries = []
|
1244
|
+
begin
|
1245
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1246
|
+
originalEntries = zf.entries.dup
|
1247
|
+
|
1248
|
+
assert_not_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
1249
|
+
zf.add(TestFiles::RANDOM_ASCII_FILE1,
|
1250
|
+
TestFiles::RANDOM_ASCII_FILE1)
|
1251
|
+
assert_contains(zf, TestFiles::RANDOM_ASCII_FILE1)
|
1252
|
+
|
1253
|
+
zf.rename(zf.entries[0], renamedName)
|
1254
|
+
assert_contains(zf, renamedName)
|
1255
|
+
|
1256
|
+
TestFiles::BINARY_TEST_FILES.each {
|
1257
|
+
|filename|
|
1258
|
+
zf.add(filename, filename)
|
1259
|
+
assert_contains(zf, filename)
|
1260
|
+
}
|
1261
|
+
|
1262
|
+
assert_contains(zf, originalEntries.last.to_s)
|
1263
|
+
zf.remove(originalEntries.last.to_s)
|
1264
|
+
assert_not_contains(zf, originalEntries.last.to_s)
|
1265
|
+
|
1266
|
+
ensure
|
1267
|
+
zf.close
|
1268
|
+
end
|
1269
|
+
begin
|
1270
|
+
zfRead = ZipFile.new(TEST_ZIP.zip_name)
|
1271
|
+
assert_contains(zfRead, TestFiles::RANDOM_ASCII_FILE1)
|
1272
|
+
assert_contains(zfRead, renamedName)
|
1273
|
+
TestFiles::BINARY_TEST_FILES.each {
|
1274
|
+
|filename|
|
1275
|
+
assert_contains(zfRead, filename)
|
1276
|
+
}
|
1277
|
+
assert_not_contains(zfRead, originalEntries.last.to_s)
|
1278
|
+
ensure
|
1279
|
+
zfRead.close
|
1280
|
+
end
|
1281
|
+
end
|
1282
|
+
|
1283
|
+
def test_compound2
|
1284
|
+
begin
|
1285
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1286
|
+
originalEntries = zf.entries.dup
|
1287
|
+
|
1288
|
+
originalEntries.each {
|
1289
|
+
|entry|
|
1290
|
+
zf.remove(entry)
|
1291
|
+
assert_not_contains(zf, entry)
|
1292
|
+
}
|
1293
|
+
assert(zf.entries.empty?)
|
1294
|
+
|
1295
|
+
TestFiles::ASCII_TEST_FILES.each {
|
1296
|
+
|filename|
|
1297
|
+
zf.add(filename, filename)
|
1298
|
+
assert_contains(zf, filename)
|
1299
|
+
}
|
1300
|
+
assert_equal(zf.entries.sort.map { |e| e.name }, TestFiles::ASCII_TEST_FILES)
|
1301
|
+
|
1302
|
+
zf.rename(TestFiles::ASCII_TEST_FILES[0], "newName")
|
1303
|
+
assert_not_contains(zf, TestFiles::ASCII_TEST_FILES[0])
|
1304
|
+
assert_contains(zf, "newName")
|
1305
|
+
ensure
|
1306
|
+
zf.close
|
1307
|
+
end
|
1308
|
+
begin
|
1309
|
+
zfRead = ZipFile.new(TEST_ZIP.zip_name)
|
1310
|
+
asciiTestFiles = TestFiles::ASCII_TEST_FILES.dup
|
1311
|
+
asciiTestFiles.shift
|
1312
|
+
asciiTestFiles.each {
|
1313
|
+
|filename|
|
1314
|
+
assert_contains(zf, filename)
|
1315
|
+
}
|
1316
|
+
|
1317
|
+
assert_contains(zf, "newName")
|
1318
|
+
ensure
|
1319
|
+
zfRead.close
|
1320
|
+
end
|
1321
|
+
end
|
1322
|
+
|
1323
|
+
private
|
1324
|
+
def assert_contains(zf, entryName, filename = entryName)
|
1325
|
+
assert(zf.entries.detect { |e| e.name == entryName} != nil, "entry #{entryName} not in #{zf.entries.join(', ')} in zip file #{zf}")
|
1326
|
+
assert_entryContents(zf, entryName, filename) if File.exists?(filename)
|
1327
|
+
end
|
1328
|
+
|
1329
|
+
def assert_not_contains(zf, entryName)
|
1330
|
+
assert(zf.entries.detect { |e| e.name == entryName} == nil, "entry #{entryName} in #{zf.entries.join(', ')} in zip file #{zf}")
|
1331
|
+
end
|
1332
|
+
end
|
1333
|
+
|
1334
|
+
class ZipFileExtractTest < Test::Unit::TestCase
|
1335
|
+
include CommonZipFileFixture
|
1336
|
+
EXTRACTED_FILENAME = "extEntry"
|
1337
|
+
ENTRY_TO_EXTRACT, *REMAINING_ENTRIES = TEST_ZIP.entry_names.reverse
|
1338
|
+
|
1339
|
+
def setup
|
1340
|
+
super
|
1341
|
+
File.delete(EXTRACTED_FILENAME) if File.exists?(EXTRACTED_FILENAME)
|
1342
|
+
end
|
1343
|
+
|
1344
|
+
def test_extract
|
1345
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1346
|
+
|zf|
|
1347
|
+
zf.extract(ENTRY_TO_EXTRACT, EXTRACTED_FILENAME)
|
1348
|
+
|
1349
|
+
assert(File.exists?(EXTRACTED_FILENAME))
|
1350
|
+
AssertEntry::assert_contents(EXTRACTED_FILENAME,
|
1351
|
+
zf.get_input_stream(ENTRY_TO_EXTRACT) { |is| is.read })
|
1352
|
+
}
|
1353
|
+
end
|
1354
|
+
|
1355
|
+
def test_extractExists
|
1356
|
+
writtenText = "written text"
|
1357
|
+
File.open(EXTRACTED_FILENAME, "w") { |f| f.write(writtenText) }
|
1358
|
+
|
1359
|
+
assert_raise(ZipDestinationFileExistsError) {
|
1360
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1361
|
+
|zf|
|
1362
|
+
zf.extract(zf.entries.first, EXTRACTED_FILENAME)
|
1363
|
+
}
|
1364
|
+
}
|
1365
|
+
File.open(EXTRACTED_FILENAME, "r") {
|
1366
|
+
|f|
|
1367
|
+
assert_equal(writtenText, f.read)
|
1368
|
+
}
|
1369
|
+
end
|
1370
|
+
|
1371
|
+
def test_extractExistsOverwrite
|
1372
|
+
writtenText = "written text"
|
1373
|
+
File.open(EXTRACTED_FILENAME, "w") { |f| f.write(writtenText) }
|
1374
|
+
|
1375
|
+
gotCalledCorrectly = false
|
1376
|
+
ZipFile.open(TEST_ZIP.zip_name) {
|
1377
|
+
|zf|
|
1378
|
+
zf.extract(zf.entries.first, EXTRACTED_FILENAME) {
|
1379
|
+
|entry, extractLoc|
|
1380
|
+
gotCalledCorrectly = zf.entries.first == entry &&
|
1381
|
+
extractLoc == EXTRACTED_FILENAME
|
1382
|
+
true
|
1383
|
+
}
|
1384
|
+
}
|
1385
|
+
|
1386
|
+
assert(gotCalledCorrectly)
|
1387
|
+
File.open(EXTRACTED_FILENAME, "r") {
|
1388
|
+
|f|
|
1389
|
+
assert(writtenText != f.read)
|
1390
|
+
}
|
1391
|
+
end
|
1392
|
+
|
1393
|
+
def test_extractNonEntry
|
1394
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1395
|
+
assert_raise(Errno::ENOENT) { zf.extract("nonExistingEntry", "nonExistingEntry") }
|
1396
|
+
ensure
|
1397
|
+
zf.close if zf
|
1398
|
+
end
|
1399
|
+
|
1400
|
+
def test_extractNonEntry2
|
1401
|
+
outFile = "outfile"
|
1402
|
+
assert_raise(Errno::ENOENT) {
|
1403
|
+
zf = ZipFile.new(TEST_ZIP.zip_name)
|
1404
|
+
nonEntry = "hotdog-diddelidoo"
|
1405
|
+
assert(! zf.entries.include?(nonEntry))
|
1406
|
+
zf.extract(nonEntry, outFile)
|
1407
|
+
zf.close
|
1408
|
+
}
|
1409
|
+
assert(! File.exists?(outFile))
|
1410
|
+
end
|
1411
|
+
|
1412
|
+
end
|
1413
|
+
|
1414
|
+
class ZipFileExtractDirectoryTest < Test::Unit::TestCase
|
1415
|
+
include CommonZipFileFixture
|
1416
|
+
TEST_OUT_NAME = "emptyOutDir"
|
1417
|
+
|
1418
|
+
def open_zip(&aProc)
|
1419
|
+
assert(aProc != nil)
|
1420
|
+
ZipFile.open(TestZipFile::TEST_ZIP4.zip_name, &aProc)
|
1421
|
+
end
|
1422
|
+
|
1423
|
+
def extract_test_dir(&aProc)
|
1424
|
+
open_zip {
|
1425
|
+
|zf|
|
1426
|
+
zf.extract(TestFiles::EMPTY_TEST_DIR, TEST_OUT_NAME, &aProc)
|
1427
|
+
}
|
1428
|
+
end
|
1429
|
+
|
1430
|
+
def setup
|
1431
|
+
super
|
1432
|
+
|
1433
|
+
Dir.rmdir(TEST_OUT_NAME) if File.directory? TEST_OUT_NAME
|
1434
|
+
File.delete(TEST_OUT_NAME) if File.exists? TEST_OUT_NAME
|
1435
|
+
end
|
1436
|
+
|
1437
|
+
def test_extractDirectory
|
1438
|
+
extract_test_dir
|
1439
|
+
assert(File.directory?(TEST_OUT_NAME))
|
1440
|
+
end
|
1441
|
+
|
1442
|
+
def test_extractDirectoryExistsAsDir
|
1443
|
+
Dir.mkdir TEST_OUT_NAME
|
1444
|
+
extract_test_dir
|
1445
|
+
assert(File.directory?(TEST_OUT_NAME))
|
1446
|
+
end
|
1447
|
+
|
1448
|
+
def test_extractDirectoryExistsAsFile
|
1449
|
+
File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" }
|
1450
|
+
assert_raise(ZipDestinationFileExistsError) { extract_test_dir }
|
1451
|
+
end
|
1452
|
+
|
1453
|
+
def test_extractDirectoryExistsAsFileOverwrite
|
1454
|
+
File.open(TEST_OUT_NAME, "w") { |f| f.puts "something" }
|
1455
|
+
gotCalled = false
|
1456
|
+
extract_test_dir {
|
1457
|
+
|entry, destPath|
|
1458
|
+
gotCalled = true
|
1459
|
+
assert_equal(TEST_OUT_NAME, destPath)
|
1460
|
+
assert(entry.is_directory)
|
1461
|
+
true
|
1462
|
+
}
|
1463
|
+
assert(gotCalled)
|
1464
|
+
assert(File.directory?(TEST_OUT_NAME))
|
1465
|
+
end
|
1466
|
+
end
|
1467
|
+
|
1468
|
+
class ZipStreamableFileTest < Test::Unit::TestCase
|
1469
|
+
def test_equality
|
1470
|
+
zipEntry1 = ZipEntry.new("zf.zip", "entryname1", "comment")
|
1471
|
+
zipEntry2 = ZipEntry.new("zf.zip", "entryname2", "comment")
|
1472
|
+
|
1473
|
+
zipStreamableFile1 = ZipStreamableFile.new(zipEntry1, "path")
|
1474
|
+
zipStreamableFile2 = ZipStreamableFile.new(zipEntry1, "path")
|
1475
|
+
zipStreamableFile3 = ZipStreamableFile.new(zipEntry1, "anotherPath")
|
1476
|
+
zipStreamableFile4 = ZipStreamableFile.new(zipEntry2, "path")
|
1477
|
+
|
1478
|
+
assert_equal(zipStreamableFile1, zipStreamableFile1)
|
1479
|
+
assert_equal(zipStreamableFile1, zipStreamableFile2)
|
1480
|
+
assert(zipStreamableFile1 != zipStreamableFile3)
|
1481
|
+
assert(zipStreamableFile1 != zipStreamableFile4)
|
1482
|
+
assert(zipStreamableFile1 != zipEntry1)
|
1483
|
+
assert(zipStreamableFile1 != "hej")
|
1484
|
+
end
|
1485
|
+
end
|
1486
|
+
|
1487
|
+
class ZipExtraFieldTest < Test::Unit::TestCase
|
1488
|
+
def test_new
|
1489
|
+
extra_pure = ZipExtraField.new("")
|
1490
|
+
extra_withstr = ZipExtraField.new("foo")
|
1491
|
+
assert_instance_of(ZipExtraField, extra_pure)
|
1492
|
+
assert_instance_of(ZipExtraField, extra_withstr)
|
1493
|
+
end
|
1494
|
+
|
1495
|
+
def test_unknownfield
|
1496
|
+
extra = ZipExtraField.new("foo")
|
1497
|
+
assert_equal(extra["Unknown"], "foo")
|
1498
|
+
extra.merge("a")
|
1499
|
+
assert_equal(extra["Unknown"], "fooa")
|
1500
|
+
extra.merge("barbaz")
|
1501
|
+
assert_equal(extra.to_s, "fooabarbaz")
|
1502
|
+
end
|
1503
|
+
|
1504
|
+
|
1505
|
+
def test_merge
|
1506
|
+
str = "UT\x5\0\x3\250$\r@Ux\0\0"
|
1507
|
+
extra1 = ZipExtraField.new("")
|
1508
|
+
extra2 = ZipExtraField.new(str)
|
1509
|
+
assert(! extra1.member?("UniversalTime"))
|
1510
|
+
assert(extra2.member?("UniversalTime"))
|
1511
|
+
extra1.merge(str)
|
1512
|
+
assert_equal(extra1["UniversalTime"].mtime, extra2["UniversalTime"].mtime)
|
1513
|
+
end
|
1514
|
+
|
1515
|
+
def test_length
|
1516
|
+
str = "UT\x5\0\x3\250$\r@Ux\0\0Te\0\0testit"
|
1517
|
+
extra = ZipExtraField.new(str)
|
1518
|
+
assert_equal(extra.local_length, extra.to_local_bin.length)
|
1519
|
+
assert_equal(extra.c_dir_length, extra.to_c_dir_bin.length)
|
1520
|
+
extra.merge("foo")
|
1521
|
+
assert_equal(extra.local_length, extra.to_local_bin.length)
|
1522
|
+
assert_equal(extra.c_dir_length, extra.to_c_dir_bin.length)
|
1523
|
+
end
|
1524
|
+
|
1525
|
+
|
1526
|
+
def test_to_s
|
1527
|
+
str = "UT\x5\0\x3\250$\r@Ux\0\0Te\0\0testit"
|
1528
|
+
extra = ZipExtraField.new(str)
|
1529
|
+
assert_instance_of(String, extra.to_s)
|
1530
|
+
|
1531
|
+
s = extra.to_s
|
1532
|
+
extra.merge("foo")
|
1533
|
+
assert_equal(s.length + 3, extra.to_s.length)
|
1534
|
+
end
|
1535
|
+
|
1536
|
+
def test_equality
|
1537
|
+
str = "UT\x5\0\x3\250$\r@"
|
1538
|
+
extra1 = ZipExtraField.new(str)
|
1539
|
+
extra2 = ZipExtraField.new(str)
|
1540
|
+
extra3 = ZipExtraField.new(str)
|
1541
|
+
assert_equal(extra1, extra2)
|
1542
|
+
|
1543
|
+
extra2["UniversalTime"].mtime = Time.now
|
1544
|
+
assert(extra1 != extra2)
|
1545
|
+
|
1546
|
+
extra3.create("IUnix")
|
1547
|
+
assert(extra1 != extra3)
|
1548
|
+
|
1549
|
+
extra1.create("IUnix")
|
1550
|
+
assert_equal(extra1, extra3)
|
1551
|
+
end
|
1552
|
+
|
1553
|
+
end
|
1554
|
+
|
1555
|
+
# Copyright (C) 2002, 2003 Thomas Sondergaard
|
1556
|
+
# rubyzip is free software; you can redistribute it and/or
|
1557
|
+
# modify it under the terms of the ruby license.
|