Bindings to ZLib for MRuby.
compress & uncompress provide a simple API for bulk compression. compress
produces a deflate stream wrapped in a zlib header, and uncompress reverses
that action.
Example:
src = 'test' * 10
# => "testtesttesttesttesttesttesttesttesttest"
src.length
# => 40
compressed = ZLib.compress(src)
# => "x\234+I-.)!\002\003\000f\261\021\201"
compressed.length
# => 15
ZLib.uncompress(src.length, compressed)
# => "testtesttesttesttesttesttesttesttesttest"-
ZLib.compress(src)- Compresses
srcwith default settings & returns the compressed result. - Returns a deflate stream wrapped in a zlib header/footer
- Compresses
-
ZLib.uncompress(max_size, src)- Uncompresses the src string (i.e. reverses
ZLib.compress) - Raises a
ZLib::ZDataErrorif src is not in the zlib format - Raises a
ZLib::ZBufferErrorifmax_sizeis not >= the size of the uncompressed data - Raises a
ZLib::ZDataErrorif the input string is incomplete
- Uncompresses the src string (i.e. reverses
These functions provide a streaming interface for zlib & gzip compression.
Example:
src = "example" * 100
stream = ZLib::ZStream.new
ZLib.deflateInit(stream)
stream.next_in = src
compressed = ZLib.deflate(stream, ZLib::Z_FINISH)
# => "x\234K\255H\314-\310IM\035\245F\251\241F\001\000)h$@"
compressed.length
# => 22
stream = ZLib::ZStream.new
ZLib.inflateInit(stream)
stream.next_in = compressed
ZLib.inflate(stream, ZLib::Z_FINISH)
# => "exampleexampleexampleexampleexampleexampleexampleexampleexampleexample...-
ZLib::deflateInit(stream, level = ZLib::Z_DEFAULT_COMPRESSION)- Initalizes a stream to deflate at the given
level leveldefaults to 6 (ZLib::Z_DEFAULT_COMPRESSION)- Raises a
ZLib::ZStreamErrorif called on a previously initialized stream
- Initalizes a stream to deflate at the given
-
ZLib.deflate(stream, flush)- Deflates all of
stream.next_in, possibly returning a chunk of the compressed output - Will process the remaining input and return all output if
flushisZLib::Z_FINISH - Raises a ZBufferError if
Z_FINISHis used twice - Raises a
ZLib::ZStreamErrorif used on a stream that has been finished already
- Deflates all of
-
ZLib::inflateInit(stream)- Initalizes a stream to inflate data
- Raises a
ZLib::ZStreamErrorif called on a previously initialized stream
-
ZLib.inflate(stream, flush)- Inflates
str, possibly returning a chunk of the uncompressed output - Returns the empty string if the stream has already been finished (TODO: should probably raise)
- Will process the remaining input and return all output if
flushisZLib::Z_FINISH
- Inflates
-
ZLib.inflateEnd(stream)&&ZLib.deflateEnd(stream)- Are used to cleanup native resources of the stream
- Called automatically by the MRuby GC when the stream is freed
- Raise a
ZLib::ZStreamErrorif the stream already ended, or is of the wrong type
These functions provide IO for GZip files similar to C's stdlib fopen, fread, fwrite, etc.
-
ZLib.gzopen(filename, mode)- Returns a
ZLib::GZFilebased on the givenfilename&modestring - Open semantics are like C's
fopen("w" create a file if it doesn't exist, etc.) - Returns
nilif trying to read a file that doesn't exist
- Returns a
-
ZLib.gzwrite(file, str)- Compresses
str, then writes it tofile(aZLib::GZFile)
- Compresses
-
ZLib::gzread(file, size)- Reads up to
sizeuncompressed bytes from the compressed GZFilefile
- Reads up to
-
ZLib.gzflush(file, flush)- Flushes the given file according to the
flushargument flushshould be one ofZLib::Z_NO_FLUSH,ZLib::Z_PARTIAL_FLUSH,ZLib::Z_SYNC_FLUSH,ZLib::Z_FULL_FLUSH, orZLib::Z_FINISH- Should be called on
GZFiles when finished, to ensure the gz file has the proper footer
- Flushes the given file according to the
-
ZLib.gzclose(file)- Closes the given GZFile
file - Is called automatically when the file is GC'ed, but you may still call it sooner
- After a file is closed, any attempt to use it will raise a
ZLib::ZIOError
- Closes the given GZFile
The following functions marked [SKIPPED] are implemented already, but lacking automated tests.
-
ZLib.gzsetparams(file, level, strategy)- [SKIPPED] Dynamically update the compression level or strategy
-
ZLib.gztell(file)- [SKIPPED] Tells the currect seek position
-
ZLib.gzrewind(file)- [SKIPPED] Seek to the begining of the file
-
ZLib.gzseek(file, offset, whence)- [SKIPPED] Set the location for the next read/write
-
ZLib.gzputs(file, string)- [SKIPPED] Writes the
stringparam (which must not contain null characters) to the file
- [SKIPPED] Writes the
-
ZLib.gzputc(file, char)(char should be an int as in'a'.ord)- [SKIPPED] Writes a character
-
ZLib.gzungetc(char, file)(char should be an int as in'a'.ord)- [SKIPPED] Ungets a character
-
ZLib.gzbuffer(file, size)- [SKIPPED] Set the internal buffer size used by this library's functions
-
ZLib.gzclearerr(file)- [SKIPPED] Clears the error and end-of-file flags for file
-
ZLib.gzdirect(file)- [SKIPPED] Returns true if file is being copied directly while reading, or false if file is a gzip stream being decompressed
-
ZLib.gzdopen(fd, mode)- [SKIPPED] gzdopen associates a gzFile * with the file descriptor fd.
-
ZLib.gzeof(file)- [SKIPPED] Returns true if the file is at eof, or else false
-
ZLib.gzerror(file)- [SKIPPED] Returns the error message for the last error which occurred on the given compressed file
-
ZLib.gzgetc- [SKIPPED] Reads one byte from the compressed file.
- [SKIPPED] Returns the byte (as an int) or -1 in case of end of file or error.
-
ZLib.gzoffset- [SKIPPED] Returns the current offset in the file being read or written
- [SKIPPED] When reading, the offset does not include as yet unused buffered input
- [SKIPPED] This information can be used for a progress indicator
-
ZLib.gzgets(file)- [SKIPPED] Reads until a newline or eof
SUCCESS [0 failed, 19 skipped, 51 total]