uncompressing tar.Z file with python?

删除回忆录丶 提交于 2019-12-30 11:37:08

问题


I need to write a python script that retrieves tar.Z files from an FTP server, and uncompress them on a windows machine. tar.Z, if I understood correctly is the result of a compress command in Unix.

Python doesn't seem to know how to handle these, it's not gz, nor bz2 or zip. Does anyone know a library that would handle these ?

Thanks in advance


回答1:


If GZIP -- the application -- can handle it, you have two choices.

  1. Try the Python gzip library. It may work.

  2. Use subprocess Popen to run gzip for you.

It may be an InstallShield .Z file. You may want to use InstallShield to unpack it and extract the .TAR file. Again, you may be able to use subprocess Popen to process the file.

It may also be a "LZW compressed file". Look at this library, it may help.

http://www.chilkatsoft.com/compression-python.asp




回答2:


Since you target a specific platform (Windows), the simplest solution may be to run gzip in a system call: http://www.gzip.org/#exe

Are there other requirements in your project that the decompression needs to be done in Python?




回答3:


A plain Python module that uncompresses is inexistant, AFAIK, but it's feasible to build one, given some knowledge:

  • the .Z format header specification
  • the .Z compression format

Almost all necessary information can be found the unarchiver CompressAlgorithm. Additional info from wikipedia for adaptive LZW and perhaps the compress man page.

Basically, you read the first three bytes (first two are magic bytes) to modify your algorithm, and then start reading and decompressing.

There's a lot of bit fiddling (.Z files begin having 9-bit tokens, up to 16-bit ones and then resetting the symbol table to the initial 256+2 values), which probably you'll deal with doing binary operations (&, <<= etc).



来源:https://stackoverflow.com/questions/2272199/uncompressing-tar-z-file-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!