- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我有一个二进制文件,其内容由 zlib.compress
在 Python 上创建,有没有一种简单的方法可以在 Clojure 中打开和解压缩它?
import zlib
import json
with open('data.json.zlib', 'wb') as f:
f.write(zlib.compress(json.dumps(data).encode('utf-8')))
基本上它不是 gzip 文件,它只是表示 deflated 的字节数据。
我只能找到这些引用资料,但不是我要找的东西(我认为前两个最相关):
我真的必须将这个多行包装器实现到 java.util.zip
还是那里有一个不错的库?实际上,我什至不确定这些字节流是否跨库兼容,或者我是否只是在尝试混合和匹配错误的库。
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)
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
请记住,“gzip”是一种算法和格式,并不意味着您需要使用“gzip”命令行工具。
请注意,Clojure 的输入不一定是文件。您可以通过套接字将 gzip 压缩数据作为原始字节发送,并仍然在 Clojure 端对其进行解压缩。详情请见:https://clojuredocs.org/clojure.java.io/input-stream
如果您需要使用纯 zlib
格式而不是 gzip
,结果非常相似:
Python代码:
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.InflaterInputStream.
(clojure.java.io/input-stream
"balloon.txt.z"))]
(println "result:" (slurp in)))
;=> result: the big red baloon
关于java - 在 Clojure 中解压 zlib 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41959409/
起初,我只想在 python3.2 中使用 install feedparser,而它需要 Distribute。当我安装 Distribute 时 python3.2 setup.py instal
我正在尝试在另一台计算机上安装我的 Yesod Web 应用程序。 我已经在我当前的机器上很好地安装了它,并且可以cabal install它在那里没有任何问题。 我似乎在另一台机器上遇到了麻烦(这是
https://www.ietf.org/rfc/rfc1951.txt 的“3.2.7. 使用动态霍夫曼代码压缩(BTYPE=10)”部分描述了压缩期间使用的动态哈夫曼树的编码。可能出现在 DEFL
给定 Elixir 中代表压缩文件的二进制文件,我如何将它们传递到 Erlang 的 zlib 进行膨胀? compressed = > 我已经尝试过: z = :zlib.open() uncomp
我知道 zlib/miniz 提供了 compressBound,它根据纯文本大小返回压缩/压缩大小的上限。这很方便。 是否有用于返回膨胀/解压缩大小上限的膨胀函数(zlib/miniz)?还是一个简
我有一组存储在数据库中的 ZLIB 压缩/base64 编码字符串(在 C 程序中完成)。我编写了一个小型 PHP 页面,应该检索这些值并绘制它们(字符串最初是 float 列表)。 压缩/编码的 C
在https://www.rfc-editor.org/rfc/rfc1951 Note that in the "deflate" format, the Huffman codes for the
在https://www.rfc-editor.org/rfc/rfc1951 Note that in the "deflate" format, the Huffman codes for the
我正在处理处理较大文件的项目,在我们的代码库中,我们会返回寻找写入证书信息,这些寻找的范围大部分时间都非常小,我想在我的流写入器/读取器中使用 zlib为了节省磁盘空间,但由于这样的搜索我无法集成它,
我正在尝试使用以下命令升级 Node 版本:npm install npm@latest -g 命令。但它给出了 zlib 绑定(bind)关闭错误。 有办法解决这个问题吗? 最佳答案 你的 No
这个问题在这里已经有了答案: no module named zlib (9 个回答) 关闭 4 年前。 # pythonbrew venv create django1.5 Creating `d
本文整理了Java中io.gomint.server.jni.zlib.ZLib.process()方法的一些代码示例,展示了ZLib.process()的具体用法。这些代码示例主要来源于Github
本文整理了Java中io.gomint.server.jni.zlib.ZLib.init()方法的一些代码示例,展示了ZLib.init()的具体用法。这些代码示例主要来源于Github/Stack
我想使用 python zlib 压缩文本,并通过 Apache Thrift 发送压缩文本,最后我用 Java 解压了压缩文本。 但我不知道该怎么做。我找不到任何像 Java 中的 python z
是否有允许使用 Zlib 压缩数据的类,或者直接使用 zlib.dylib 是我唯一的可能吗? 最佳答案 NSData+Compression 是一个易于使用的 NSData 类别实现。 NSData
我使用 rvm 安装了 zlib 包和 ruby 1.9.3,但是每当我尝试安装时它说的 gem 无法加载此类文件--zlib 我用来安装的命令是 $ rvm install 1.9.3 $ rv
在 Django Design Patterns ,作者建议使用 zlib.crc32 来屏蔽 URL 中的主键。经过一些快速测试后,我注意到 crc32 大约有一半的时间会生成负整数,这似乎不适合在
我想以 ZLIB 格式在我的 C# 和 C++ 应用程序之间发送压缩数据。在 C++ 中,我使用 boost::iostreams 中可用的 zlib_compressor/zlib_decompre
我在 Python 和 C 中对 crc32 进行了一些试验,但我的结果不匹配。 C: #include #include #include #define NUM_BYTES 9 int ma
来自 ./configure --help: --with-zlib=DIR Include ZLIB support (requires zlib >= 1.0.9) --with-zlib-
我是一名优秀的程序员,十分优秀!