How to download a file and unzip it from memory in clojure?

前端 未结 2 2069
名媛妹妹
名媛妹妹 2021-02-14 01:12

I\'m making a GET request using clj-http and the response is a zip file. The contents of this zip is always one CSV file. I want to save the CSV file to disk, but I can\'t figur

2条回答
  •  广开言路
    2021-02-14 01:49

    You can use the pure java java.util.zip.ZipInputStream or java.util.zip.GZIPInputStream. Depends how the content is zipped. This is the code that saves your file using java.util.zip.GZIPInputStream :

    (->
      (client/get "http://localhost:8000/blah.zip" {:as :byte-array})
      (:body)
      (io/input-stream)
      (java.util.zip.GZIPInputStream.)
      (clojure.java.io/copy (clojure.java.io/file "/path/to/output/file")))
    

    Using java.util.zip.ZipInputStream makes it only a bit more complicated :

    (let [stream (->
                    (client/get "http://localhost:8000/blah.zip" {:as :byte-array})
                    (:body)
                    (io/input-stream)
                    (java.util.zip.ZipInputStream.))]
          (.getNextEntry stream)
          (clojure.java.io/copy stream (clojure.java.io/file "/path/to/output/file")))
    

提交回复
热议问题