Skip to content

Instantly share code, notes, and snippets.

@AhrazA
Created January 23, 2024 20:38
Show Gist options
  • Save AhrazA/4dafcb7c4eea7643f9a17f7e04edb694 to your computer and use it in GitHub Desktop.
Save AhrazA/4dafcb7c4eea7643f9a17f7e04edb694 to your computer and use it in GitHub Desktop.
Lazy zip file reading in clojure
(require '(clojure.java [io :as io]))
(defn zip-file-entries [filepath]
(let [is (io/input-stream filepath)
zin (java.util.zip.ZipInputStream. is)]
((fn proc-entry [zin]
(if-let [entry (.getNextEntry zin)]
(if (not (.isDirectory entry))
(do
(let [in (java.io.BufferedInputStream. zin)
out (java.io.ByteArrayOutputStream.)]
(io/copy in out)
(.closeEntry zin)
(lazy-seq (cons {:entry entry :contents (.toByteArray out)} (proc-entry zin)))))
(lazy-seq (proc-entry zin)))
(do
(println "Closing")
(.close is)
(.close zin)))) zin)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment