archive-zip 0.11.0 → 0.13.0.pre1

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 (133) hide show
  1. checksums.yaml +5 -5
  2. data/LICENSE +1 -1
  3. data/NEWS.md +12 -0
  4. data/README.md +2 -1
  5. data/lib/archive/support/ioextensions.rb +5 -7
  6. data/lib/archive/support/iowindow.rb +34 -87
  7. data/lib/archive/support/stringio.rb +27 -0
  8. data/lib/archive/support/zlib.rb +0 -435
  9. data/lib/archive/zip/codec/deflate/reader.rb +187 -0
  10. data/lib/archive/zip/codec/deflate/writer.rb +209 -0
  11. data/lib/archive/zip/codec/deflate.rb +92 -244
  12. data/lib/archive/zip/codec/null_encryption.rb +4 -184
  13. data/lib/archive/zip/codec/store/reader.rb +97 -0
  14. data/lib/archive/zip/codec/store/writer.rb +78 -0
  15. data/lib/archive/zip/codec/store.rb +6 -231
  16. data/lib/archive/zip/codec/traditional_encryption/base.rb +85 -0
  17. data/lib/archive/zip/codec/traditional_encryption/reader.rb +65 -0
  18. data/lib/archive/zip/codec/traditional_encryption/writer.rb +71 -0
  19. data/lib/archive/zip/codec/traditional_encryption.rb +6 -360
  20. data/lib/archive/zip/codec.rb +2 -2
  21. data/lib/archive/zip/dos_time.rb +103 -0
  22. data/lib/archive/zip/entry.rb +86 -70
  23. data/lib/archive/zip.rb +2 -5
  24. metadata +37 -227
  25. data/.yardopts +0 -1
  26. data/Rakefile +0 -247
  27. data/lib/archive/support/binary_stringio.rb +0 -30
  28. data/lib/archive/support/integer.rb +0 -13
  29. data/lib/archive/support/io-like.rb +0 -14
  30. data/lib/archive/support/time.rb +0 -119
  31. data/lib/archive/zip/version.rb +0 -6
  32. data/spec/archive/dos_time_spec.rb +0 -113
  33. data/spec/archive/zip/archive_spec.rb +0 -54
  34. data/spec/archive/zip/codec/deflate/compress/checksum_spec.rb +0 -44
  35. data/spec/archive/zip/codec/deflate/compress/close_spec.rb +0 -45
  36. data/spec/archive/zip/codec/deflate/compress/crc32_spec.rb +0 -23
  37. data/spec/archive/zip/codec/deflate/compress/data_descriptor_spec.rb +0 -74
  38. data/spec/archive/zip/codec/deflate/compress/new_spec.rb +0 -39
  39. data/spec/archive/zip/codec/deflate/compress/open_spec.rb +0 -48
  40. data/spec/archive/zip/codec/deflate/compress/write_spec.rb +0 -111
  41. data/spec/archive/zip/codec/deflate/decompress/checksum_spec.rb +0 -20
  42. data/spec/archive/zip/codec/deflate/decompress/close_spec.rb +0 -34
  43. data/spec/archive/zip/codec/deflate/decompress/crc32_spec.rb +0 -20
  44. data/spec/archive/zip/codec/deflate/decompress/data_descriptor_spec.rb +0 -74
  45. data/spec/archive/zip/codec/deflate/decompress/new_spec.rb +0 -16
  46. data/spec/archive/zip/codec/deflate/decompress/open_spec.rb +0 -29
  47. data/spec/archive/zip/codec/deflate/fixtures/classes.rb +0 -25
  48. data/spec/archive/zip/codec/deflate/fixtures/compressed_file.bin +0 -1
  49. data/spec/archive/zip/codec/deflate/fixtures/compressed_file_nocomp.bin +0 -0
  50. data/spec/archive/zip/codec/deflate/fixtures/raw_file.txt +0 -10
  51. data/spec/archive/zip/codec/null_encryption/decrypt/close_spec.rb +0 -34
  52. data/spec/archive/zip/codec/null_encryption/decrypt/new_spec.rb +0 -16
  53. data/spec/archive/zip/codec/null_encryption/decrypt/open_spec.rb +0 -29
  54. data/spec/archive/zip/codec/null_encryption/decrypt/read_spec.rb +0 -26
  55. data/spec/archive/zip/codec/null_encryption/decrypt/rewind_spec.rb +0 -27
  56. data/spec/archive/zip/codec/null_encryption/decrypt/seek_spec.rb +0 -59
  57. data/spec/archive/zip/codec/null_encryption/decrypt/tell_spec.rb +0 -23
  58. data/spec/archive/zip/codec/null_encryption/encrypt/close_spec.rb +0 -34
  59. data/spec/archive/zip/codec/null_encryption/encrypt/new_spec.rb +0 -16
  60. data/spec/archive/zip/codec/null_encryption/encrypt/open_spec.rb +0 -31
  61. data/spec/archive/zip/codec/null_encryption/encrypt/rewind_spec.rb +0 -28
  62. data/spec/archive/zip/codec/null_encryption/encrypt/seek_spec.rb +0 -52
  63. data/spec/archive/zip/codec/null_encryption/encrypt/tell_spec.rb +0 -31
  64. data/spec/archive/zip/codec/null_encryption/encrypt/write_spec.rb +0 -31
  65. data/spec/archive/zip/codec/null_encryption/fixtures/classes.rb +0 -12
  66. data/spec/archive/zip/codec/null_encryption/fixtures/raw_file.txt +0 -10
  67. data/spec/archive/zip/codec/store/compress/close_spec.rb +0 -34
  68. data/spec/archive/zip/codec/store/compress/data_descriptor_spec.rb +0 -77
  69. data/spec/archive/zip/codec/store/compress/new_spec.rb +0 -16
  70. data/spec/archive/zip/codec/store/compress/open_spec.rb +0 -29
  71. data/spec/archive/zip/codec/store/compress/rewind_spec.rb +0 -28
  72. data/spec/archive/zip/codec/store/compress/seek_spec.rb +0 -52
  73. data/spec/archive/zip/codec/store/compress/tell_spec.rb +0 -31
  74. data/spec/archive/zip/codec/store/compress/write_spec.rb +0 -29
  75. data/spec/archive/zip/codec/store/decompress/close_spec.rb +0 -34
  76. data/spec/archive/zip/codec/store/decompress/data_descriptor_spec.rb +0 -75
  77. data/spec/archive/zip/codec/store/decompress/new_spec.rb +0 -16
  78. data/spec/archive/zip/codec/store/decompress/open_spec.rb +0 -29
  79. data/spec/archive/zip/codec/store/decompress/read_spec.rb +0 -26
  80. data/spec/archive/zip/codec/store/decompress/rewind_spec.rb +0 -27
  81. data/spec/archive/zip/codec/store/decompress/seek_spec.rb +0 -59
  82. data/spec/archive/zip/codec/store/decompress/tell_spec.rb +0 -23
  83. data/spec/archive/zip/codec/store/fixtures/classes.rb +0 -12
  84. data/spec/archive/zip/codec/store/fixtures/raw_file.txt +0 -10
  85. data/spec/archive/zip/codec/traditional_encryption/decrypt/close_spec.rb +0 -53
  86. data/spec/archive/zip/codec/traditional_encryption/decrypt/new_spec.rb +0 -20
  87. data/spec/archive/zip/codec/traditional_encryption/decrypt/open_spec.rb +0 -43
  88. data/spec/archive/zip/codec/traditional_encryption/decrypt/read_spec.rb +0 -127
  89. data/spec/archive/zip/codec/traditional_encryption/decrypt/rewind_spec.rb +0 -36
  90. data/spec/archive/zip/codec/traditional_encryption/decrypt/seek_spec.rb +0 -80
  91. data/spec/archive/zip/codec/traditional_encryption/decrypt/tell_spec.rb +0 -27
  92. data/spec/archive/zip/codec/traditional_encryption/encrypt/close_spec.rb +0 -53
  93. data/spec/archive/zip/codec/traditional_encryption/encrypt/new_spec.rb +0 -20
  94. data/spec/archive/zip/codec/traditional_encryption/encrypt/open_spec.rb +0 -41
  95. data/spec/archive/zip/codec/traditional_encryption/encrypt/rewind_spec.rb +0 -39
  96. data/spec/archive/zip/codec/traditional_encryption/encrypt/seek_spec.rb +0 -73
  97. data/spec/archive/zip/codec/traditional_encryption/encrypt/tell_spec.rb +0 -40
  98. data/spec/archive/zip/codec/traditional_encryption/encrypt/write_spec.rb +0 -114
  99. data/spec/archive/zip/codec/traditional_encryption/fixtures/classes.rb +0 -27
  100. data/spec/archive/zip/codec/traditional_encryption/fixtures/encrypted_file.bin +0 -0
  101. data/spec/archive/zip/codec/traditional_encryption/fixtures/raw_file.txt +0 -10
  102. data/spec/binary_stringio/new_spec.rb +0 -40
  103. data/spec/binary_stringio/set_encoding_spec.rb +0 -17
  104. data/spec/ioextensions/read_exactly_spec.rb +0 -52
  105. data/spec/zlib/fixtures/classes.rb +0 -65
  106. data/spec/zlib/fixtures/compressed_file.bin +0 -1
  107. data/spec/zlib/fixtures/compressed_file_gzip.bin +0 -0
  108. data/spec/zlib/fixtures/compressed_file_huffman.bin +0 -2
  109. data/spec/zlib/fixtures/compressed_file_minmem.bin +0 -0
  110. data/spec/zlib/fixtures/compressed_file_minwin.bin +0 -1
  111. data/spec/zlib/fixtures/compressed_file_nocomp.bin +0 -0
  112. data/spec/zlib/fixtures/compressed_file_raw.bin +0 -1
  113. data/spec/zlib/fixtures/raw_file.txt +0 -10
  114. data/spec/zlib/zreader/checksum_spec.rb +0 -42
  115. data/spec/zlib/zreader/close_spec.rb +0 -16
  116. data/spec/zlib/zreader/compressed_size_spec.rb +0 -20
  117. data/spec/zlib/zreader/new_spec.rb +0 -43
  118. data/spec/zlib/zreader/open_spec.rb +0 -51
  119. data/spec/zlib/zreader/read_spec.rb +0 -58
  120. data/spec/zlib/zreader/rewind_spec.rb +0 -25
  121. data/spec/zlib/zreader/seek_spec.rb +0 -57
  122. data/spec/zlib/zreader/tell_spec.rb +0 -23
  123. data/spec/zlib/zreader/uncompressed_size_spec.rb +0 -20
  124. data/spec/zlib/zwriter/checksum_spec.rb +0 -43
  125. data/spec/zlib/zwriter/close_spec.rb +0 -16
  126. data/spec/zlib/zwriter/compressed_size_spec.rb +0 -21
  127. data/spec/zlib/zwriter/new_spec.rb +0 -66
  128. data/spec/zlib/zwriter/open_spec.rb +0 -70
  129. data/spec/zlib/zwriter/rewind_spec.rb +0 -28
  130. data/spec/zlib/zwriter/seek_spec.rb +0 -52
  131. data/spec/zlib/zwriter/tell_spec.rb +0 -31
  132. data/spec/zlib/zwriter/uncompressed_size_spec.rb +0 -21
  133. data/spec/zlib/zwriter/write_spec.rb +0 -28
@@ -0,0 +1,209 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'io/like_helpers/delegated_io'
4
+
5
+ require 'archive/support/zlib'
6
+ require 'archive/zip/data_descriptor'
7
+
8
+ module Archive; class Zip; module Codec; class Deflate
9
+ # Archive::Zip::Codec::Deflate::Writer extends Zlib::ZWriter in order to
10
+ # specify the standard Zlib options required by ZIP archives and to provide
11
+ # a close method which can optionally close the delegate IO-like object.
12
+ # In addition a convenience method is provided for generating DataDescriptor
13
+ # objects based on the data which is passed through this object.
14
+ #
15
+ # Instances of this class should only be accessed via the
16
+ # Archive::Zip::Codec::Deflate#compressor method.
17
+ class Writer < IO::LikeHelpers::DelegatedIO
18
+ # Creates a new instance of this class. _delegate_ must respond to the
19
+ # _write_ method as an instance of IO would. _level_, _mem_level_, and
20
+ # _strategy_ are all passed directly to Zlib::Deflate.new().
21
+ #
22
+ # <b>
23
+ # The following descriptions of _level_, _mem_level_, and _strategy_ are based
24
+ # upon or pulled largely verbatim from descriptions found in zlib.h version
25
+ # 1.2.3 with changes made to account for different parameter names and to
26
+ # improve readability. Some of the statements concerning default settings or
27
+ # value ranges may not be accurate depending on the version of the zlib
28
+ # library used by a given Ruby interpreter.
29
+ # </b>
30
+ #
31
+ # The _level_ parameter must be +nil+, Zlib::DEFAULT_COMPRESSION, or between
32
+ # <tt>0</tt> and <tt>9</tt>: <tt>1</tt> gives best speed, <tt>9</tt> gives
33
+ # best compression, <tt>0</tt> gives no compression at all (the input data
34
+ # is simply copied a block at a time). Zlib::DEFAULT_COMPRESSION requests a
35
+ # default compromise between speed and compression (currently equivalent to
36
+ # level <tt>6</tt>). If unspecified or +nil+, _level_ defaults to
37
+ # Zlib::DEFAULT_COMPRESSION.
38
+ #
39
+ # The _mem_level_ parameter specifies how much memory should be allocated
40
+ # for the internal compression state. A value of <tt>1</tt> uses minimum
41
+ # memory but is slow and reduces compression ratio; a value of <tt>9</tt>
42
+ # uses maximum memory for optimal speed. The default value is <tt>8</tt> if
43
+ # unspecified or +nil+.
44
+ #
45
+ # The _strategy_ parameter is used to tune the compression algorithm. It
46
+ # only affects the compression ratio but not the correctness of the
47
+ # compressed output even if it is not set appropriately. The default value
48
+ # is Zlib::DEFAULT_STRATEGY if unspecified or +nil+.
49
+ #
50
+ # Use the value Zlib::DEFAULT_STRATEGY for normal data, Zlib::FILTERED for
51
+ # data produced by a filter (or predictor), Zlib::HUFFMAN_ONLY to force
52
+ # Huffman encoding only (no string match), Zlib::RLE to limit match
53
+ # distances to 1 (run-length encoding), or Zlib::FIXED to simplify decoder
54
+ # requirements.
55
+ #
56
+ # The effect of Zlib::FILTERED is to force more Huffman coding and less
57
+ # string matching; it is somewhat intermediate between
58
+ # Zlib::DEFAULT_STRATEGY and Zlib::HUFFMAN_ONLY. Filtered data consists
59
+ # mostly of small values with a somewhat random distribution. In this case,
60
+ # the compression algorithm is tuned to compress them better.
61
+ #
62
+ # Zlib::RLE is designed to be almost as fast as Zlib::HUFFMAN_ONLY, but give
63
+ # better compression for PNG image data.
64
+ #
65
+ # Zlib::FIXED prevents the use of dynamic Huffman codes, allowing for a
66
+ # simpler decoder for special applications.
67
+ #
68
+ # This class has extremely limited seek capabilities. It is possible to
69
+ # seek with an offset of <tt>0</tt> and a whence of <tt>IO::SEEK_CUR</tt>.
70
+ # As a result, the _pos_ and _tell_ methods also work as expected.
71
+ #
72
+ # If _delegate_ also responds to _rewind_, then the _rewind_ method of this
73
+ # class can be used to reset the whole stream back to the beginning. Using
74
+ # _seek_ of this class to seek directly to offset <tt>0</tt> using
75
+ # <tt>IO::SEEK_SET</tt> for whence will also work in this case.
76
+ #
77
+ # <b>NOTE:</b> Due to limitations in Ruby's finalization capabilities, the
78
+ # #close method is _not_ automatically called when this object is garbage
79
+ # collected. Make sure to call #close when finished with this object.
80
+ def initialize(
81
+ delegate,
82
+ autoclose: true,
83
+ level: Zlib::DEFAULT_COMPRESSION,
84
+ mem_level: nil,
85
+ strategy: nil
86
+ )
87
+ super(delegate, autoclose: autoclose)
88
+
89
+ @deflater = Zlib::Deflate.new(level, -Zlib::MAX_WBITS, mem_level, strategy)
90
+ self.deflate_buffer = ''
91
+ @crc32 = 0
92
+ @compressed_size = nil
93
+ @uncompressed_size = nil
94
+ end
95
+
96
+ # Closes the writer by finishing the compressed data and flushing it to the
97
+ # delegate.
98
+ def close
99
+ return nil if closed?
100
+
101
+ result = flush
102
+ return result if Symbol === result
103
+
104
+ unless @deflater.finished?
105
+ self.deflate_buffer = @deflater.finish
106
+ result = flush
107
+ return result if Symbol === result
108
+ end
109
+
110
+ @compressed_size = @deflater.total_out
111
+ @uncompressed_size = @deflater.total_in
112
+ @deflater.close
113
+
114
+ super
115
+ end
116
+
117
+ # The CRC32 checksum of the uncompressed data written using this object.
118
+ #
119
+ # <b>NOTE:</b> Anything still in the internal write buffer has not been
120
+ # processed, so calling #flush prior to examining this attribute may be
121
+ # necessary for an accurate computation.
122
+ attr_reader :crc32
123
+
124
+ # Returns the number of bytes of compressed data produced so far.
125
+ #
126
+ # <b>NOTE:</b> This value is only updated when both the internal write
127
+ # buffer is flushed and there is enough data to produce a compressed block.
128
+ # It does not necessarily reflect the amount of data written to the
129
+ # delegate until this stream is closed however. Until then the only
130
+ # guarantee is that the value will be greater than or equal to <tt>0</tt>.
131
+ def compressed_size
132
+ @deflater.closed? ? @compressed_size : @deflater.total_out
133
+ end
134
+
135
+ # Returns the number of bytes sent to be compressed so far.
136
+ #
137
+ # <b>NOTE:</b> This value is only updated when the internal write buffer is
138
+ # flushed.
139
+ def uncompressed_size
140
+ @deflater.closed? ? @uncompressed_size : @deflater.total_in
141
+ end
142
+
143
+ # Returns an instance of Archive::Zip::DataDescriptor with information
144
+ # regarding the data which has passed through this object to the delegate
145
+ # object. The close or flush methods should be called before using this
146
+ # method in order to ensure that any possibly buffered data is flushed to
147
+ # the delegate object; otherwise, the contents of the data descriptor may
148
+ # be inaccurate.
149
+ def data_descriptor
150
+ DataDescriptor.new(crc32, compressed_size, uncompressed_size)
151
+ end
152
+
153
+ # Allows resetting this object and the delegate object back to the beginning
154
+ # of the stream or reporting the current position in the stream.
155
+ #
156
+ # Raises Errno::EINVAL unless _offset_ is <tt>0</tt> and _whence_ is either
157
+ # IO::SEEK_SET or IO::SEEK_CUR. Raises Errno::EINVAL if _whence_ is
158
+ # IO::SEEK_SEK and the delegate object does not respond to the _rewind_
159
+ # method.
160
+ def seek(amount, whence = IO::SEEK_SET)
161
+ assert_open
162
+ raise Errno::ESPIPE if amount != 0 || whence == IO::SEEK_END
163
+
164
+ case whence
165
+ when IO::SEEK_SET
166
+ result = super
167
+ return result if Symbol === result
168
+ @deflater.reset
169
+ self.deflate_buffer = ''
170
+ @crc32 = 0
171
+ result
172
+ when IO::SEEK_CUR
173
+ @deflater.total_in
174
+ else
175
+ raise Errno::EINVAL
176
+ end
177
+ end
178
+
179
+ def write(buffer, length: buffer.bytesize)
180
+ # First try to write out the contents of the deflate buffer because if
181
+ # that raises a failure we can let that pass up the call stack without
182
+ # having polluted the deflater instance.
183
+ result = flush
184
+ return result if Symbol === result
185
+
186
+ buffer = buffer[0, length] unless length == buffer.bytesize
187
+ self.deflate_buffer = @deflater.deflate(buffer)
188
+ @crc32 = Zlib.crc32(buffer, @crc32)
189
+
190
+ length
191
+ end
192
+
193
+ private
194
+
195
+ def deflate_buffer=(buffer)
196
+ @deflate_buffer = buffer
197
+ @deflate_buffer_idx = 0
198
+ end
199
+
200
+ def flush
201
+ while @deflate_buffer_idx < @deflate_buffer.bytesize
202
+ result = delegate.write(@deflate_buffer[@deflate_buffer_idx..-1])
203
+ return result if Symbol === result
204
+ @deflate_buffer_idx += result
205
+ end
206
+ nil
207
+ end
208
+ end
209
+ end; end; end; end
@@ -1,256 +1,104 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require 'archive/support/zlib'
4
3
  require 'archive/zip/codec'
5
- require 'archive/zip/data_descriptor'
4
+ require 'archive/zip/codec/deflate/reader'
5
+ require 'archive/zip/codec/deflate/writer'
6
6
 
7
7
  module Archive; class Zip; module Codec
8
- # Archive::Zip::Codec::Deflate is a handle for the deflate-inflate codec
9
- # as defined in Zlib which provides convenient interfaces for writing and
10
- # reading deflated streams.
11
- class Deflate
12
- # Archive::Zip::Codec::Deflate::Compress extends Zlib::ZWriter in order to
13
- # specify the standard Zlib options required by ZIP archives and to provide
14
- # a close method which can optionally close the delegate IO-like object.
15
- # In addition a convenience method is provided for generating DataDescriptor
16
- # objects based on the data which is passed through this object.
17
- #
18
- # Instances of this class should only be accessed via the
19
- # Archive::Zip::Codec::Deflate#compressor method.
20
- class Compress < Zlib::ZWriter
21
- # Creates a new instance of this class with the given arguments using #new
22
- # and then passes the instance to the given block. The #close method is
23
- # guaranteed to be called after the block completes.
24
- #
25
- # Equivalent to #new if no block is given.
26
- def self.open(io, compression_level)
27
- deflate_io = new(io, compression_level)
28
- return deflate_io unless block_given?
29
-
30
- begin
31
- yield(deflate_io)
32
- ensure
33
- deflate_io.close unless deflate_io.closed?
34
- end
35
- end
36
-
37
- # Creates a new instance of this class using _io_ as a data sink. _io_
38
- # must be writable and must provide a _write_ method as IO does or errors
39
- # will be raised when performing write operations. _compression_level_
40
- # must be one of Zlib::DEFAULT_COMPRESSION, Zlib::BEST_COMPRESSION,
41
- # Zlib::BEST_SPEED, or Zlib::NO_COMPRESSION and specifies the amount of
42
- # compression to be applied to the data stream.
43
- def initialize(io, compression_level)
44
- super(io, compression_level, -Zlib::MAX_WBITS)
45
- @crc32 = 0
46
- end
47
-
48
- # The CRC32 checksum of the uncompressed data written using this object.
49
- #
50
- # <b>NOTE:</b> Anything still in the internal write buffer has not been
51
- # processed, so calling #flush prior to examining this attribute may be
52
- # necessary for an accurate computation.
53
- attr_reader :crc32
54
- alias :checksum :crc32
55
-
56
- # Closes this object so that further write operations will fail. If
57
- # _close_delegate_ is +true+, the delegate object used as a data sink will
58
- # also be closed using its close method.
59
- def close(close_delegate = true)
60
- super()
61
- delegate.close if close_delegate
62
- end
63
-
64
- # Returns an instance of Archive::Zip::DataDescriptor with information
65
- # regarding the data which has passed through this object to the delegate
66
- # object. The close or flush methods should be called before using this
67
- # method in order to ensure that any possibly buffered data is flushed to
68
- # the delegate object; otherwise, the contents of the data descriptor may
69
- # be inaccurate.
70
- def data_descriptor
71
- DataDescriptor.new(crc32, compressed_size, uncompressed_size)
72
- end
73
-
74
- private
75
-
76
- def unbuffered_seek(offset, whence = IO::SEEK_SET)
77
- result = super(offset, whence)
78
- @crc32 = 0 if whence == IO::SEEK_SET
79
- result
80
- end
81
-
82
- def unbuffered_write(string)
83
- result = super(string)
84
- @crc32 = Zlib.crc32(string, @crc32)
85
- result
86
- end
87
- end
88
-
89
- # Archive::Zip::Codec::Deflate::Decompress extends Zlib::ZReader in order to
90
- # specify the standard Zlib options required by ZIP archives and to provide
91
- # a close method which can optionally close the delegate IO-like object.
92
- # In addition a convenience method is provided for generating DataDescriptor
93
- # objects based on the data which is passed through this object.
94
- #
95
- # Instances of this class should only be accessed via the
96
- # Archive::Zip::Codec::Deflate#decompressor method.
97
- class Decompress < Zlib::ZReader
98
- # Creates a new instance of this class with the given arguments using #new
99
- # and then passes the instance to the given block. The #close method is
100
- # guaranteed to be called after the block completes.
101
- #
102
- # Equivalent to #new if no block is given.
103
- def self.open(io)
104
- inflate_io = new(io)
105
- return inflate_io unless block_given?
106
-
107
- begin
108
- yield(inflate_io)
109
- ensure
110
- inflate_io.close unless inflate_io.closed?
111
- end
112
- end
113
-
114
- # Creates a new instance of this class using _io_ as a data source. _io_
115
- # must be readable and provide a _read_ method as IO does or errors will
116
- # be raised when performing read operations. If _io_ provides a _rewind_
117
- # method, this class' _rewind_ method will be enabled.
118
- def initialize(io)
119
- super(io, -Zlib::MAX_WBITS)
120
- @crc32 = 0
121
- end
122
-
123
- # The CRC32 checksum of the uncompressed data read using this object.
124
- #
125
- # <b>NOTE:</b> The contents of the internal read buffer are immediately
126
- # processed any time the internal buffer is filled, so this checksum is
127
- # only accurate if all data has been read out of this object.
128
- attr_reader :crc32
129
- alias :checksum :crc32
130
-
131
- # Closes this object so that further read operations will fail. If
132
- # _close_delegate_ is +true+, the delegate object used as a data source
133
- # will also be closed using its close method.
134
- def close(close_delegate = true)
135
- super()
136
- delegate.close if close_delegate
137
- end
138
-
139
- # Returns an instance of Archive::Zip::DataDescriptor with information
140
- # regarding the data which has passed through this object from the
141
- # delegate object. It is recommended to call the close method before
142
- # calling this in order to ensure that no further read operations change
143
- # the state of this object.
144
- def data_descriptor
145
- DataDescriptor.new(crc32, compressed_size, uncompressed_size)
146
- end
147
-
148
- private
149
-
150
- def unbuffered_read(length)
151
- result = super(length)
152
- @crc32 = Zlib.crc32(result, @crc32)
153
- result
154
- end
155
-
156
- def unbuffered_seek(offset, whence = IO::SEEK_SET)
157
- result = super(offset, whence)
158
- @crc32 = 0 if whence == IO::SEEK_SET
159
- result
160
- end
161
- end
162
-
163
- # The numeric identifier assigned to this compression codec by the ZIP
164
- # specification.
165
- ID = 8
166
-
167
- # Register this compression codec.
168
- COMPRESSION_CODECS[ID] = self
169
-
170
- # A bit mask used to denote that Zlib's default compression level should be
171
- # used.
172
- NORMAL = 0b000
173
- # A bit mask used to denote that Zlib's highest/slowest compression level
174
- # should be used.
175
- MAXIMUM = 0b010
176
- # A bit mask used to denote that Zlib's lowest/fastest compression level
177
- # should be used.
178
- FAST = 0b100
179
- # A bit mask used to denote that Zlib should not compress data at all.
180
- SUPER_FAST = 0b110
181
-
182
- # This method signature is part of the interface contract expected by
183
- # Archive::Zip::Entry for compression codec objects.
184
- #
185
- # Creates a new instance of this class using bits 1 and 2 of
186
- # _general_purpose_flags_ to select a compression level to be used by
187
- # #compressor to set up a compression IO object. The constants NORMAL,
188
- # MAXIMUM, FAST, and SUPER_FAST can be used for _general_purpose_flags_ to
189
- # manually set the compression level.
190
- def initialize(general_purpose_flags = NORMAL)
191
- @compression_level = general_purpose_flags & 0b110
192
- @zlib_compression_level = case @compression_level
193
- when NORMAL
194
- Zlib::DEFAULT_COMPRESSION
195
- when MAXIMUM
196
- Zlib::BEST_COMPRESSION
197
- when FAST
198
- Zlib::BEST_SPEED
199
- when SUPER_FAST
200
- Zlib::NO_COMPRESSION
201
- else
202
- raise Error, 'Invalid compression level'
203
- end
204
- end
8
+ # Archive::Zip::Codec::Deflate is a handle for the deflate-inflate codec
9
+ # as defined in Zlib which provides convenient interfaces for writing and
10
+ # reading deflated streams.
11
+ class Deflate
12
+ # The numeric identifier assigned to this compression codec by the ZIP
13
+ # specification.
14
+ ID = 8
15
+
16
+ # Register this compression codec.
17
+ COMPRESSION_CODECS[ID] = self
18
+
19
+ # A bit mask used to denote that Zlib's default compression level should be
20
+ # used.
21
+ NORMAL = 0b000
22
+ # A bit mask used to denote that Zlib's highest/slowest compression level
23
+ # should be used.
24
+ MAXIMUM = 0b010
25
+ # A bit mask used to denote that Zlib's lowest/fastest compression level
26
+ # should be used.
27
+ FAST = 0b100
28
+ # A bit mask used to denote that Zlib should not compress data at all.
29
+ SUPER_FAST = 0b110
30
+
31
+ # This method signature is part of the interface contract expected by
32
+ # Archive::Zip::Entry for compression codec objects.
33
+ #
34
+ # Creates a new instance of this class using bits 1 and 2 of
35
+ # _general_purpose_flags_ to select a compression level to be used by
36
+ # #compressor to set up a compression IO object. The constants NORMAL,
37
+ # MAXIMUM, FAST, and SUPER_FAST can be used for _general_purpose_flags_ to
38
+ # manually set the compression level.
39
+ def initialize(general_purpose_flags = NORMAL)
40
+ @compression_level = general_purpose_flags & 0b110
41
+ @zlib_compression_level = case @compression_level
42
+ when NORMAL
43
+ Zlib::DEFAULT_COMPRESSION
44
+ when MAXIMUM
45
+ Zlib::BEST_COMPRESSION
46
+ when FAST
47
+ Zlib::BEST_SPEED
48
+ when SUPER_FAST
49
+ Zlib::NO_COMPRESSION
50
+ else
51
+ raise Error, 'Invalid compression level'
52
+ end
53
+ end
205
54
 
