archive-zip 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HACKING +25 -42
- data/NEWS +25 -0
- data/README +2 -2
- data/Rakefile +202 -0
- data/TODO +5 -0
- data/default.mspec +8 -0
- data/lib/archive/support/binary_stringio.rb +23 -0
- data/lib/archive/support/integer.rb +13 -0
- data/lib/archive/support/io-like.rb +3 -1
- data/lib/archive/support/ioextensions.rb +16 -0
- data/lib/archive/support/iowindow.rb +10 -18
- data/lib/archive/support/time.rb +2 -0
- data/lib/archive/support/zlib.rb +298 -71
- data/lib/archive/zip.rb +161 -139
- data/lib/archive/zip/codec.rb +2 -0
- data/lib/archive/zip/codec/deflate.rb +59 -11
- data/lib/archive/zip/codec/null_encryption.rb +75 -14
- data/lib/archive/zip/codec/store.rb +75 -26
- data/lib/archive/zip/codec/traditional_encryption.rb +146 -35
- data/lib/archive/zip/data_descriptor.rb +6 -4
- data/lib/archive/zip/entry.rb +184 -132
- data/lib/archive/zip/error.rb +2 -0
- data/lib/archive/zip/extra_field.rb +20 -6
- data/lib/archive/zip/extra_field/extended_timestamp.rb +141 -60
- data/lib/archive/zip/extra_field/raw.rb +70 -12
- data/lib/archive/zip/extra_field/unix.rb +58 -16
- data/lib/archive/zip/version.rb +6 -0
- data/spec/archive/zip/codec/deflate/compress/checksum_spec.rb +42 -0
- data/spec/archive/zip/codec/deflate/compress/close_spec.rb +44 -0
- data/spec/archive/zip/codec/deflate/compress/crc32_spec.rb +21 -0
- data/spec/archive/zip/codec/deflate/compress/data_descriptor_spec.rb +67 -0
- data/spec/archive/zip/codec/deflate/compress/new_spec.rb +37 -0
- data/spec/archive/zip/codec/deflate/compress/open_spec.rb +46 -0
- data/spec/archive/zip/codec/deflate/compress/write_spec.rb +109 -0
- data/spec/archive/zip/codec/deflate/decompress/checksum_spec.rb +18 -0
- data/spec/archive/zip/codec/deflate/decompress/close_spec.rb +33 -0
- data/spec/archive/zip/codec/deflate/decompress/crc32_spec.rb +18 -0
- data/spec/archive/zip/codec/deflate/decompress/data_descriptor_spec.rb +67 -0
- data/spec/archive/zip/codec/deflate/decompress/new_spec.rb +14 -0
- data/spec/archive/zip/codec/deflate/decompress/open_spec.rb +27 -0
- data/spec/archive/zip/codec/deflate/fixtures/classes.rb +25 -0
- data/spec/archive/zip/codec/deflate/fixtures/compressed_file.bin +1 -0
- data/spec/archive/zip/codec/deflate/fixtures/compressed_file_nocomp.bin +0 -0
- data/spec/archive/zip/codec/deflate/fixtures/raw_file.txt +10 -0
- data/spec/archive/zip/codec/null_encryption/decrypt/close_spec.rb +33 -0
- data/spec/archive/zip/codec/null_encryption/decrypt/new_spec.rb +14 -0
- data/spec/archive/zip/codec/null_encryption/decrypt/open_spec.rb +27 -0
- data/spec/archive/zip/codec/null_encryption/decrypt/read_spec.rb +24 -0
- data/spec/archive/zip/codec/null_encryption/decrypt/rewind_spec.rb +25 -0
- data/spec/archive/zip/codec/null_encryption/decrypt/seek_spec.rb +57 -0
- data/spec/archive/zip/codec/null_encryption/decrypt/tell_spec.rb +21 -0
- data/spec/archive/zip/codec/null_encryption/encrypt/close_spec.rb +33 -0
- data/spec/archive/zip/codec/null_encryption/encrypt/new_spec.rb +14 -0
- data/spec/archive/zip/codec/null_encryption/encrypt/open_spec.rb +27 -0
- data/spec/archive/zip/codec/null_encryption/encrypt/rewind_spec.rb +26 -0
- data/spec/archive/zip/codec/null_encryption/encrypt/seek_spec.rb +50 -0
- data/spec/archive/zip/codec/null_encryption/encrypt/tell_spec.rb +29 -0
- data/spec/archive/zip/codec/null_encryption/encrypt/write_spec.rb +29 -0
- data/spec/archive/zip/codec/null_encryption/fixtures/classes.rb +12 -0
- data/spec/archive/zip/codec/null_encryption/fixtures/raw_file.txt +10 -0
- data/spec/archive/zip/codec/store/compress/close_spec.rb +33 -0
- data/spec/archive/zip/codec/store/compress/data_descriptor_spec.rb +68 -0
- data/spec/archive/zip/codec/store/compress/new_spec.rb +14 -0
- data/spec/archive/zip/codec/store/compress/open_spec.rb +27 -0
- data/spec/archive/zip/codec/store/compress/rewind_spec.rb +26 -0
- data/spec/archive/zip/codec/store/compress/seek_spec.rb +50 -0
- data/spec/archive/zip/codec/store/compress/tell_spec.rb +29 -0
- data/spec/archive/zip/codec/store/compress/write_spec.rb +29 -0
- data/spec/archive/zip/codec/store/decompress/close_spec.rb +33 -0
- data/spec/archive/zip/codec/store/decompress/data_descriptor_spec.rb +68 -0
- data/spec/archive/zip/codec/store/decompress/new_spec.rb +14 -0
- data/spec/archive/zip/codec/store/decompress/open_spec.rb +27 -0
- data/spec/archive/zip/codec/store/decompress/read_spec.rb +24 -0
- data/spec/archive/zip/codec/store/decompress/rewind_spec.rb +25 -0
- data/spec/archive/zip/codec/store/decompress/seek_spec.rb +57 -0
- data/spec/archive/zip/codec/store/decompress/tell_spec.rb +21 -0
- data/spec/archive/zip/codec/store/fixtures/classes.rb +12 -0
- data/spec/archive/zip/codec/store/fixtures/raw_file.txt +10 -0
- data/spec/archive/zip/codec/traditional_encryption/decrypt/close_spec.rb +64 -0
- data/spec/archive/zip/codec/traditional_encryption/decrypt/new_spec.rb +18 -0
- data/spec/archive/zip/codec/traditional_encryption/decrypt/open_spec.rb +39 -0
- data/spec/archive/zip/codec/traditional_encryption/decrypt/read_spec.rb +126 -0
- data/spec/archive/zip/codec/traditional_encryption/decrypt/rewind_spec.rb +38 -0
- data/spec/archive/zip/codec/traditional_encryption/decrypt/seek_spec.rb +82 -0
- data/spec/archive/zip/codec/traditional_encryption/decrypt/tell_spec.rb +25 -0
- data/spec/archive/zip/codec/traditional_encryption/encrypt/close_spec.rb +64 -0
- data/spec/archive/zip/codec/traditional_encryption/encrypt/new_spec.rb +18 -0
- data/spec/archive/zip/codec/traditional_encryption/encrypt/open_spec.rb +39 -0
- data/spec/archive/zip/codec/traditional_encryption/encrypt/rewind_spec.rb +41 -0
- data/spec/archive/zip/codec/traditional_encryption/encrypt/seek_spec.rb +75 -0
- data/spec/archive/zip/codec/traditional_encryption/encrypt/tell_spec.rb +42 -0
- data/spec/archive/zip/codec/traditional_encryption/encrypt/write_spec.rb +127 -0
- data/spec/archive/zip/codec/traditional_encryption/fixtures/classes.rb +27 -0
- data/spec/archive/zip/codec/traditional_encryption/fixtures/encrypted_file.bin +0 -0
- data/spec/archive/zip/codec/traditional_encryption/fixtures/raw_file.txt +10 -0
- data/spec/binary_stringio/new_spec.rb +34 -0
- data/spec/binary_stringio/set_encoding_spec.rb +14 -0
- data/spec/ioextensions/read_exactly_spec.rb +50 -0
- data/spec/zlib/fixtures/classes.rb +65 -0
- data/spec/zlib/fixtures/compressed_file.bin +1 -0
- data/spec/zlib/fixtures/compressed_file_gzip.bin +0 -0
- data/spec/zlib/fixtures/compressed_file_huffman.bin +2 -0
- data/spec/zlib/fixtures/compressed_file_minmem.bin +0 -0
- data/spec/zlib/fixtures/compressed_file_minwin.bin +1 -0
- data/spec/zlib/fixtures/compressed_file_nocomp.bin +0 -0
- data/spec/zlib/fixtures/compressed_file_raw.bin +1 -0
- data/spec/zlib/fixtures/raw_file.txt +10 -0
- data/spec/zlib/zreader/checksum_spec.rb +40 -0
- data/spec/zlib/zreader/close_spec.rb +14 -0
- data/spec/zlib/zreader/compressed_size_spec.rb +18 -0
- data/spec/zlib/zreader/new_spec.rb +41 -0
- data/spec/zlib/zreader/open_spec.rb +49 -0
- data/spec/zlib/zreader/read_spec.rb +47 -0
- data/spec/zlib/zreader/rewind_spec.rb +23 -0
- data/spec/zlib/zreader/seek_spec.rb +55 -0
- data/spec/zlib/zreader/tell_spec.rb +21 -0
- data/spec/zlib/zreader/uncompressed_size_spec.rb +18 -0
- data/spec/zlib/zwriter/checksum_spec.rb +41 -0
- data/spec/zlib/zwriter/close_spec.rb +14 -0
- data/spec/zlib/zwriter/compressed_size_spec.rb +19 -0
- data/spec/zlib/zwriter/new_spec.rb +64 -0
- data/spec/zlib/zwriter/open_spec.rb +68 -0
- data/spec/zlib/zwriter/rewind_spec.rb +26 -0
- data/spec/zlib/zwriter/seek_spec.rb +54 -0
- data/spec/zlib/zwriter/tell_spec.rb +29 -0
- data/spec/zlib/zwriter/uncompressed_size_spec.rb +19 -0
- data/spec/zlib/zwriter/write_spec.rb +28 -0
- data/spec_helper.rb +49 -0
- metadata +296 -74
- data/MANIFEST +0 -27
- data/lib/archive/support/io.rb +0 -14
- data/lib/archive/support/stringio.rb +0 -22
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class TraditionalEncryptionSpecs
|
4
|
+
class << self
|
5
|
+
def password
|
6
|
+
'p455w0rd'
|
7
|
+
end
|
8
|
+
|
9
|
+
def mtime
|
10
|
+
Time.at(0)
|
11
|
+
end
|
12
|
+
|
13
|
+
def encrypted_data
|
14
|
+
File.open(
|
15
|
+
File.join(File.dirname(__FILE__), 'encrypted_file.bin'), 'rb'
|
16
|
+
) do |f|
|
17
|
+
block_given? ? yield(f) : f.read
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_data
|
22
|
+
File.open(File.join(File.dirname(__FILE__), 'raw_file.txt'), 'rb') do |f|
|
23
|
+
block_given? ? yield(f) : f.read
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
4
|
+
require 'archive/support/binary_stringio'
|
5
|
+
|
6
|
+
describe "BinaryStringIO.new" do
|
7
|
+
it "returns a new instance" do
|
8
|
+
io = BinaryStringIO.new
|
9
|
+
io.class.should == BinaryStringIO
|
10
|
+
io.close
|
11
|
+
end
|
12
|
+
|
13
|
+
it "creates a decendent of StringIO" do
|
14
|
+
io = BinaryStringIO.new
|
15
|
+
io.should be_kind_of StringIO
|
16
|
+
io.close
|
17
|
+
end
|
18
|
+
|
19
|
+
it "takes the same arguments as StringIO.new" do
|
20
|
+
lambda { BinaryStringIO.new }.should_not raise_error(ArgumentError)
|
21
|
+
lambda { BinaryStringIO.new('') }.should_not raise_error(ArgumentError)
|
22
|
+
lambda { BinaryStringIO.new('', 'r') }.should_not raise_error(ArgumentError)
|
23
|
+
lambda { BinaryStringIO.new('', 'w') }.should_not raise_error(ArgumentError)
|
24
|
+
|
25
|
+
lambda { BinaryStringIO.new('', 'w', 42) }.should raise_error(ArgumentError)
|
26
|
+
end
|
27
|
+
|
28
|
+
with_feature :encoding do
|
29
|
+
it "sets the external encoding to binary" do
|
30
|
+
io = BinaryStringIO.new
|
31
|
+
io.external_encoding.should == Encoding::ASCII_8BIT
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
4
|
+
require 'archive/support/binary_stringio'
|
5
|
+
|
6
|
+
with_feature :encoding do
|
7
|
+
describe "BinaryStringIO#set_encoding" do
|
8
|
+
it "should be private" do
|
9
|
+
lambda do
|
10
|
+
BinaryStringIO.new.set_encoding('utf-8')
|
11
|
+
end.should raise_error(NoMethodError)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../../spec_helper'
|
4
|
+
require 'archive/support/ioextensions.rb'
|
5
|
+
require 'archive/support/binary_stringio'
|
6
|
+
|
7
|
+
describe "IOExtensions.read_exactly" do
|
8
|
+
it "reads and returns length bytes from a given IO object" do
|
9
|
+
io = BinaryStringIO.new('This is test data')
|
10
|
+
IOExtensions.read_exactly(io, 4).should == 'This'
|
11
|
+
IOExtensions.read_exactly(io, 13).should == ' is test data'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "raises an error when too little data is available" do
|
15
|
+
io = BinaryStringIO.new('This is test data')
|
16
|
+
lambda do
|
17
|
+
IOExtensions.read_exactly(io, 18)
|
18
|
+
end.should raise_error(EOFError)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "takes an optional buffer argument and fills it" do
|
22
|
+
io = BinaryStringIO.new('This is test data')
|
23
|
+
buffer = ''
|
24
|
+
IOExtensions.read_exactly(io, 4, buffer)
|
25
|
+
buffer.should == 'This'
|
26
|
+
buffer = ''
|
27
|
+
IOExtensions.read_exactly(io, 13, buffer)
|
28
|
+
buffer.should == ' is test data'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "empties the optional buffer before filling it" do
|
32
|
+
io = BinaryStringIO.new('This is test data')
|
33
|
+
buffer = ''
|
34
|
+
IOExtensions.read_exactly(io, 4, buffer)
|
35
|
+
buffer.should == 'This'
|
36
|
+
IOExtensions.read_exactly(io, 13, buffer)
|
37
|
+
buffer.should == ' is test data'
|
38
|
+
end
|
39
|
+
|
40
|
+
it "can read 0 bytes" do
|
41
|
+
io = BinaryStringIO.new('This is test data')
|
42
|
+
IOExtensions.read_exactly(io, 0).should == ''
|
43
|
+
end
|
44
|
+
|
45
|
+
it "retries partial reads" do
|
46
|
+
io = mock('io')
|
47
|
+
io.should_receive(:read).exactly(2).and_return('hello')
|
48
|
+
IOExtensions.read_exactly(io, 10).should == 'hellohello'
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
class ZlibSpecs
|
4
|
+
def self.compressed_data
|
5
|
+
File.open(
|
6
|
+
File.join(File.dirname(__FILE__), 'compressed_file.bin'), 'rb'
|
7
|
+
) do |f|
|
8
|
+
block_given? ? yield(f) : f.read
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.compressed_data_nocomp
|
13
|
+
File.open(
|
14
|
+
File.join(File.dirname(__FILE__), 'compressed_file_nocomp.bin'), 'rb'
|
15
|
+
) do |f|
|
16
|
+
block_given? ? yield(f) : f.read
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.compressed_data_minwin
|
21
|
+
File.open(
|
22
|
+
File.join(File.dirname(__FILE__), 'compressed_file_minwin.bin'), 'rb'
|
23
|
+
) do |f|
|
24
|
+
block_given? ? yield(f) : f.read
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.compressed_data_minmem
|
29
|
+
File.open(
|
30
|
+
File.join(File.dirname(__FILE__), 'compressed_file_minmem.bin'), 'rb'
|
31
|
+
) do |f|
|
32
|
+
block_given? ? yield(f) : f.read
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.compressed_data_huffman
|
37
|
+
File.open(
|
38
|
+
File.join(File.dirname(__FILE__), 'compressed_file_huffman.bin'), 'rb'
|
39
|
+
) do |f|
|
40
|
+
block_given? ? yield(f) : f.read
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.compressed_data_gzip
|
45
|
+
File.open(
|
46
|
+
File.join(File.dirname(__FILE__), 'compressed_file_gzip.bin'), 'rb'
|
47
|
+
) do |f|
|
48
|
+
block_given? ? yield(f) : f.read
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.compressed_data_raw
|
53
|
+
File.open(
|
54
|
+
File.join(File.dirname(__FILE__), 'compressed_file_raw.bin'), 'rb'
|
55
|
+
) do |f|
|
56
|
+
block_given? ? yield(f) : f.read
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.test_data
|
61
|
+
File.open(File.join(File.dirname(__FILE__), 'raw_file.txt'), 'rb') do |f|
|
62
|
+
f.read
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
x�%�A�0E�9�?A��\�kj���E5��K���o��)+�S.��-� �����Z����J[0��u~*����wC���D�m%���c�$Q�T����-ecx���pX"�m[K\ٝd�Ƌ����/n�Ɂr)>e����=��`��T��CQ�
|
Binary file
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
�%�A�0E�9�?A��\�kj���E5��K���o��)+�S.��-� �����Z����J[0��u~*����wC���D�m%���c�$Q�T����-ecx���pX"�m[K\ٝd�Ƌ����/n�Ɂr)>e����=��`��T��CQ�
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
%�A�0E�9�?A��\�kj���E5��K���o��)+�S.��-� �����Z����J[0��u~*����wC���D�m%���c�$Q�T����-ecx���pX"�m[K\ٝd�Ƌ����/n�Ɂr)>e����=��`��T�
|
@@ -0,0 +1,40 @@
|
|
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#checksum" do
|
8
|
+
it "computes the ADLER32 checksum of zlib formatted data" do
|
9
|
+
closed_zr = ZlibSpecs.compressed_data do |f|
|
10
|
+
Zlib::ZReader.open(f, 15) do |zr|
|
11
|
+
zr.read
|
12
|
+
zr.checksum.should == Zlib.adler32(ZlibSpecs.test_data)
|
13
|
+
zr
|
14
|
+
end
|
15
|
+
end
|
16
|
+
closed_zr.checksum.should == Zlib.adler32(ZlibSpecs.test_data)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "computes the CRC32 checksum of gzip formatted data" do
|
20
|
+
closed_zr = ZlibSpecs.compressed_data_gzip do |f|
|
21
|
+
Zlib::ZReader.open(f, 31) do |zr|
|
22
|
+
zr.read
|
23
|
+
zr.checksum.should == Zlib.crc32(ZlibSpecs.test_data)
|
24
|
+
zr
|
25
|
+
end
|
26
|
+
end
|
27
|
+
closed_zr.checksum.should == Zlib.crc32(ZlibSpecs.test_data)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "does not compute a checksum for raw zlib data" do
|
31
|
+
closed_zr = ZlibSpecs.compressed_data_raw do |f|
|
32
|
+
Zlib::ZReader.open(f, -15) do |zr|
|
33
|
+
zr.read
|
34
|
+
zr.checksum.should == 1
|
35
|
+
zr
|
36
|
+
end
|
37
|
+
end
|
38
|
+
closed_zr.checksum.should == 1
|
39
|
+
end
|
40
|
+
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::ZReader.close" do
|
9
|
+
it "closes the stream" do
|
10
|
+
zr = Zlib::ZReader.new(BinaryStringIO.new)
|
11
|
+
zr.close
|
12
|
+
zr.closed?.should be_true
|
13
|
+
end
|
14
|
+
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#compressed_size" do
|
8
|
+
it "returns the number of bytes of compressed 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.compressed_size.should == 160
|
13
|
+
zr
|
14
|
+
end
|
15
|
+
end
|
16
|
+
closed_zr.compressed_size.should == 160
|
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::ZReader.new" do
|
9
|
+
it "returns a new instance" do
|
10
|
+
Zlib::ZReader.new(BinaryStringIO.new).class.should == Zlib::ZReader
|
11
|
+
end
|
12
|
+
|
13
|
+
it "does not require window_bits to be set" do
|
14
|
+
data = ZlibSpecs.test_data
|
15
|
+
compressed_data = BinaryStringIO.new
|
16
|
+
Zlib::ZWriter.open(compressed_data) do |zw|
|
17
|
+
zw.write(data)
|
18
|
+
end
|
19
|
+
compressed_data.rewind
|
20
|
+
|
21
|
+
zr = Zlib::ZReader.new(compressed_data)
|
22
|
+
zr.read.should == data
|
23
|
+
zr.close
|
24
|
+
end
|
25
|
+
|
26
|
+
it "allows window_bits to be set" do
|
27
|
+
data = ZlibSpecs.test_data
|
28
|
+
compressed_data = BinaryStringIO.new
|
29
|
+
window_bits = -Zlib::MAX_WBITS
|
30
|
+
Zlib::ZWriter.open(
|
31
|
+
compressed_data, Zlib::DEFAULT_COMPRESSION, window_bits
|
32
|
+
) do |zw|
|
33
|
+
zw.write(data)
|
34
|
+
end
|
35
|
+
compressed_data.rewind
|
36
|
+
|
37
|
+
zr = Zlib::ZReader.new(compressed_data, window_bits)
|
38
|
+
zr.read.should == data
|
39
|
+
zr.close
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,49 @@
|
|
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::ZReader.open" do
|
9
|
+
it "returns a new instance when run without a block" do
|
10
|
+
Zlib::ZReader.open(BinaryStringIO.new).class.should == Zlib::ZReader
|
11
|
+
end
|
12
|
+
|
13
|
+
it "executes a block with a new instance as an argument" do
|
14
|
+
Zlib::ZReader.open(BinaryStringIO.new) { |zr| zr.class.should == Zlib::ZReader }
|
15
|
+
end
|
16
|
+
|
17
|
+
it "closes the object after executing a block" do
|
18
|
+
Zlib::ZReader.open(BinaryStringIO.new) { |zr| zr }.closed?.should.be_true
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does not require window_bits to be set" do
|
22
|
+
data = ZlibSpecs.test_data
|
23
|
+
compressed_data = BinaryStringIO.new
|
24
|
+
Zlib::ZWriter.open(compressed_data) do |zw|
|
25
|
+
zw.write(data)
|
26
|
+
end
|
27
|
+
compressed_data.rewind
|
28
|
+
|
29
|
+
Zlib::ZReader.open(compressed_data) do |zr|
|
30
|
+
zr.read.should == data
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it "allows window_bits to be set" do
|
35
|
+
data = ZlibSpecs.test_data
|
36
|
+
compressed_data = BinaryStringIO.new
|
37
|
+
window_bits = -Zlib::MAX_WBITS
|
38
|
+
Zlib::ZWriter.open(
|
39
|
+
compressed_data, Zlib::DEFAULT_COMPRESSION, window_bits
|
40
|
+
) do |zw|
|
41
|
+
zw.write(data)
|
42
|
+
end
|
43
|
+
compressed_data.rewind
|
44
|
+
|
45
|
+
Zlib::ZReader.open(compressed_data, window_bits) do |zr|
|
46
|
+
zr.read.should == data
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,47 @@
|
|
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::ZReader#read" do
|
9
|
+
it "calls the read method of the delegate" do
|
10
|
+
delegate = mock('delegate')
|
11
|
+
delegate.should_receive(:read).and_return(nil)
|
12
|
+
Zlib::ZReader.open(delegate) do |zr|
|
13
|
+
# Capture and ignore the Zlib::BufError which is generated due to mocking.
|
14
|
+
begin
|
15
|
+
zr.read
|
16
|
+
rescue Zlib::BufError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "decompresses compressed data" do
|
22
|
+
ZlibSpecs.compressed_data do |cd|
|
23
|
+
Zlib::ZReader.open(cd) do |zr|
|
24
|
+
zr.read.should == ZlibSpecs.test_data
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
it "raises Zlib::DataError when reading invalid data" do
|
30
|
+
Zlib::ZReader.open(BinaryStringIO.new('This is not compressed data')) do |zr|
|
31
|
+
lambda { zr.read }.should raise_error(Zlib::DataError)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
it "raises Zlib::BufError when reading truncated data" do
|
36
|
+
truncated_data = ZlibSpecs.compressed_data { |cd| cd.read(100) }
|
37
|
+
Zlib::ZReader.open(BinaryStringIO.new(truncated_data)) do |zr|
|
38
|
+
lambda { zr.read }.should raise_error(Zlib::BufError)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
it "raises Zlib::BufError when reading empty data" do
|
43
|
+
Zlib::ZReader.open(BinaryStringIO.new()) do |zr|
|
44
|
+
lambda { zr.read }.should raise_error(Zlib::BufError)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|