Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC/merge: Support for gzip functions in zlib #1355

Merged
merged 2 commits into from Oct 28, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 5 additions & 9 deletions base/io.jl
Expand Up @@ -337,16 +337,12 @@ function read(s::IOStream, ::Type{Uint8})
end

function read{T<:Union(Int8,Uint8,Int16,Uint16,Int32,Uint32,Int64,Uint64,Int128,Uint128,Float32,Float64,Complex64,Complex128)}(s::IOStream, a::Array{T})
if isa(T,BitsKind)
nb = numel(a)*sizeof(T)
if ccall(:ios_readall, Uint,
(Ptr{Void}, Ptr{Void}, Uint), s.ios, a, nb) < nb
throw(EOFError())
end
a
else
invoke(read, (Any, Array), s, a)
nb = numel(a)*sizeof(T)
if ccall(:ios_readall, Uint,
(Ptr{Void}, Ptr{Void}, Uint), s.ios, a, nb) < nb
throw(EOFError())
end
a
end

## text I/O ##
Expand Down
84 changes: 84 additions & 0 deletions doc/helpdb.jl
Expand Up @@ -4454,6 +4454,90 @@ glp_eval_tab_col(glp_prob, k)

"),

(E"gzip.jl",E"gzopen",E"gzopen(fname[, gzmode[, buf_size]])

Opens a file with mode (default 'r'), setting internal buffer size
to buf_size (default Z_DEFAULT_BUFSIZE=8192), and returns a the
file as a GZipStream.

'gzmode' must contain one of

+------+-----------------------------------+
| r | read |
+------+-----------------------------------+
| w | write, create, truncate |
+------+-----------------------------------+
| a | write, create, append |
+------+-----------------------------------+

In addition, gzmode may also contain

+-------+-----------------------------------+
| x | create the file exclusively |
+-------+-----------------------------------+
| 0-9 | compression level |
+-------+-----------------------------------+

and/or a compression strategy:

+------+-----------------------------------+
| f | filtered data |
+------+-----------------------------------+
| h | Huffman-only compression |
+------+-----------------------------------+
| R | run-length encoding |
+------+-----------------------------------+
| F | fixed code compression |
+------+-----------------------------------+

Note that '+' is not allowed in gzmode.

If an error occurs, 'gzopen' throws a GZError()

"),

(E"gzip.jl",E"gzdopen",E"gzdopen(fd[, gzmode[, buf_size]])

Create a 'GZipStream' object from an integer file descriptor. See
'gzopen' for 'gzmode' and 'buf_size' descriptions.

"),

(E"gzip.jl",E"gzdopen",E"gzdopen(s[, gzmode[, buf_size]])

Create a 'GZipStream' object from IOStream 's'.

"),

(E"gzip.jl",E"GZipStream",E"type GZipStream(name, gz_file[, buf_size[, fd[, s]]])

Subtype of 'IO' which wraps a gzip stream. Returned by 'gzopen'
and 'gzdopen'.

"),

(E"gzip.jl",E"GZError",E"type GZError(err, err_str)

gzip error number and string. Possible error values:

+-----------------------+-----------------------------------------+
| 'Z_OK' | No error |
+-----------------------+-----------------------------------------+
| 'Z_ERRNO' | Filesystem error (consult errno()) |
+-----------------------+-----------------------------------------+
| 'Z_STREAM_ERROR' | Inconsistent stream state |
+-----------------------+-----------------------------------------+
| 'Z_DATA_ERROR' | Compressed data error |
+-----------------------+-----------------------------------------+
| 'Z_MEM_ERROR' | Out of memory |
+-----------------------+-----------------------------------------+
| 'Z_BUF_ERROR' | Input buffer full/output buffer empty |
+-----------------------+-----------------------------------------+
| 'Z_VERSION_ERROR' | zlib library version is incompatible |
+-----------------------+-----------------------------------------+

"),


(E"options.jl",E"options",E"options()

Expand Down
126 changes: 126 additions & 0 deletions doc/stdlib/gzip.rst
@@ -0,0 +1,126 @@
:mod:`gzip.jl` --- Wrapper for gzip functions in zlib
=====================================================

.. module:: gzip.jl
:synopsis: Wrapper for gzip functions in zlib


This module provides a wrapper for the gzip related functions of
(`zlib <http://zlib.net/>`_), a free, general-purpose, legally
unencumbered, lossless data-compression library. These functions
allow the reading and writing of gzip files.

It is currently based on ``zlib`` 1.2.7.

-----
Notes
-----

* This interface is only for gzipped files, not the streaming zlib
compression interface. Internally, it depends on/uses the streaming
interface, but the gzip related functions are higher level
functions pertaining to gzip files only.

* GZipStream is an implementation of IO and can be used virtually
anywhere IO is used.

* This implementation mimics the IOStream implementation, and should
be a drop-in replacement for IOStream, with some exceptions
* ``seek_end()`` and ``truncate()`` are not available
* ``readuntil()`` is available, but is not very efficient. (But readline() works fine.)

In addition to ``gzopen()`` and ``gzfdio()``/``gzdopen()``, the
following IO/IOStream functions are supported:

``close()``
``flush()``
``seek()``
``skip()``
``position()``
``eof()``
``read()``
``readuntil()``
``readline()``
``write()``

Due to limitations in ``zlib``, ``seek_end()`` and ``truncate()`` are not available.

---------
Functions
---------

.. function:: gzopen(fname[, gzmode[, buf_size]])

Opens a file with mode (default "r"), setting internal buffer size
to buf_size (default Z_DEFAULT_BUFSIZE=8192), and returns a the
file as a GZipStream.

``gzmode`` must contain one of

==== =================================
r read
w write, create, truncate
a write, create, append
==== =================================

In addition, gzmode may also contain

===== =================================
x create the file exclusively
(fails if file exists)
0-9 compression level
===== =================================

and/or a compression strategy:

==== =================================
f filtered data
h Huffman-only compression
R run-length encoding
F fixed code compression
==== =================================

Note that '+' is not allowed in gzmode.

If an error occurs, ``gzopen`` throws a GZError()


.. function:: gzdopen(fd[, gzmode[, buf_size]])

Create a ``GZipStream`` object from an integer file descriptor.
See ``gzopen`` for ``gzmode`` and ``buf_size`` descriptions.

.. function:: gzdopen(s[, gzmode[, buf_size]])

Create a ``GZipStream`` object from IOStream ``s``.

-----
Types
-----

.. type:: GZipStream(name, gz_file[, buf_size[, fd[, s]]])

Subtype of ``IO`` which wraps a gzip stream. Returned by
``gzopen`` and ``gzdopen``.

.. type:: GZError(err, err_str)

gzip error number and string. Possible error values:

+---------------------+---------------------------------------+
| ``Z_OK`` | No error |
+---------------------+---------------------------------------+
| ``Z_ERRNO`` | Filesystem error (consult errno()) |
+---------------------+---------------------------------------+
| ``Z_STREAM_ERROR`` | Inconsistent stream state |
+---------------------+---------------------------------------+
| ``Z_DATA_ERROR`` | Compressed data error |
+---------------------+---------------------------------------+
| ``Z_MEM_ERROR`` | Out of memory |
+---------------------+---------------------------------------+
| ``Z_BUF_ERROR`` | Input buffer full/output buffer empty |
+---------------------+---------------------------------------+
| ``Z_VERSION_ERROR`` | zlib library version is incompatible |
| | with caller version |
+---------------------+---------------------------------------+

1 change: 1 addition & 0 deletions doc/stdlib/index.rst
Expand Up @@ -32,6 +32,7 @@ Extras
strpack
sound
argparse
gzip

****************
Math & Numerical
Expand Down