Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Clojure中解压缩zlib流_Java_Clojure_Compression_Gzip_Deflate - Fatal编程技术网

Java 在Clojure中解压缩zlib流

Java 在Clojure中解压缩zlib流,java,clojure,compression,gzip,deflate,Java,Clojure,Compression,Gzip,Deflate,我有一个二进制文件,其中的内容是由Python上的zlib.compress创建的,有没有一种简单的方法可以在Clojure中打开和解压缩它 import zlib import json with open('data.json.zlib', 'wb') as f: f.write(zlib.compress(json.dumps(data).encode('utf-8'))) 基本上,它不是gzip文件,它只是表示数据的字节 我只能找到这些参考资料,但不完全是我想要的(我认为前两

我有一个二进制文件,其中的内容是由Python上的
zlib.compress
创建的,有没有一种简单的方法可以在Clojure中打开和解压缩它

import zlib
import json

with open('data.json.zlib', 'wb') as f:
    f.write(zlib.compress(json.dumps(data).encode('utf-8')))
基本上,它不是gzip文件,它只是表示数据的字节

我只能找到这些参考资料,但不完全是我想要的(我认为前两个是最相关的):

我真的必须将这个多行包装器实现到
java.util.zip
,还是有一个很好的库?实际上,我甚至不确定这些字节流是否跨库兼容,或者我只是在尝试混合和匹配错误的lib

Python中的步骤:

>>> '{"hello": "world"}'.encode('utf-8')
b'{"hello": "world"}'
>>> zlib.compress(b'{"hello": "world"}')
b'x\x9c\xabV\xcaH\xcd\xc9\xc9W\xb2RP*\xcf/\xcaIQ\xaa\x05\x009\x99\x06\x17'
>>> [int(i) for i in zlib.compress(b'{"hello": "world"}')]
[120, 156, 171, 86, 202, 72, 205, 201, 201, 87, 178, 82, 80, 42, 207, 47, 202, 73, 81, 170, 5, 0, 57, 153, 6, 23]
>>> import numpy
>>> [numpy.int8(i) for i in zlib.compress(b'{"hello": "world"}')]
[120, -100, -85, 86, -54, 72, -51, -55, -55, 87, -78, 82, 80, 42, -49, 47, -54, 73, 81, -86, 5, 0, 57, -103, 6, 23]
>>> zlib.decompress(bytes([120, 156, 171, 86, 202, 72, 205, 201, 201, 87, 178, 82, 80, 42, 207, 47, 202, 73, 81, 170, 5, 0, 57, 153, 6, 23])).decode('utf-8')
'{"hello": "world"}'
Clojure中的解码尝试:

; https://github.com/funcool/buddy-core/blob/master/src/buddy/util/deflate.clj#L40 without try-catch
(ns so.core
  (:import java.io.ByteArrayInputStream
           java.io.ByteArrayOutputStream
           java.util.zip.Deflater
           java.util.zip.DeflaterOutputStream
           java.util.zip.InflaterInputStream
           java.util.zip.Inflater
           java.util.zip.ZipException)
  (:gen-class))

(defn uncompress
  "Given a compressed data as byte-array, uncompress it and return as an other byte array."
  ([^bytes input] (uncompress input nil))
  ([^bytes input {:keys [nowrap buffer-size]
                  :or {nowrap true buffer-size 2048}
                  :as opts}]
   (let [buf  (byte-array (int buffer-size))
         os   (ByteArrayOutputStream.)
         inf  (Inflater. ^Boolean nowrap)]
     (with-open [is  (ByteArrayInputStream. input)
                 iis (InflaterInputStream. is inf)]
       (loop []
         (let [readed (.read iis buf)]
           (when (pos? readed)
             (.write os buf 0 readed)
             (recur)))))
     (.toByteArray os))))

(uncompress (byte-array [120, -100, -85, 86, -54, 72, -51, -55, -55, 87, -78, 82, 80, 42, -49, 47, -54, 73, 81, -86, 5, 0, 57, -103, 6, 23]))
ZipException invalid stored block lengths  java.util.zip.InflaterInputStream.read (InflaterInputStream.java:164)

任何帮助都将不胜感激。我不想使用zip或gzip文件,因为我只关心原始内容,而不关心此上下文中的文件名或修改日期。但是,如果它是唯一的选项,则可以在Python端使用其他压缩算法。

这里有一个使用gzip的简单方法:

Python代码:

import gzip
content = "the quick brown fox"
with gzip.open('fox.txt.gz', 'wb') as f:
    f.write(content)
import zlib
fp = open( 'balloon.txt.z', 'wb' )
fp.write( zlib.compress( 'the big red baloon' ))
fp.close()
Clojure代码:

(with-open [in (java.util.zip.GZIPInputStream.
                (clojure.java.io/input-stream
                 "fox.txt.gz"))]
  (println "result:" (slurp in)))

;=>  result: the quick brown fox
(with-open [in (java.util.zip.InflaterInputStream.
                (clojure.java.io/input-stream
                 "balloon.txt.z"))]
  (println "result:" (slurp in)))

;=> result: the big red baloon
请记住,“gzip”是一种算法和格式,并不意味着您需要使用“gzip”命令行工具

请注意,Clojure的输入不必是文件。您可以通过套接字将gzip压缩数据作为原始字节发送,并且仍然可以在Clojure端对其进行解压缩。详情请浏览:

更新 如果需要使用纯
zlib
格式而不是
gzip
,结果非常相似:

Python代码:

import gzip
content = "the quick brown fox"
with gzip.open('fox.txt.gz', 'wb') as f:
    f.write(content)
import zlib
fp = open( 'balloon.txt.z', 'wb' )
fp.write( zlib.compress( 'the big red baloon' ))
fp.close()
Clojure代码:

(with-open [in (java.util.zip.GZIPInputStream.
                (clojure.java.io/input-stream
                 "fox.txt.gz"))]
  (println "result:" (slurp in)))

;=>  result: the quick brown fox
(with-open [in (java.util.zip.InflaterInputStream.
                (clojure.java.io/input-stream
                 "balloon.txt.z"))]
  (println "result:" (slurp in)))

;=> result: the big red baloon

虽然OP生成的是zlib流,而不是gzip流,所以您需要使用充气器类来代替。感谢gzip示例,尽管我希望解压缩
(字节数组[120,-100,-85,86,…])
格式化数据(不确定
GZIPInputStream
是否可以处理它)。我现有的所有Python代码都使用
zlib
,如果坚持使用它,并且在我打算与Clojure交互的项目上不必使用
gzip
,那就太好了。哇,这太棒了,基本上是一行,谢谢!我对Clojure的信心恢复了;)这就是在JVM上运行的能力——您需要的所有库都已经为您编写好了!