File: zlib.html

package info (click to toggle)
swi-prolog 7.2.3%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 84,180 kB
  • ctags: 45,684
  • sloc: ansic: 330,358; perl: 268,104; sh: 6,795; java: 4,904; makefile: 4,561; cpp: 4,153; ruby: 1,594; yacc: 843; xml: 82; sed: 12; sql: 6
file content (369 lines) | stat: -rw-r--r-- 11,001 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
<head>
<title>SWI-Prolog binding to zlib</title>
<style type="text/css">

/* Style sheet for SWI-Prolog latex2html
*/

dd.defbody
{ margin-bottom: 1em;
}

dt.pubdef
{ background-color: #c5e1ff;
}

dt.multidef
{ background-color: #c8ffc7;
}

.bib dd
{ margin-bottom: 1em;
}

.bib dt
{ float: left;
margin-right: 1.3ex;
}

pre.code
{ margin-left: 1.5em;
margin-right: 1.5em;
border: 1px dotted;
padding-top: 5px;
padding-left: 5px;
padding-bottom: 5px;
background-color: #f8f8f8;
}

div.navigate
{ text-align: center;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
}

div.title
{ text-align: center;
padding-bottom: 1em;
font-size: 200%;
font-weight: bold;
}

div.author
{ text-align: center;
font-style: italic;
}

div.abstract
{ margin-top: 2em;
background-color: #f0f0f0;
border: 1px dotted;
padding: 5px;
margin-left: 10%; margin-right:10%;
}

div.abstract-title
{ text-align: center;
padding: 5px;
font-size: 120%;
font-weight: bold;
}

div.toc-h1
{ font-size: 200%;
font-weight: bold;
}

div.toc-h2
{ font-size: 120%;
font-weight: bold;
margin-left: 2em;
}

div.toc-h3
{ font-size: 100%;
font-weight: bold;
margin-left: 4em;
}

div.toc-h4
{ font-size: 100%;
margin-left: 6em;
}

span.sec-nr
{
}

span.sec-title
{
}

span.pred-ext
{ font-weight: bold;
}

span.pred-tag
{ float: right;
padding-top: 0.2em;
font-size: 80%;
font-style: italic;
color: #202020;
}

div.caption
{ width: 80%;
margin: auto;
text-align:center;
}

/* Footnotes */

sup.fn { color: blue; text-decoration: underline; }
span.fn-text { display: none; }
sup.fn span {display: none;}
sup:hover span
{ display: block !important;
position: absolute; top: auto; left: auto; width: 80%;
color: #000; background: white;
border: 2px solid;
padding: 5px; margin: 10px; z-index: 100;
font-size: smaller;
}

/* Lists */

dl.latex
{ margin-top: 1ex;
margin-bottom: 0.5ex;
}

dl.latex dl.latex dd.defbody
{ margin-bottom: 0.5ex;
}

/* PlDoc Tags */

dl.tags
{ font-size: 90%;
margin-left: 5ex;
margin-top: 1ex;
margin-bottom: 0.5ex;
}

dl.tags dt
{ margin-left: 0pt;
font-weight: bold;
}

dl.tags dd
{ margin-left: 3ex;
}

td.param
{ font-style: italic;
font-weight: bold;
}

/* Index */

dt.index-sep
{ font-weight: bold;
font-size: +1;
margin-top: 1ex;
}
</style>
</head>
<body style="background:white"> 
<div class="title">SWI-Prolog binding to zlib</div>
<div class="author">Jan Wielemaker <br>
University of Amsterdam <br>
VU University Amsterdam <br>
The Netherlands <br>
E-mail: <a class="url" href="mailto:J.Wielemaker@vu.nl">J.Wielemaker@vu.nl</a></div>
<div class="abstract">
<div class="abstract-title">Abstract</div> The library <code>library(zlib)</code> 
provides a binding to the
<a class="url" href="http://www.zlib.net/">zlib</a> general purpose 
compression library. The prolog library aims as seamlessly reading and 
writing files compatible to the <b>gzip</b> program as well as 
compressed (network) communication.
</div>

<h1><a name="document-contents">Table of Contents</a></h1>

<div class="toc">
<div class="toc-h2"><a class="sec" href="#sec:1"><span class="sec-nr">1</span> <span class="sec-title">Zlib 
and compression</span></a></div>
<div class="toc-h2"><a class="sec" href="#sec:2"><span class="sec-nr">2</span> <span class="sec-title">Predicate 
reference</span></a></div>
<div class="toc-h2"><a class="sec" href="#sec:3"><span class="sec-nr">3</span> <span class="sec-title">Interaction 
with Prolog stream predicates</span></a></div>
</div>

<p><h2 id="sec:zlib"><a name="sec:1"><span class="sec-nr">1</span> <span class="sec-title">Zlib 
and compression</span></a></h2>

<a name="sec:zlib"></a>

<p>Zlib is a widespread library implementing the RFC1950 (zlib wrapper), 
RFC1951 (deflate stream) and RFC1952 (gzip wrapper) compression 
standards. The SWI-Prolog binding is a foreign library that creates a 
compressed stream as a wrapper around a normal stream. Implemented this 
way, it can perform a wide variety of tasks:

<p>
<ul class="latex">
<li>Read/write gzip compatible files
<li>Setup standard compressed stream communication
<li>Realise in-memory compression or decompression
<li>Deal with streams holding embedded compressed objects
</ul>

<p>The core predicate of the library is <a name="idx:zopen3:1"></a><a class="pred" href="#zopen/3">zopen/3</a>. 
The remainder of the functionality of <code>library(zlib)</code> is 
defined in Prolog and can be used as a starting point for other 
high-level primitives. See also <code>ztest.pl</code> providing test and 
demo code. This file is part of the source distribution.

<p>Part of the functionality of this library can also be realised using 
the pipe interface and the <b>gzip</b> program. For example, a gziped 
file can also be opened in Prolog using the code below.

<pre class="code">
        ...
        open(pipe('gunzip &lt; file.gz'), read, In),
        ...
</pre>

<p>The advantage of this library over using an external program for such 
tasks is enhanced platform independence and reduced time to open a file. 
Platform independence is improved as we do not have to worry about 
availability of the <b>gunzip</b> utility and we do not have to worry 
about shell and filename quoting issues. While the above replacement 
code works well on most modern Unix systems, it only works with special 
precautions on Windows.<sup class="fn">1<span class="fn-text">Install 
gunzip, deal with Windows path-names, the windows shell and quoting.</span></sup>

<p>The library becomes unavoidable if we consider compressed network 
communication. Here we get the stream from <a name="idx:tcpopensocket3:2"></a><span class="pred-ext">tcp_open_socket/3</span>. 
The library provides efficient creation of a compressed stream, as well 
as support for flushing output through the standard Prolog <a name="idx:flushoutput1:3"></a><span class="pred-ext">flush_output/1</span> 
call.

<p><h2 id="sec:zlib-predicates"><a name="sec:2"><span class="sec-nr">2</span> <span class="sec-title">Predicate 
reference</span></a></h2>

<a name="sec:zlib-predicates"></a>

<dl class="latex">
<dt class="pubdef"><a name="zopen/3"><strong>zopen</strong>(<var>+Stream, 
-ZStream, +Options</var>)</a></dt>
<dd class="defbody">
Creates <var>ZStream</var>, providing compressed access to <var>Stream</var>. 
If an input stream is wrapped, it recognises a gzip or deflate header. 
If an output stream is wrapped, <var>Options</var> define the desired 
wrapper and compression level. The new <var>ZStream</var> inherits its
<em>encoding</em> from <var>Stream</var>. In other words, if <var>Stream</var> 
is a text-stream, so is <var>ZStream</var>. The original <var>Stream</var> 
is switched to binary mode while it is wrapped. The original encoding of
<var>Stream</var> is restored if <var>ZStream</var> is closed. Note that <a name="idx:zopen3:4"></a><a class="pred" href="#zopen/3">zopen/3</a> 
does not actually process any data and therefore succeeds on input 
streams that do not contain valid data. Errors may be generated by read 
operations performed on the stream.

<p>Defined options on output streams are:

