Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created September 3, 2012 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcromartie/3610244 to your computer and use it in GitHub Desktop.
Save jcromartie/3610244 to your computer and use it in GitHub Desktop.
(ns
^{:author "John Cromartie"
:doc "A library for reading and writing gzip-compressed data"}
cljz.core
(:refer-clojure :exclue [spit slurp])
(require [clojure.java.io :as io])
(import [java.util.zip GZIPInputStream GZIPOutputStream]))
(defn input-stream
[x & opts]
(GZIPInputStream. (apply io/input-stream x opts)))
(defn output-stream
[x & opts]
(GZIPOutputStream. (apply io/output-stream x opts)))
(defn reader
[x & opts]
(apply io/reader (input-stream x) opts))
(defn writer
[x & opts]
(apply io/writer (output-stream x) opts))
(defn spit
[f content]
(with-open [w (writer f)]
(.write w (str content))))
(defn slurp
[f]
(with-open [r (reader f)]
(let [sb (StringBuilder.)]
(loop [c (.read r)]
(if (neg? c)
(str sb)
(do
(.append sb (char c))
(recur (.read r))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment