Java has a stock of ZipOutputStream that can be used from Clojure. I do not know if there is a library anywhere. I use simple Java functions with a little helper macro:
(defmacro ^:private with-entry [zip entry-name & body] `(let [^ZipOutputStream zip# ~zip] (.putNextEntry zip# (ZipEntry. ~entry-name)) ~@body (flush) (.closeEntry zip#)))
Obviously, each ZIP entry describes a file.
(require '[clojure.java.io :as io]) (with-open [file (io/output-stream "foo.zip") zip (ZipOutputStream. file) wrt (io/writer zip)] (binding [*out* wrt] (doto zip (with-entry "foo.txt" (println "foo")) (with-entry "bar/baz.txt" (println "baz")))))
To pin a file, you can do something like this:
(with-open [output (ZipOutputStream. (io/output-stream "foo.zip")) input (io/input-stream "foo")] (with-entry output "foo" (io/copy input output)))
source share