View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2006-2016, University of Amsterdam
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(zlib,
   37          [ zopen/3,                    % +Stream, -ZStream, +Option
   38            gzopen/3,                   % +File, +Mode, -Stream
   39            gzopen/4                    % +File, +Mode, -Stream, +Options
   40          ]).   41:- use_module(library(shlib)).   42:- use_module(library(error)).   43:- use_module(library(apply)).   44:- use_module(library(option)).   45
   46:- predicate_options(zopen/3, 3,
   47                     [ format(oneof([gzip,deflate])),
   48                       multi_part(boolean),
   49                       close_parent(boolean),
   50                       level(between(0,9))
   51                     ]).   52:- predicate_options(gzopen/4, 4,
   53                     [ pass_to(zopen/3, 3),
   54                       pass_to(system:open/4, 4)
   55                     ]).   56
   57/** <module> Zlib wrapper for SWI-Prolog
   58
   59Read/write compressed data based on the zlib library.
   60
   61@author Jan Wielemaker
   62@see    http://www.zlib.net/
   63@see    http://www.swi-prolog.org/packages/zlib.html
   64*/
   65
   66:- use_foreign_library(foreign(zlib4pl)).   67:- public zdebug/1.                     % Set debug level
   68
   69%!  gzopen(+File, +Mode, -Stream) is det.
   70%!  gzopen(+File, +Mode, -Stream, +Options) is det.
   71%
   72%   Open a file compatible with the  gzip   program.  Note that if a
   73%   file is opened in =append= mode,  a   second  gzip image will be
   74%   added to the end of the file.   The gzip standard defines that a
   75%   file can hold multiple  gzip  images   and  inflating  the  file
   76%   results in a concatenated stream of all inflated images.
   77%
   78%   Options are passed to open/4  and   zopen/3.  Default  format is
   79%   =gzip=.
   80
   81gzopen(File, Mode, Stream) :-
   82    gzopen(File, Mode, Stream, []).
   83
   84gzopen(File, Mode, Stream, Options) :-
   85    must_be(oneof([read,write,append]), Mode),
   86    partition(zoption, Options, ZOptions0, OpenOptions),
   87    merge_options(ZOptions0,
   88                  [ format(gzip),
   89                    close_parent(true)
   90                  ], ZOptions),
   91    open(File, Mode, Stream0, OpenOptions),
   92    zopen(Stream0, Stream, ZOptions),
   93    set_stream(Stream, file_name(File)),
   94    (   option(alias(Alias), ZOptions)
   95    ->  set_stream(Stream, alias(Alias))
   96    ;   true
   97    ).
   98
   99
  100zoption(format(_)).
  101zoption(multi_part(_)).
  102zoption(level(_)).
  103zoption(alias(_)).
  104
  105%!  http:encoding_filter(+Encoding, +In0, -In) is semidet.
  106%
  107%   Act as plugin for library(http/http_open) for processing content
  108%   with =|Content-encoding: gzip|=
  109
  110http:encoding_filter(gzip, In0, In) :-
  111    zopen(In0, In,
  112          [ close_parent(true)
  113          ])