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

I am making a GET request using clj-http and the response is a zip file. The contents of this zip are always one CSV file. I want to save a CSV file to disk, but I cannot figure out how to do this.

If I have a file on disk, (fs/unzip filename destination)the Raynes / fs library works fine, but I can't figure out how I can force the clj-http response to something that it can read. If possible, I would like to unzip the file directly without

The closest I got (if it's even close) leads me to a BufferedInputStream, but I got lost from there.

(require '[clj-http.client :as client])
(require '[clojure.java.io :as io])

(->
  (client/get "http://localhost:8000/blah.zip" {:as :byte-array})
  (:body)
  (io/input-stream))
+4
source share
1 answer

java java.util.zip.ZipInputStream java.util.zip.GZIPInputStream. , . , 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")))

java.util.zip.ZipInputStream :

(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")))
+8

Source: https://habr.com/ru/post/1608687/


All Articles