处理文件 / 归档

(require '[clojure.java.io :as io])

;; the archive to unpack, made here so the example runs on its own
(with-open [zip (java.util.zip.ZipOutputStream. (io/output-stream "data.zip"))]
  (doseq [[nm text] {"one.txt" "one" "sub/two.txt" "two"}]
    (.putNextEntry zip (java.util.zip.ZipEntry. nm))
    (io/copy text zip)
    (.closeEntry zip)))

;; ZipInputStream hands the entries over one by one, and the stream itself is
;; the content of the current entry — copy it out, slurp would close the stream
(with-open [zip (java.util.zip.ZipInputStream. (io/input-stream "data.zip"))]
  (loop [entry (.getNextEntry zip)]
    (when entry
      (when-not (.isDirectory entry)
        (let [target (io/file "tmp" (.getName entry))]
          (io/make-parents target)
          (io/copy zip target)
          (println "unpacked" (.getPath target))))
      (recur (.getNextEntry zip)))))