Skip to content

Instantly share code, notes, and snippets.

@camdez
Created January 23, 2024 23:28
Show Gist options
  • Save camdez/eed2b76a2a132b09625a1b11abead473 to your computer and use it in GitHub Desktop.
Save camdez/eed2b76a2a132b09625a1b11abead473 to your computer and use it in GitHub Desktop.
(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