Created
January 23, 2024 20:38
-
-
Save AhrazA/4dafcb7c4eea7643f9a17f7e04edb694 to your computer and use it in GitHub Desktop.
Lazy zip file reading in clojure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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