206
- # This method signature is part of the interface contract expected by
207
- # Archive::Zip::Entry for compression codec objects.
208
- #
209
- # A convenience method for creating an
210
- # Archive::Zip::Codec::Deflate::Compress object using that class' open
211
- # method. The compression level for the open method is pulled from the
212
- # value of the _general_purpose_flags_ argument of new.
213
- def compressor(io, &b)
214
- Compress.open(io, @zlib_compression_level, &b)
215
- end
55
+ # This method signature is part of the interface contract expected by
56
+ # Archive::Zip::Entry for compression codec objects.
57
+ #
58
+ # A convenience method for creating an
59
+ # Archive::Zip::Codec::Deflate::Writer object using that class' open
60
+ # method. The compression level for the open method is pulled from the
61
+ # value of the _general_purpose_flags_ argument of new.
62
+ def compressor(io, &b)
63
+ Writer.open(io, level: @zlib_compression_level, &b)
64
+ end
216
65
 
217
- # This method signature is part of the interface contract expected by
218
- # Archive::Zip::Entry for compression codec objects.
219
- #
220
- # A convenience method for creating an
221
- # Archive::Zip::Codec::Deflate::Decompress object using that class' open
222
- # method.
223
- def decompressor(io, &b)
224
- Decompress.open(io, &b)
225
- end
66
+ # This method signature is part of the interface contract expected by
67
+ # Archive::Zip::Entry for compression codec objects.
68
+ #
69
+ # A convenience method for creating an
70
+ # Archive::Zip::Codec::Deflate::Reader object using that class' open
71
+ # method.
72
+ def decompressor(io, &b)
73
+ Reader.open(io, &b)
74
+ end
226
75
 
