2 ;;; Copyright (c) 2007 Zachary Beane, All Rights Reserved
4 ;;; Redistribution and use in source and binary forms, with or without
5 ;;; modification, are permitted provided that the following conditions
8 ;;; * Redistributions of source code must retain the above copyright
9 ;;; notice, this list of conditions and the following disclaimer.
11 ;;; * Redistributions in binary form must reproduce the above
12 ;;; copyright notice, this list of conditions and the following
13 ;;; disclaimer in the documentation and/or other materials
14 ;;; provided with the distribution.
16 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
17 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
20 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 (defun make-stream-output-callback (stream)
32 "Return a function suitable for use as a compressor callback that
33 writes all compressed data to STREAM."
35 (write-sequence buffer stream
:end end
)))
37 (defun gzip-stream (input output
)
38 (let ((callback (make-stream-output-callback output
))
39 (buffer (make-array 8192 :element-type
'(unsigned-byte 8))))
40 (with-compressor (compressor 'gzip-compressor
43 (let ((end (read-sequence buffer input
)))
46 (compress-octet-vector buffer compressor
:end end
))))))
48 (defun gzip-file (input output
&key
(if-exists :supersede
))
49 (with-open-file (istream input
:element-type
'(unsigned-byte 8))
50 (with-open-file (ostream output
51 :element-type
'(unsigned-byte 8)
54 (gzip-stream istream ostream
)))
57 (defun compress-data (data compressor-class
)
60 (with-compressor (compressor compressor-class
61 :callback
(lambda (buffer end
)
63 (push (subseq buffer
0 end
)
65 (salza2:compress-octet-vector data compressor
))
66 (let ((compressed (make-array size
:element-type
'(unsigned-byte 8)))
68 (dolist (chunk (nreverse chunks
))
69 (replace compressed chunk
:start1 start
)
70 (incf start
(length chunk
)))
73 (defun deflate-compress (data)
74 (compress-data data
'deflate-compressor
))
76 (defun zlib-compress (data)
77 (compress-data data
'zlib-compressor
))
79 (defun gzip-compress (data)
80 (compress-data data
'gzip-compressor
))