使用Python解压缩.Z文件 [英] Uncompressing a .Z file with Python

查看:416
本文介绍了使用Python解压缩.Z文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Python解压缩* .Z文件。我通过FTP(二进制模式)下载了它。该文件使用7zip成功解压缩(文件中的信息表示其类型为 Z)。原始文件位于 ftp://cddis.gsfc。 nasa.gov/gps/products/1860/igr18600.sp3.Z

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.

我已经阅读了Python中zlib模块的用法,并使用了一些测试代码:

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显然不喜欢标题。前两个字节似乎与压缩文件的正确魔术数字序列0x1F9d匹配(每个 https:// en .wikipedia.org / wiki / List_of_file_signatures )。

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).

在紧要关头,我可以通过直接掏空7zip来解决此问题。但是我希望找到一个纯Python类型的答案。尽管一天中大部分时间都在搜寻答案(或此错误消息),但我的运气并不好。也许我的搜索技能正在萎缩?

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?

推荐答案

Python在模块中没有等效的Unix uncompress,这是您需要解压缩的。 Z文件。您可能需要a)外壳到Unix compress命令,b)外壳到gzip,c)外壳到7-zip(gzip和7-zip都具有解压缩.Z文件的能力),d)修改在C中原始的解压缩代码并链接到Python(该代码可在线获得),或者e)在本机Python中编写自己的LZW解压缩器。

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.

对于d),您可以在mathematica.stackexchange.com 上的答案中找到我编写的一些C代码来。请参见 unlzw()函数。

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.

这篇关于使用Python解压缩.Z文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