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:- autoload(library(apply),[partition/4]).   42:- autoload(library(error),[must_be/2]).   43:- autoload(library(option),[merge_options/3,option/2]).   44
   45:- predicate_options(zopen/3, 3,
   46                     [ format(oneof([gzip,deflate,raw_deflate])),
   47                       multi_part(boolean),
   48                       close_parent(boolean),
   49                       level(between(0,9))
   50                     ]).   51:- predicate_options(gzopen/4, 4,
   52                     [ pass_to(zopen/3, 3),
   53                       pass_to(system:open/4, 4)
   54                     ]).   55
   56/** <module> Zlib wrapper for SWI-Prolog
   57
   58Read/write compressed data based on the zlib library.
   59
   60@author Jan Wielemaker
   61@see    http://www.zlib.net/
   62@see    http://www.swi-prolog.org/packages/zlib.html
   63*/
   64
   65:- use_foreign_library(foreign(zlib4pl)).   66:- public zdebug/1.                     % Set debug level
   67
   68%!  gzopen(+File, +Mode, -Stream) is det.
   69%!  gzopen(+File, +Mode, -Stream, +Options) is det.
   70%
   71%   Open a file compatible with the  gzip   program.  Note that if a
   72%   file is opened in =append= mode,  a   second  gzip image will be
   73%   added to the end of the file.   The gzip standard defines that a
   74%   file can hold multiple  gzip  images   and  inflating  the  file
   75%   results in a concatenated stream of all inflated images.
   76%
   77%   Options are passed to open/4  and   zopen/3.  Default  format is
   78%   =gzip=.
   79
   80gzopen(File, Mode, Stream) :-
   81    gzopen(File, Mode, Stream, []).
   82
   83gzopen(File, Mode, Stream, Options) :-
   84    must_be(oneof([read,write,append]), Mode),
   85    partition(zoption, Options, ZOptions0, OpenOptions),
   86    merge_options(ZOptions0,
   87                  [ format(gzip),
   88                    close_parent(true)
   89                  ], ZOptions),
   90    open(File, Mode, Stream0, OpenOptions),
   91    zopen(Stream0, Stream, ZOptions),
   92    set_stream(Stream, file_name(File)),
   93    (   option(alias(Alias), ZOptions)
   94    ->  set_stream(Stream, alias(Alias))
   95    ;   true
   96    ).
   97
   98
   99zoption(format(_)).
  100zoption(multi_part(_)).
  101zoption(level(_)).
  102zoption(alias(_)).
  103
  104%!  http:encoding_filter(+Encoding, +In0, -In) is semidet.
  105%
  106%   Act as plugin for library(http/http_open) for processing content
  107%   with =|Content-encoding: gzip|=
  108
  109http:encoding_filter(gzip, In0, In) :-
  110    zopen(In0, In,
  111          [ close_parent(true)
  112          ])