使用Python取消压缩a .z文件

发布于 2025-01-25 21:52:09 字数 1108 浏览 5 评论 0 原文

我正在尝试使用Python解开 *.z文件。我通过FTP(二进制模式)下载了它。该文件成功地使用7ZIP取消压缩(文件上的“ info”表示它是类型为“ z”)。可以在

我已经阅读了在Python中使用ZLIB模块的使用,并且正在使用一些测试代码:

import zlib

comp_data = open('C:\Temp\igr18600.sp3.Z', 'rb').read()

print(comp_data[0:10])

uncomp_data = zlib.decompress(comp_data)
with open('c:\temp\igr18600.sp3', 'wb') as f:
    f.write(uncomp_data)
    f.close()

执行此操作时,我将获得以下输出:

b'\x1f\x9d\x90#\xc6@\x91\x01#F'
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    uncomp_data = zlib.decompress(comp_data)
zlib.error: Error -3 while decompressing data: incorrect header check

Z​​lib显然不喜欢标头。前几个字节似乎与压缩文件的正确魔法编号序列0x1f9d匹配(per )。

在紧要关头,我可以通过直接向7zip弹壳来解决这个问题。但是我希望找到一种纯粹的Python类型的答案。尽管大部分时间都在谷歌搜索答案(或此错误消息),但我并没有太多运气。也许我的搜索技能萎缩了?

I'm trying to uncompress a *.Z file using Python. I downloaded it via FTP (binary mode). The file successfully uncompresses with 7zip (whose "info" on the file says it's of type "Z"). The original file can be found at ftp://cddis.gsfc.nasa.gov/gps/products/1860/igr18600.sp3.Z.

I've read up on the use of the zlib module in Python and have some test code I'm using:

import zlib

comp_data = open('C:\Temp\igr18600.sp3.Z', 'rb').read()

print(comp_data[0:10])

uncomp_data = zlib.decompress(comp_data)
with open('c:\temp\igr18600.sp3', 'wb') as f:
    f.write(uncomp_data)
    f.close()

When I execute this I get the following output:

b'\x1f\x9d\x90#\xc6@\x91\x01#F'
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    uncomp_data = zlib.decompress(comp_data)
zlib.error: Error -3 while decompressing data: incorrect header check

zlib clearly doesn't like the header. The first couple of bytes appear to match the proper magic number sequence 0x1F9d for a compressed file (per https://en.wikipedia.org/wiki/List_of_file_signatures).

In a pinch I can work around this by shelling out to 7zip directly. But I was hoping to find a pure Python type of answer. Despite spending most of the day googling for an answer (or this error message) I haven't had much luck. Perhaps my search skills are atrophying?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

拥有 2025-02-01 21:52:09

Python在模块中没有可用的Unix Uncompress等效,这是您需要解压缩一个.Z文件的内容。您要么需要a)弹出到unix compress命令,b)shell to Gzip,c)shell to 7-zip(gzip和7-zip都具有解压缩.z文件的能力),d)修改C中的原始Uncompress代码并将其链接到Python(该代码可在线提供),或者e)在本机Python中编写自己的LZW解压缩器。

对于d),您可以找到我写的一些C代码来完成此工作在Mathematica.stackexkexchange.com 。请参阅 unlzw()函数。

Python does not have the equivalent of Unix uncompress available in a module, which is what you'd need to decompress a .Z file. You would either need to a) shell out to the Unix compress command, b) shell out to gzip, c) shell out to 7-zip (both gzip and 7-zip have the ability to decompress .Z files), d) modify the original uncompress code in C and link that to Python (the code is available online), or e) write your own LZW decompressor in native Python.

For d), you can find some C code I wrote to do this job in this answer on mathematica.stackexchange.com. See the unlzw() function.

半城柳色半声笛 2025-02-01 21:52:09

几年过去了,现在有python包: lunlzw unlzw3 [1]

1)unlzw

  • github: ionelmc/python-unlzw (上次提交2017-10)
  • pypi: lunlzw
  • docs: python-unlzw.readthedocs.io
  • pros/cons:该软件包提供 c-扩展文件(.so或.pyd),这使其快速。不利的一面是它仅构建了最多的cpython 3.6(因为它是不保留?),如果您将其安装在Windows上,PIP将下载 .tar.gz ,并且必须安装C型补充器才能使安装正常工作。我尝试过,并且由于安装了C型补充器,因此可以与Cpython 3.8.6一起使用。

1.1)安装*

pip install unlzw

**请参阅上面的ProS/CONS上的注释。

1.2)示例用法

from unlzw import unlzw

with open('file.Z', 'rb') as fh:
    compressed_data = fh.read()
    uncompressed_data = unlzw(compressed_data)

2)unlzw3

  • github: scivision/unlzw3 (最后一个提交2020-07)。基于 github.com/umeat/unlzw
  • pypi: lunlzw3
  • pros/cons:纯Python实现,这意味着它将安装&amp;在带有python的“任何”平台上运行,包括没有C型补充器的Windows。它也比C实施慢(请参见下面的基准)。

2.1)安装

pip install unlzw3

2.2)示例使用

documentation

import unlzw3
from pathlib import Path

uncompressed_data = unlzw3.unlzw(Path('file.Z'))

附录:

使用文件 igsg1450。 20i.z

unlzw
3.5 ms ± 93 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

unlzw3
166 ms ± 2.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

基准测试表明,对于1000个类似的.z文件,您只需要使用UNLZW等待3.5秒,但使用UNLZW3等待2.8分钟。对于一次性转换,差异可能无关紧要。


[1]所有这些实际上均基于 Mark Adler的答案 Mathematica.stackexchange.com上

Some years have gone by, and now there are python packages for this: unlzw and unlzw3[1]

1) unlzw

  • GitHub: ionelmc/python-unlzw (Last commit 2017-10)
  • PyPI: unlzw
  • Docs: python-unlzw.readthedocs.io
  • Pros/Cons: The package provides a C-extension file (.so or .pyd) which makes it fast. The downside is that it has built versions only up to CPython 3.6 (as it is not maintained?), and if you install it on Windows, pip will download the .tar.gz, and you'll have to have a C-compiler installed to make the installation to work properly. I tried and since I have a C-compiler installed, got it working with CPython 3.8.6.

1.1) Installation*

pip install unlzw

*See the note on Pros/Cons above.

1.2) Example usage

from unlzw import unlzw

with open('file.Z', 'rb') as fh:
    compressed_data = fh.read()
    uncompressed_data = unlzw(compressed_data)

2) unlzw3

  • GitHub: scivision/unlzw3 (Last commit 2020-07). Based on github.com/umeat/unlzw
  • PyPI: unlzw3
  • Pros/Cons: Pure python implementation, meaning that it will install & run on "any" platform with python, including Windows without C-compiler. It is also slower than the C-implementation (see benchmarks below).

2.1) Installation

pip install unlzw3

2.2) Example usage

From the documentation:

import unlzw3
from pathlib import Path

uncompressed_data = unlzw3.unlzw(Path('file.Z'))

Appendix: Benchmarks

Using a file igsg1450.20i.Z downloaded from https://cddis.nasa.gov/archive/gnss/products/ionex/2020/145/.

unlzw
3.5 ms ± 93 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

unlzw3
166 ms ± 2.7 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

The benchmarks show, that for 1000 similar .Z files you would have to wait only 3.5 seconds with unlzw, but 2.8 minutes with unlzw3. For one-off conversion, the difference probably does not matter.


[1] All of these are actually based on the Mark Adler's answer on mathematica.stackexchange.com

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文