Created
August 3, 2012 18:18
-
-
Save vemv/3250136 to your computer and use it in GitHub Desktop.
file unzipper
This file contains hidden or 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 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