(defn extract-jar
  "Extracts jar-file into target-dir. jar-file can be a JarFile
  instance or a path to a jar file on disk. target-dir will be created
  if it doesn't exist."  [jar-file target-dir]
  (let [jar (if (isa? jar-file JarFile)
              jar-file
              (JarFile. jar-file true))
        entries (enumeration-seq (.entries jar))
        target-file #(str target-dir "/" (.getName %))
        bytes (make-array Byte/TYPE 1000)]
    ;; First make all the directories
    (doseq [entry entries :when (.isDirectory entry)]
      (.mkdirs (java.io.File. (target-file entry))))
    ;; Then write the files
    (doseq [entry entries :when (not (.isDirectory entry))]
      (with-open [in-stream (.getInputStream jar entry)
                  out-stream (java.io.FileOutputStream. (target-file entry))]
        (while (not= (.read in-stream bytes) -1)
          (.write out-stream bytes))))))

Generated by Phil Hagelberg using scpaste at Wed Apr 22 16:41:48 2009. PDT. (original)