Skip to content

Instantly share code, notes, and snippets.

@Frozenlock
Created June 9, 2012 00:53
Show Gist options
  • Save Frozenlock/2898908 to your computer and use it in GitHub Desktop.
Save Frozenlock/2898908 to your computer and use it in GitHub Desktop.
Clojure: spit directly to zip
(defn spit-to-zip
"Put the content in filename and zip it into zip-filename. Accept
one or more filename/content."
[zip-filename filename content & others]
(with-open [zip (java.util.zip.ZipOutputStream.
(clojure.java.io/output-stream zip-filename))]
(let [add-entry (fn [name cont remain]
(-> zip (.putNextEntry (java.util.zip.ZipEntry. name)))
(doto (java.io.PrintStream. zip true)
(.println content))
(when (seq remain)
(recur (first remain) (second remain) (drop 2 remain))))]
(add-entry filename content others))))
@KGOH
Copy link

KGOH commented Mar 24, 2021

There's an error in the 10th line s/content/cont

(defn spit-to-zip
  "Put the content in filename and zip it into zip-filename. Accept
one or more filename/content."
  [zip-filename filename content & others]
  (with-open [zip (java.util.zip.ZipOutputStream.
                   (clojure.java.io/output-stream zip-filename))]
    (let [add-entry (fn [name cont remain] 
                      (-> zip (.putNextEntry (java.util.zip.ZipEntry. name)))
                      (doto (java.io.PrintStream. zip true)
                        (.println cont))
                      (when (seq remain)
                        (recur (first remain) (second remain) (drop 2 remain))))]
      (add-entry filename content others))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment