Created
July 25, 2016 21:51
-
-
Save mangosmoothie/36ff1141901e0608e7099cc3d9087eb4 to your computer and use it in GitHub Desktop.
clojure untar
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 untar | |
(:import (org.apache.commons.compress.archivers.tar TarArchiveInputStream) | |
(java.io File) | |
(java.io FileOutputStream) | |
(java.io FileInputStream))) | |
(defn untar-file | |
"untar file and save to location - preserving filenames" | |
[zipfile outdirpath] | |
(let [buffer (byte-array 1024) | |
outdir (File. outdirpath)] | |
(.mkdir outdir) | |
(println "untarring " (.getPath zipfile)) | |
(with-open [zis (TarArchiveInputStream. (FileInputStream. zipfile))] | |
(loop [e (.getNextEntry zis)] | |
(if e | |
(let [filename (.getName e) | |
outfile (File. (str outdir (File/separator) filename))] | |
(if (.isDirectory e) | |
(.mkdirs outfile) | |
(do | |
(.mkdirs (File. (.getParent outfile))) | |
(with-open [outstream (FileOutputStream. outfile)] | |
(loop [len (.read zis buffer)] | |
(if (< 0 len) | |
(do | |
(.write outstream buffer 0 len) | |
(recur (.read zis buffer)))))))) | |
(recur (.getNextEntry zis)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment