gzip.ss
#lang scheme/base
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; FILE.plt - file utilities 
;;
;;
;; Bonzai Lab, LLC.  All rights reserved.
;;
;; Licensed under LGPL.
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; gzip.ss - port utilities for gzipped ports and files.
;; yc 9/21/2009 - first version
;; yc 3/7/2010 - move from port.plt to file.plt

(require file/gunzip file/gzip 
         scheme/file 
         scheme/system
         "depend.ss"
         (rename-in file/gzip (gzip-through-ports input-gzip-filter))
         (rename-in file/gunzip 
                    (gunzip-through-ports input-gunzip-filter)
                    (gunzip-through-ports output-gunzip-filter))
         )

(define (gzip/exe)
  (find-executable-path (+:windows "gzip.exe" "gzip")))

(define (gzip/sys path)
  (system* (gzip/exe) path))

(define (gunzip/sys path)
  (system* (gzip/exe) "-d" path))

(define (gzipped-path path)
  (string->path (string-append (if (path? path) (path->string path) path) ".gz")))

(define (open-input-gzip-port in)
  (make-input-filter-port in input-gzip-filter #f))

(define (open-input-gunzip-port in)
  (make-input-filter-port in input-gunzip-filter #f))

(define (output-gzip-filter in out)
  (gzip-through-ports in out #f (current-seconds)))

(define (open-output-gzip-port out)
  (make-output-filter-port out output-gzip-filter #f))

(define (open-output-gunzip-port out)
  (make-output-filter-port out output-gunzip-filter #f))

(define (open-input-deflate-port in) 
  (make-input-filter-port in deflate #f)) 

(define (open-input-inflate-port in) 
  (make-input-filter-port in inflate #f)) 

(define (open-output-deflate-port out) 
  (make-output-filter-port out deflate #f)) 

(define (open-output-inflate-port out) 
  (make-output-filter-port out inflate #f)) 

(provide/contract 
 (gzip/sys (-> path-string? any))
 (gunzip/sys (-> path-string? any))
 (open-input-gzip-port (-> input-port? input-port?))
 (open-input-gunzip-port (-> input-port? input-port?))
 (open-output-gzip-port (-> output-port? output-port?)) 
 (open-output-gunzip-port (-> output-port? output-port?))
 (gzipped-path (-> path-string? path-string?))
 (output-gzip-filter port-filter/c)
 (open-input-deflate-port (-> input-port? input-port?)) 
 (open-input-inflate-port (-> input-port? input-port?)) 
 (open-output-deflate-port (-> output-port? output-port?)) 
 (open-output-inflate-port (-> output-port? output-port?)) 
 )

(provide input-gzip-filter
         input-gunzip-filter 
         output-gunzip-filter
         inflate 
         deflate
         )