Created
January 23, 2024 23:28
-
-
Save camdez/eed2b76a2a132b09625a1b11abead473 to your computer and use it in GitHub Desktop.
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
(ns zip-file-entries | |
(:require | |
[clojure.java.io :as io]) | |
(:import | |
(java.io BufferedInputStream ByteArrayOutputStream) | |
(java.util.zip ZipInputStream))) | |
(defn zip-file-seq [^ZipInputStream zin] | |
(when-let [entry (.getNextEntry zin)] | |
(if (.isDirectory entry) | |
(lazy-seq (zip-file-seq zin)) | |
(let [out (ByteArrayOutputStream.)] | |
(io/copy (BufferedInputStream. zin) out) | |
(.closeEntry zin) | |
(lazy-seq | |
(cons {:entry entry | |
:contents (.toByteArray out)} | |
(zip-file-seq zin))))))) | |
;;; Usage: | |
(with-open [zis (-> "foo.zip" io/input-stream ZipInputStream.)] | |
(mapv #(.getName (:entry %)) (zip-file-seq zis))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment