class ZStream
A body which compresses or decompresses the contents using the DEFLATE or GZIP algorithm.
Definitions
DEFAULT_LEVEL = 7
The default compression level.
DEFLATE = -Zlib::MAX_WBITS
The DEFLATE window size.
GZIP = Zlib::MAX_WBITS | 16
The GZIP window size.
ENCODINGS = {...}
The supported encodings.
Implementation
ENCODINGS = {
"deflate" => DEFLATE,
"gzip" => GZIP,
}
def initialize(body, stream)
Initialize the body with the given stream.
Signature
-
parameter
body
Readable
the body to wrap.
-
parameter
stream
Zlib::Deflate | Zlib::Inflate
the stream to use for compression or decompression.
Implementation
def initialize(body, stream)
super(body)
@stream = stream
@input_length = 0
@output_length = 0
end
def close(error = nil)
Close the stream.
Signature
-
parameter
error
Exception | Nil
the error that caused the stream to be closed.
Implementation
def close(error = nil)
if stream = @stream
@stream = nil
stream.close unless stream.closed?
end
super
end
def length
The length of the output, if known. Generally, this is not known due to the nature of compression.
Implementation
def length
# We don't know the length of the output until after it's been compressed.
nil
end
attr :input_length
Signature
-
attribute
Integer
input_length the total number of bytes read from the input.
attr :output_length
Signature
-
attribute
Integer
output_length the total number of bytes written to the output.
def ratio
The compression ratio, according to the input and output lengths.
Signature
-
returns
Float
the compression ratio, e.g. 0.5 for 50% compression.
Implementation
def ratio
if @input_length != 0
@output_length.to_f / @input_length.to_f
else
1.0
end
end
def inspect
Inspect the body, including the compression ratio.
Signature
-
returns
String
a string representation of the body.
Implementation
def inspect
"#{super} | \#<#{self.class} #{(ratio*100).round(2)}%>"
end