On 10/10/13 7:21 PM, Brian Craft wrote:
I'm struggling with how to gzip/gunzip byte arrays from clojure. I don't really get how clojure.java.io streams supposed to be used.

This appears to gzip a string:

(let [baos (java.io.ByteArrayOutputStream.)
      gzos (java.util.zip.GZIPOutputStream. baos)]
    (.write gzos (.getBytes "foo"))
    (.finish gzos)
    (.toByteArray baos))


and I can expand on this to gunzip the string afterwards:

(let [baos (java.io.ByteArrayOutputStream.)
      gzos (java.util.zip.GZIPOutputStream. baos)]
  (.write gzos (.getBytes "foo"))
  (.finish gzos)
  (let [gzbytes (.toByteArray baos)
        bais (java.io.ByteArrayInputStream. gzbytes)
        gzis (java.util.zip.GZIPInputStream. bais)]
    (slurp gzis)))

But slurp builds strings. I need to do this with byte arrays. Do I have to rewrite slurp to build a byte array? Is there any simpler way to do this?

Zach Tellman's byte-streams library is very handy for these kinds of conversions:

https://github.com/ztellman/byte-streams

-Ben

--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to