archive-zip 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. data/HACKING +25 -42
  2. data/NEWS +25 -0
  3. data/README +2 -2
  4. data/Rakefile +202 -0
  5. data/TODO +5 -0
  6. data/default.mspec +8 -0
  7. data/lib/archive/support/binary_stringio.rb +23 -0
  8. data/lib/archive/support/integer.rb +13 -0
  9. data/lib/archive/support/io-like.rb +3 -1
  10. data/lib/archive/support/ioextensions.rb +16 -0
  11. data/lib/archive/support/iowindow.rb +10 -18
  12. data/lib/archive/support/time.rb +2 -0
  13. data/lib/archive/support/zlib.rb +298 -71
  14. data/lib/archive/zip.rb +161 -139
  15. data/lib/archive/zip/codec.rb +2 -0
  16. data/lib/archive/zip/codec/deflate.rb +59 -11
  17. data/lib/archive/zip/codec/null_encryption.rb +75 -14
  18. data/lib/archive/zip/codec/store.rb +75 -26
  19. data/lib/archive/zip/codec/traditional_encryption.rb +146 -35
  20. data/lib/archive/zip/data_descriptor.rb +6 -4
  21. data/lib/archive/zip/entry.rb +184 -132
  22. data/lib/archive/zip/error.rb +2 -0
  23. data/lib/archive/zip/extra_field.rb +20 -6
  24. data/lib/archive/zip/extra_field/extended_timestamp.rb +141 -60
  25. data/lib/archive/zip/extra_field/raw.rb +70 -12
  26. data/lib/archive/zip/extra_field/unix.rb +58 -16
  27. data/lib/archive/zip/version.rb +6 -0
  28. data/spec/archive/zip/codec/deflate/compress/checksum_spec.rb +42 -0
  29. data/spec/archive/zip/codec/deflate/compress/close_spec.rb +44 -0
  30. data/spec/archive/zip/codec/deflate/compress/crc32_spec.rb +21 -0
  31. data/spec/archive/zip/codec/deflate/compress/data_descriptor_spec.rb +67 -0
  32. data/spec/archive/zip/codec/deflate/compress/new_spec.rb +37 -0
  33. data/spec/archive/zip/codec/deflate/compress/open_spec.rb +46 -0
  34. data/spec/archive/zip/codec/deflate/compress/write_spec.rb +109 -0
  35. data/spec/archive/zip/codec/deflate/decompress/checksum_spec.rb +18 -0
  36. data/spec/archive/zip/codec/deflate/decompress/close_spec.rb +33 -0
  37. data/spec/archive/zip/codec/deflate/decompress/crc32_spec.rb +18 -0
  38. data/spec/archive/zip/codec/deflate/decompress/data_descriptor_spec.rb +67 -0
  39. data/spec/archive/zip/codec/deflate/decompress/new_spec.rb +14 -0
  40. data/spec/archive/zip/codec/deflate/decompress/open_spec.rb +27 -0
  41. data/spec/archive/zip/codec/deflate/fixtures/classes.rb +25 -0
  42. data/spec/archive/zip/codec/deflate/fixtures/compressed_file.bin +1 -0
  43. data/spec/archive/zip/codec/deflate/fixtures/compressed_file_nocomp.bin +0 -0
  44. data/spec/archive/zip/codec/deflate/fixtures/raw_file.txt +10 -0
  45. data/spec/archive/zip/codec/null_encryption/decrypt/close_spec.rb +33 -0
  46. data/spec/archive/zip/codec/null_encryption/decrypt/new_spec.rb +14 -0
  47. data/spec/archive/zip/codec/null_encryption/decrypt/open_spec.rb +27 -0
  48. data/spec/archive/zip/codec/null_encryption/decrypt/read_spec.rb +24 -0
  49. data/spec/archive/zip/codec/null_encryption/decrypt/rewind_spec.rb +25 -0
  50. data/spec/archive/zip/codec/null_encryption/decrypt/seek_spec.rb +57 -0
  51. data/spec/archive/zip/codec/null_encryption/decrypt/tell_spec.rb +21 -0
  52. data/spec/archive/zip/codec/null_encryption/encrypt/close_spec.rb +33 -0
  53. data/spec/archive/zip/codec/null_encryption/encrypt/new_spec.rb +14 -0
  54. data/spec/archive/zip/codec/null_encryption/encrypt/open_spec.rb +27 -0
  55. data/spec/archive/zip/codec/null_encryption/encrypt/rewind_spec.rb +26 -0
  56. data/spec/archive/zip/codec/null_encryption/encrypt/seek_spec.rb +50 -0
  57. data/spec/archive/zip/codec/null_encryption/encrypt/tell_spec.rb +29 -0
  58. data/spec/archive/zip/codec/null_encryption/encrypt/write_spec.rb +29 -0
  59. data/spec/archive/zip/codec/null_encryption/fixtures/classes.rb +12 -0
  60. data/spec/archive/zip/codec/null_encryption/fixtures/raw_file.txt +10 -0
  61. data/spec/archive/zip/codec/store/compress/close_spec.rb +33 -0
  62. data/spec/archive/zip/codec/store/compress/data_descriptor_spec.rb +68 -0
  63. data/spec/archive/zip/codec/store/compress/new_spec.rb +14 -0
  64. data/spec/archive/zip/codec/store/compress/open_spec.rb +27 -0
  65. data/spec/archive/zip/codec/store/compress/rewind_spec.rb +26 -0
  66. data/spec/archive/zip/codec/store/compress/seek_spec.rb +50 -0
  67. data/spec/archive/zip/codec/store/compress/tell_spec.rb +29 -0
  68. data/spec/archive/zip/codec/store/compress/write_spec.rb +29 -0
  69. data/spec/archive/zip/codec/store/decompress/close_spec.rb +33 -0
  70. data/spec/archive/zip/codec/store/decompress/data_descriptor_spec.rb +68 -0
  71. data/spec/archive/zip/codec/store/decompress/new_spec.rb +14 -0
  72. data/spec/archive/zip/codec/store/decompress/open_spec.rb +27 -0
  73. data/spec/archive/zip/codec/store/decompress/read_spec.rb +24 -0
  74. data/spec/archive/zip/codec/store/decompress/rewind_spec.rb +25 -0
  75. data/spec/archive/zip/codec/store/decompress/seek_spec.rb +57 -0
  76. data/spec/archive/zip/codec/store/decompress/tell_spec.rb +21 -0
  77. data/spec/archive/zip/codec/store/fixtures/classes.rb +12 -0
  78. data/spec/archive/zip/codec/store/fixtures/raw_file.txt +10 -0
  79. data/spec/archive/zip/codec/traditional_encryption/decrypt/close_spec.rb +64 -0
  80. data/spec/archive/zip/codec/traditional_encryption/decrypt/new_spec.rb +18 -0
  81. data/spec/archive/zip/codec/traditional_encryption/decrypt/open_spec.rb +39 -0
  82. data/spec/archive/zip/codec/traditional_encryption/decrypt/read_spec.rb +126 -0
  83. data/spec/archive/zip/codec/traditional_encryption/decrypt/rewind_spec.rb +38 -0
  84. data/spec/archive/zip/codec/traditional_encryption/decrypt/seek_spec.rb +82 -0
  85. data/spec/archive/zip/codec/traditional_encryption/decrypt/tell_spec.rb +25 -0
  86. data/spec/archive/zip/codec/traditional_encryption/encrypt/close_spec.rb +64 -0
  87. data/spec/archive/zip/codec/traditional_encryption/encrypt/new_spec.rb +18 -0
  88. data/spec/archive/zip/codec/traditional_encryption/encrypt/open_spec.rb +39 -0
  89. data/spec/archive/zip/codec/traditional_encryption/encrypt/rewind_spec.rb +41 -0
  90. data/spec/archive/zip/codec/traditional_encryption/encrypt/seek_spec.rb +75 -0
  91. data/spec/archive/zip/codec/traditional_encryption/encrypt/tell_spec.rb +42 -0
  92. data/spec/archive/zip/codec/traditional_encryption/encrypt/write_spec.rb +127 -0
  93. data/spec/archive/zip/codec/traditional_encryption/fixtures/classes.rb +27 -0
  94. data/spec/archive/zip/codec/traditional_encryption/fixtures/encrypted_file.bin +0 -0
  95. data/spec/archive/zip/codec/traditional_encryption/fixtures/raw_file.txt +10 -0
  96. data/spec/binary_stringio/new_spec.rb +34 -0
  97. data/spec/binary_stringio/set_encoding_spec.rb +14 -0
  98. data/spec/ioextensions/read_exactly_spec.rb +50 -0
  99. data/spec/zlib/fixtures/classes.rb +65 -0
  100. data/spec/zlib/fixtures/compressed_file.bin +1 -0
  101. data/spec/zlib/fixtures/compressed_file_gzip.bin +0 -0
  102. data/spec/zlib/fixtures/compressed_file_huffman.bin +2 -0
  103. data/spec/zlib/fixtures/compressed_file_minmem.bin +0 -0
  104. data/spec/zlib/fixtures/compressed_file_minwin.bin +1 -0
  105. data/spec/zlib/fixtures/compressed_file_nocomp.bin +0 -0
  106. data/spec/zlib/fixtures/compressed_file_raw.bin +1 -0
  107. data/spec/zlib/fixtures/raw_file.txt +10 -0
  108. data/spec/zlib/zreader/checksum_spec.rb +40 -0
  109. data/spec/zlib/zreader/close_spec.rb +14 -0
  110. data/spec/zlib/zreader/compressed_size_spec.rb +18 -0
  111. data/spec/zlib/zreader/new_spec.rb +41 -0
  112. data/spec/zlib/zreader/open_spec.rb +49 -0
  113. data/spec/zlib/zreader/read_spec.rb +47 -0
  114. data/spec/zlib/zreader/rewind_spec.rb +23 -0
  115. data/spec/zlib/zreader/seek_spec.rb +55 -0
  116. data/spec/zlib/zreader/tell_spec.rb +21 -0
  117. data/spec/zlib/zreader/uncompressed_size_spec.rb +18 -0
  118. data/spec/zlib/zwriter/checksum_spec.rb +41 -0
  119. data/spec/zlib/zwriter/close_spec.rb +14 -0
  120. data/spec/zlib/zwriter/compressed_size_spec.rb +19 -0
  121. data/spec/zlib/zwriter/new_spec.rb +64 -0
  122. data/spec/zlib/zwriter/open_spec.rb +68 -0
  123. data/spec/zlib/zwriter/rewind_spec.rb +26 -0
  124. data/spec/zlib/zwriter/seek_spec.rb +54 -0
  125. data/spec/zlib/zwriter/tell_spec.rb +29 -0
  126. data/spec/zlib/zwriter/uncompressed_size_spec.rb +19 -0
  127. data/spec/zlib/zwriter/write_spec.rb +28 -0
  128. data/spec_helper.rb +49 -0
  129. metadata +296 -74
  130. data/MANIFEST +0 -27
  131. data/lib/archive/support/io.rb +0 -14
  132. data/lib/archive/support/stringio.rb +0 -22
