這裏是代碼從Java具體的答案端口:
(ns my-project.core
(:require [clojure.string :as cs])
(:import java.util.zip.ZipInputStream)
(:gen-class))
(defrecord HELPER [])
(defn get-code-location []
(when-let [src (.getCodeSource (.getProtectionDomain HELPER))]
(.getLocation src)))
(defn list-zip-contents [zip-location]
(with-open [zip-stream (ZipInputStream. (.openStream zip-location))]
(loop [dirs []]
(if-let [entry (.getNextEntry zip-stream)]
(recur (conj dirs (.getName entry)))
dirs))))
(defn -main [& args]
(println (some->> (get-code-location)
list-zip-contents
(filter #(cs/starts-with? % "a/")))))
被投入到主命名空間,並與罐子運行將輸出在/resources/a
文件夾中的所有路徑..
java -jar ./target/my-project-0.1.0-SNAPSHOT-standalone.jar
;;=> (a/ a/b/ a/b/222.txt a/222.txt)
另外一些快速的研究將我帶到這個圖書館: https://github.com/ronmamo/reflections
它縮短了代碼,但也需要一些依賴於項目(我想這可能是不可取的):
[org.reflections/reflections "0.9.11"]
[javax.servlet/servlet-api "2.5"]
[com.google.guava/guava "23.0"]
和代碼是這樣的:
(ns my-project.core
(:require [clojure.string :as cs])
(:import java.util.zip.ZipInputStream
[org.reflections
Reflections
scanners.ResourcesScanner
scanners.Scanner
util.ClasspathHelper
util.ConfigurationBuilder])
(:gen-class))
(defn -main [& args]
(let [conf (doto (ConfigurationBuilder.)
(.setScanners (into-array Scanner [(ResourcesScanner.)]))
(.setUrls (ClasspathHelper/forClassLoader (make-array ClassLoader 0))))]
(println
(filter #(cs/starts-with? % "a/")
(.getResources (Reflections. conf) #".*")))))
的[我如何列出一個JAR文件裏的文件可能的複製?](https://stackoverflow.com/questions/1429172/how-do-i-list-the-files-inside-a-jar-file) – leetwinski
這些都沒有顯示1)一個簡單的方法來做到這一點2)這是一個圖書館。我拒絕接受這個答案是一大堆java代碼。 – siltalau
好..它是一種簡單的任務,將java代碼翻譯成clojure。 – leetwinski