227
- # This method signature is part of the interface contract expected by
228
- # Archive::Zip::Entry for compression codec objects.
229
- #
230
- # Returns an integer which indicates the version of the official ZIP
231
- # specification which introduced support for this compression codec.
232
- def version_needed_to_extract
233
- 0x0014
234
- end
76
+ # This method signature is part of the interface contract expected by
77
+ # Archive::Zip::Entry for compression codec objects.
78
+ #
79
+ # Returns an integer which indicates the version of the official ZIP
80
+ # specification which introduced support for this compression codec.
81
+ def version_needed_to_extract
82
+ 0x0014
83
+ end
235
84
 
236
- # This method signature is part of the interface contract expected by
237
- # Archive::Zip::Entry for compression codec objects.
238
- #
239
- # Returns an integer used to flag that this compression codec is used for a
240
- # particular ZIP archive entry.
241
- def compression_method
242
- ID
243
- end
85
+ # This method signature is part of the interface contract expected by
86
+ # Archive::Zip::Entry for compression codec objects.
87
+ #
88
+ # Returns an integer used to flag that this compression codec is used for a
89
+ # particular ZIP archive entry.
90
+ def compression_method
91
+ ID
92
+ end
244
93
 
245
- # This method signature is part of the interface contract expected by
246
- # Archive::Zip::Entry for compression codec objects.
247
- #
248
- # Returns an integer representing the general purpose flags of a ZIP archive
249
- # entry where bits 1 and 2 are set according to the compression level
250
- # selected for this object. All other bits are zero'd out.
251
- def general_purpose_flags
252
- @compression_level
253
- end
94
+ # This method signature is part of the interface contract expected by
95
+ # Archive::Zip::Entry for compression codec objects.
96
+ #
97
+ # Returns an integer representing the general purpose flags of a ZIP archive
98
+ # entry where bits 1 and 2 are set according to the compression level
99
+ # selected for this object. All other bits are zero'd out.
100
+ def general_purpose_flags
101
+ @compression_level
254
102
  end
103
+ end
255
104
  end; end; end
256
-