@@ -0,0 +1,23 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+
7
+ describe "Zlib::ZReader#rewind" do
8
+ it "can rewind the stream when the delegate responds to rewind" do
9
+ ZlibSpecs.compressed_data do |cd|
10
+ Zlib::ZReader.open(cd) do |zr|
11
+ zr.read(4)
12
+ lambda { zr.rewind }.should_not raise_error
13
+ zr.read.should == ZlibSpecs.test_data
14
+ end
15
+ end
16
+ end
17
+
18
+ it "raises Errno::EINVAL when attempting to rewind the stream when the delegate does not respond to rewind" do
19
+ Zlib::ZReader.open(Object.new) do |zr|
20
+ lambda { zr.rewind }.should raise_error(Errno::EINVAL)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,55 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+
7
+ describe "Zlib::ZReader#seek" do
8
+ it "can seek to the beginning of the stream when the delegate responds to rewind" do
9
+ ZlibSpecs.compressed_data do |cd|
10
+ Zlib::ZReader.open(cd) do |zr|
11
+ zr.read(4)
12
+ lambda { zr.seek(0) }.should_not raise_error
13
+ end
14
+ end
15
+ end
16
+
17
+ it "raises Errno::EINVAL when attempting to seek to the beginning of the stream when the delegate does not respond to rewind" do
18
+ Zlib::ZReader.open(Object.new) do |zr|
19
+ lambda { zr.seek(0) }.should raise_error(Errno::EINVAL)
20
+ end
21
+ end
22
+
23
+ it "raises Errno::EINVAL when seeking forward or backward from the current position of the stream" do
24
+ ZlibSpecs.compressed_data do |cd|
25
+ Zlib::ZReader.open(cd) do |zr|
26
+ # Disable read buffering to avoid some seeking optimizations implemented
27
+ # by IO::Like which allow seeking forward within the buffer.
28
+ zr.fill_size = 0
29
+
30
+ zr.read(4)
31
+ lambda { zr.seek(1, IO::SEEK_CUR) }.should raise_error(Errno::EINVAL)
32
+ lambda { zr.seek(-1, IO::SEEK_CUR) }.should raise_error(Errno::EINVAL)
33
+ end
34
+ end
35
+ end
36
+
37
+ it "raises Errno::EINVAL when seeking a non-zero offset relative to the beginning of the stream" do
38
+ ZlibSpecs.compressed_data do |cd|
39
+ Zlib::ZReader.open(cd) do |zr|
40
+ lambda { zr.seek(-1, IO::SEEK_SET) }.should raise_error(Errno::EINVAL)
41
+ lambda { zr.seek(1, IO::SEEK_SET) }.should raise_error(Errno::EINVAL)
42
+ end
43
+ end
44
+ end
45
+
46
+ it "raises Errno::EINVAL when seeking relative to the end of the stream" do
47
+ ZlibSpecs.compressed_data do |cd|
48
+ Zlib::ZReader.open(cd) do |zr|
49
+ lambda { zr.seek(0, IO::SEEK_END) }.should raise_error(Errno::EINVAL)
50
+ lambda { zr.seek(-1, IO::SEEK_END) }.should raise_error(Errno::EINVAL)
51
+ lambda { zr.seek(1, IO::SEEK_END) }.should raise_error(Errno::EINVAL)
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+
7
+ describe "Zlib::ZReader#tell" do
8
+ it "returns the current position of the stream" do
9
+ ZlibSpecs.compressed_data do |cd|
10
+ Zlib::ZReader.open(cd) do |zr|
11
+ zr.tell.should == 0
12
+ zr.read(4)
13
+ zr.tell.should == 4
14
+ zr.read
15
+ zr.tell.should == 235
16
+ zr.rewind
17
+ zr.tell.should == 0
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+
7
+ describe "Zlib::ZReader#uncompressed_size" do
8
+ it "returns the number of bytes of uncompressed data" do
9
+ closed_zr = ZlibSpecs.compressed_data_raw do |compressed_data|
10
+ Zlib::ZReader.open(compressed_data, -15) do |zr|
11
+ zr.read
12
+ zr.uncompressed_size.should == 235
13
+ zr
14
+ end
15
+ end
16
+ closed_zr.uncompressed_size.should == 235
17
+ end
18
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter#checksum" do
9
+ it "computes the ADLER32 checksum of zlib formatted data" do
10
+ compressed_data = BinaryStringIO.new
11
+ closed_zw = Zlib::ZWriter.open(compressed_data, nil, 15) do |zw|
12
+ zw.write(ZlibSpecs.test_data)
13
+ zw.flush
14
+ zw.checksum.should == Zlib.adler32(ZlibSpecs.test_data)
15
+ zw
16
+ end
17
+ closed_zw.checksum.should == Zlib.adler32(ZlibSpecs.test_data)
18
+ end
19
+
20
+ it "computes the CRC32 checksum of gzip formatted data" do
21
+ compressed_data = BinaryStringIO.new
22
+ closed_zw = Zlib::ZWriter.open(compressed_data, nil, 31) do |zw|
23
+ zw.write(ZlibSpecs.test_data)
24
+ zw.flush
25
+ zw.checksum.should == Zlib.crc32(ZlibSpecs.test_data)
26
+ zw
27
+ end
28
+ closed_zw.checksum.should == Zlib.crc32(ZlibSpecs.test_data)
29
+ end
30
+
31
+ it "does not compute a checksum for raw zlib data" do
32
+ compressed_data = BinaryStringIO.new
33
+ closed_zw = Zlib::ZWriter.open(compressed_data, nil, -15) do |zw|
34
+ zw.write(ZlibSpecs.test_data)
35
+ zw.flush
36
+ zw.checksum.should == 1
37
+ zw
38
+ end
39
+ closed_zw.checksum.should == 1
40
+ end
41
+ end
@@ -0,0 +1,14 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter.close" do
9
+ it "closes the stream" do
10
+ zw = Zlib::ZWriter.new(BinaryStringIO.new)
11
+ zw.close
12
+ zw.closed?.should be_true
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter#compressed_size" do
9
+ it "returns the number of bytes of compressed data" do
10
+ compressed_data = BinaryStringIO.new
11
+ closed_zw = Zlib::ZWriter.open(compressed_data, nil, -15) do |zw|
12
+ zw.sync = true
13
+ zw.write(ZlibSpecs.test_data)
14
+ zw.compressed_size.should >= 0
15
+ zw
16
+ end
17
+ closed_zw.compressed_size.should == 160
18
+ end
19
+ end
@@ -0,0 +1,64 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter.new" do
9
+ it "returns a new instance" do
10
+ zw = Zlib::ZWriter.new(BinaryStringIO.new)
11
+ zw.class.should == Zlib::ZWriter
12
+ zw.close
13
+ end
14
+
15
+ it "provides default settings for level, window_bits, mem_level, and strategy" do
16
+ data = ZlibSpecs.test_data
17
+ compressed_data = BinaryStringIO.new
18
+ zw = Zlib::ZWriter.new(compressed_data)
19
+ zw.write(data)
20
+ zw.close
21
+
22
+ compressed_data.string.should == ZlibSpecs.compressed_data
23
+ end
24
+
25
+ it "allows level to be set" do
26
+ data = ZlibSpecs.test_data
27
+ compressed_data = BinaryStringIO.new
28
+ zw = Zlib::ZWriter.new(compressed_data, Zlib::NO_COMPRESSION)
29
+ zw.write(data)
30
+ zw.close
31
+
32
+ compressed_data.string.should == ZlibSpecs.compressed_data_nocomp
33
+ end
34
+
35
+ it "allows window_bits to be set" do
36
+ data = ZlibSpecs.test_data
37
+ compressed_data = BinaryStringIO.new
38
+ zw = Zlib::ZWriter.new(compressed_data, nil, 8)
39
+ zw.write(data)
40
+ zw.close
41
+
42
+ compressed_data.string.should == ZlibSpecs.compressed_data_minwin
43
+ end
44
+
45
+ it "allows mem_level to be set" do
46
+ data = ZlibSpecs.test_data
47
+ compressed_data = BinaryStringIO.new
48
+ zw = Zlib::ZWriter.new(compressed_data, nil, nil, 1)
49
+ zw.write(data)
50
+ zw.close
51
+
52
+ compressed_data.string.should == ZlibSpecs.compressed_data_minmem
53
+ end
54
+
55
+ it "allows strategy to be set" do
56
+ data = ZlibSpecs.test_data
57
+ compressed_data = BinaryStringIO.new
58
+ zw = Zlib::ZWriter.new(compressed_data, nil, nil, nil, Zlib::HUFFMAN_ONLY)
59
+ zw.write(data)
60
+ zw.close
61
+
62
+ compressed_data.string.should == ZlibSpecs.compressed_data_huffman
63
+ end
64
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter.open" do
9
+ it "returns a new instance when run without a block" do
10
+ zw = Zlib::ZWriter.open(BinaryStringIO.new)
11
+ zw.class.should == Zlib::ZWriter
12
+ zw.close
13
+ end
14
+
15
+ it "executes a block with a new instance as an argument" do
16
+ Zlib::ZWriter.open(BinaryStringIO.new) { |zr| zr.class.should == Zlib::ZWriter }
17
+ end
18
+
19
+ it "closes the object after executing a block" do
20
+ Zlib::ZWriter.open(BinaryStringIO.new) { |zr| zr }.closed?.should.be_true
21
+ end
22
+
23
+ it "provides default settings for level, window_bits, mem_level, and strategy" do
24
+ data = ZlibSpecs.test_data
25
+ compressed_data = BinaryStringIO.new
26
+ Zlib::ZWriter.open(compressed_data) { |zw| zw.write(data) }
27
+
28
+ compressed_data.string.should == ZlibSpecs.compressed_data
29
+ end
30
+
31
+ it "allows level to be set" do
32
+ data = ZlibSpecs.test_data
33
+ compressed_data = BinaryStringIO.new
34
+ Zlib::ZWriter.open(compressed_data, Zlib::NO_COMPRESSION) do |zw|
35
+ zw.write(data)
36
+ end
37
+
38
+ compressed_data.string.should == ZlibSpecs.compressed_data_nocomp
39
+ end
40
+
41
+ it "allows window_bits to be set" do
42
+ data = ZlibSpecs.test_data
43
+ compressed_data = BinaryStringIO.new
44
+ Zlib::ZWriter.open(compressed_data, nil, 8) { |zw| zw.write(data) }
45
+
46
+ compressed_data.string.should == ZlibSpecs.compressed_data_minwin
47
+ end
48
+
49
+ it "allows mem_level to be set" do
50
+ data = ZlibSpecs.test_data
51
+ compressed_data = BinaryStringIO.new
52
+ Zlib::ZWriter.open(compressed_data, nil, nil, 1) { |zw| zw.write(data) }
53
+
54
+ compressed_data.string.should == ZlibSpecs.compressed_data_minmem
55
+ end
56
+
57
+ it "allows strategy to be set" do
58
+ data = ZlibSpecs.test_data
59
+ compressed_data = BinaryStringIO.new
60
+ Zlib::ZWriter.open(
61
+ compressed_data, nil, nil, nil, Zlib::HUFFMAN_ONLY
62
+ ) do |zw|
63
+ zw.write(data)
64
+ end
65
+
66
+ compressed_data.string.should == ZlibSpecs.compressed_data_huffman
67
+ end
68
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter#rewind" do
9
+ it "can rewind the stream when the delegate responds to rewind" do
10
+ sio = BinaryStringIO.new
11
+ Zlib::ZWriter.open(sio) do |zw|
12
+ zw.write('test')
13
+ lambda { zw.rewind }.should_not raise_error
14
+ zw.write(ZlibSpecs.test_data)
15
+ end
16
+ sio.string.should == ZlibSpecs.compressed_data
17
+ end
18
+
19
+ it "raises Errno::EINVAL when attempting to rewind the stream when the delegate does not respond to rewind" do
20
+ delegate = mock('delegate')
21
+ delegate.should_receive(:write).at_least(:once).and_return(1)
22
+ Zlib::ZWriter.open(delegate) do |zw|
23
+ lambda { zw.rewind }.should raise_error(Errno::EINVAL)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter#seek" do
9
+ it "can seek to the beginning of the stream when the delegate responds to rewind" do
10
+ compressed_data = BinaryStringIO.new
11
+ Zlib::ZWriter.open(compressed_data) do |c|
12
+ c.write('test')
13
+ lambda { c.seek(0) }.should_not raise_error
14
+ end
15
+ end
16
+
17
+ it "raises Errno::EINVAL when attempting to seek to the beginning of the stream when the delegate does not respond to rewind" do
18
+ delegate = mock('delegate')
19
+ # RSpec's mocking facility supposedly supports this, but MSpec's does not as
20
+ # of version 1.5.10.
21
+ #delegate.should_receive(:write).with(an_instance_of(String)).at_least(:once).and_return { |s| s.length }
22
+ # Use the following instead for now.
23
+ delegate.should_receive(:write).at_least(:once).and_return(1)
24
+ Zlib::ZWriter.open(delegate) do |c|
25
+ lambda { c.seek(0) }.should raise_error(Errno::EINVAL)
26
+ end
27
+ end
28
+
29
+ it "raises Errno::EINVAL when seeking forward or backward from the current position of the stream" do
30
+ compressed_data = BinaryStringIO.new
31
+ Zlib::ZWriter.open(compressed_data) do |c|
32
+ c.write('test')
33
+ lambda { c.seek(1, IO::SEEK_CUR) }.should raise_error(Errno::EINVAL)
34
+ lambda { c.seek(-1, IO::SEEK_CUR) }.should raise_error(Errno::EINVAL)
35
+ end
36
+ end
37
+
38
+ it "raises Errno::EINVAL when seeking a non-zero offset relative to the beginning of the stream" do
39
+ compressed_data = BinaryStringIO.new
40
+ Zlib::ZWriter.open(compressed_data) do |c|
41
+ lambda { c.seek(-1, IO::SEEK_SET) }.should raise_error(Errno::EINVAL)
42
+ lambda { c.seek(1, IO::SEEK_SET) }.should raise_error(Errno::EINVAL)
43
+ end
44
+ end
45
+
46
+ it "raises Errno::EINVAL when seeking relative to the end of the stream" do
47
+ compressed_data = BinaryStringIO.new
48
+ Zlib::ZWriter.open(compressed_data) do |c|
49
+ lambda { c.seek(0, IO::SEEK_END) }.should raise_error(Errno::EINVAL)
50
+ lambda { c.seek(-1, IO::SEEK_END) }.should raise_error(Errno::EINVAL)
51
+ lambda { c.seek(1, IO::SEEK_END) }.should raise_error(Errno::EINVAL)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: UTF-8
2
+
3
+ require File.dirname(__FILE__) + '/../../../spec_helper'
4
+ require File.dirname(__FILE__) + '/../fixtures/classes'
5
+ require 'archive/support/zlib'
6
+ require 'archive/support/binary_stringio'
7
+
8
+ describe "Zlib::ZWriter#tell" do
9
+ it "returns the current position of the stream" do
10
+ sio = BinaryStringIO.new
11
+ Zlib::ZWriter.open(sio) do |zw|
12
+ zw.tell.should == 0
13
+ zw.write('test1')
14
+ zw.tell.should == 5
15
+ zw.write('test2')
16
+ zw.tell.should == 10
17
+ zw.rewind
18
+ zw.tell.should == 0
19
+ end
20
+ end
21
+
22
+ it "raises IOError on closed stream" do
23
+ delegate = mock('delegate')
24
+ delegate.should_receive(:write).at_least(:once).and_return(1)
25
+ lambda do
26
+ Zlib::ZWriter.open(delegate) { |zw| zw }.tell
27
+ end.should raise_error(IOError)
28
+ end
29
+ end