<dl class="latex">
<dt><strong>format</strong>(<var>+Format</var>)</dt>
<dd class="defbody">
Either <code>deflate</code> (default) or <code>gzip</code>. The <code>deflate</code> 
envelope is simple and short and is typically used for compressed 
(network) communication. The <code>gzip</code> envelope is compatible to 
the <b>gzip</b> program and intended to read/write compressed files.</dd>
<dt><strong>level</strong>(<var>+Level</var>)</dt>
<dd class="defbody">
Number between 0 and 9, specifying the compression level, Higher levels 
use more resources. Default is 6, generally believed to be a good 
compromise between speed, memory requirement and compression.</dd>
<dt><strong>multi_part</strong>(<var>+Boolean</var>)</dt>
<dd class="defbody">
If <code>true</code>, restart reading if the input is not at 
end-of-file. The default is <code>true</code> for gzip streams.
</dd>
</dl>

<p>Generic options are:

<dl class="latex">
<dt><strong>close_parent</strong>(<var>Bool</var>)</dt>
<dd class="defbody">
If <code>true</code> (default), closing the compressed stream also 
closes (and thus invalidates) the wrapped stream. If <code>false</code>, 
the wrapped stream is <em>not</em> closed. This can be used to 
read/write a compressed data block as partial input/output on a stream.
</dd>
</dl>

</dd>
<dt class="pubdef"><a name="gzopen/3"><strong>gzopen</strong>(<var>+File, 
+Mode, -Stream</var>)</a></dt>
<dd class="defbody">
Same as <code>gzopen(File, Mode, Stream,[])</code>.</dd>
<dt class="pubdef"><a name="gzopen/4"><strong>gzopen</strong>(<var>+File, 
+Mode, -Stream, +Options</var>)</a></dt>
<dd class="defbody">
Open <b>gzip</b> compatible <var>File</var> for reading or writing. If a 
file is opened in =append= mode, a new gzip image will be added to the 
end of the file. The gzip standard defines that a file can hold multiple 
gzip images and inflating the file results in a concatenated stream of 
all inflated images. Options are passed to <a name="idx:open4:5"></a><span class="pred-ext">open/4</span> 
and <a name="idx:zopen3:6"></a><a class="pred" href="#zopen/3">zopen/3</a>. 
Default format is
<code>gzip</code>.
</dd>
</dl>

<p><h2 id="sec:zlib-streams"><a name="sec:3"><span class="sec-nr">3</span> <span class="sec-title">Interaction 
with Prolog stream predicates</span></a></h2>

<a name="sec:zlib-streams"></a>

<p>Using <a name="idx:flushoutput1:7"></a><span class="pred-ext">flush_output/1</span> 
on a compressed stream causes a
<code>Z_SYNC_FLUSH</code> on the stream. Using <a name="idx:close1:8"></a><span class="pred-ext">close/1</span> 
on a compressed stream causes a <code>Z_FINISH</code> on the stream. If 
the stream uses the <code>gzip</code> format, a <b>gzip</b> compatible 
footer is written to the stream. If <code>close_parent</code> is set 
(default) the underlying stream is closed too. Otherwise it remains open 
and the user can continue communication in non-compressed format or 
reopen the stream for compression using <a name="idx:zopen3:9"></a><a class="pred" href="#zopen/3">zopen/3</a>.

<h1><a name="document-index">Index</a></h1>

<dl>
<dt class="index-sep">?</dt>
<dt>close/1</dt>
<dd>
<a class="idx" href="#idx:close1:8">3</a></dd>
<dt>flush_output/1</dt>
<dd>
<a class="idx" href="#idx:flushoutput1:3">1</a> <a class="idx" href="#idx:flushoutput1:7">3</a></dd>
<dt><a class="idx" href="#gzopen/3">gzopen/3</a></dt>
<dt><a class="idx" href="#gzopen/4">gzopen/4</a></dt>
<dt>open/4</dt>
<dd>
<a class="idx" href="#idx:open4:5">2</a></dd>
<dt>tcp_open_socket/3</dt>
<dd>
<a class="idx" href="#idx:tcpopensocket3:2">1</a></dd>
<dt><a class="idx" href="#zopen/3">zopen/3</a></dt>
<dd>
<a class="idx" href="#idx:zopen3:1">1</a> <a class="idx" href="#idx:zopen3:4">2</a> <a class="idx" href="#idx:zopen3:6">2</a> <a class="idx" href="#idx:zopen3:9">3</a></dd>
</dl>

</body></html>