Skip to content

Instantly share code, notes, and snippets.

@vemv
Created August 3, 2012 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vemv/3250136 to your computer and use it in GitHub Desktop.
Save vemv/3250136 to your computer and use it in GitHub Desktop.
file unzipper
(ns vemv
(:require [clojure.java.io :as io]
[clojure.string :as string])
(:import [java.util.zip ZipInputStream]
[java.io FileOutputStream]))
(defmacro while-let
"The composition of a side-effects based while, and a single-binding let, ala if-let.\n\nAvoids loop/recur redundance."
[[sym expr] & body]
`(loop [~sym ~expr]
(when ~sym
~@body
(recur ~expr))))
(defn -main [[path file]]
(let [zis (ZipInputStream. (io/input-stream (str path file)))]
(while-let [entry (.getNextEntry zis)]
(let [size (.getSize entry)
bytes (byte-array size) ; XXX there should be a max size
output (FileOutputStream. (str path (last (string/split (.getName entry) #"/"))))]
(println (.getName entry))
(.read zis bytes 0 size) ; mutate bytes
(doto output (.write bytes) .close)))))
(-main ["/media/Media/" "example.zip"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment