rubyzip 0.9.4 → 0.9.5

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.

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