Разархивация части .gz файла с помощью Python

Кодирование Ужаса имеет большое сообщение [приблизительно 110], как настроить Подрывную деятельность в Windows.

Following учебное руководство, я смог получить Subervsion и TortoiseSVN, работающий локально, и я получил образование, в котором я нуждался из него.

, Насколько Мерзавец идет, это - вероятно, хорошая идея сделать руки на эксперимент с ними обоими, понять который соответствия Ваша определенная практика разработки.

8
задан rjmunro 27 October 2014 в 16:23
поделиться

2 ответа

I seems that you need to look into Python zlib library instead

The GZIP format relies on zlib, but introduces a file-level compression concept along with CRC checking, and this appears to be what you do not want/need at the moment.

See for example these code snippets from Dough Hellman

Edit: the code on Doubh Hellman's site only show how to compress or decompress with zlib. As indicated above, GZIP is "zlib with an envelope", and you'll need to decode the envellope before getting to the zlib-compressed data per se. Here's more info to go about it, it's really not that complicated:

  • see RFC 1952 for details about the GZIP format
  • This format starts with a 10 bytes header, followed by optional, non compressed elements such as the file name or a comment, followed by the zlib-compressed data, itself followed by a CRC-32 (precisely an "Adler32" CRC).
  • By using Python's struct module, parsing the header should be relatively simple
  • The zlib sequence (or its first few thousand bytes, since that is what you want to do) can then be decompressed with python's zlib module, as shown in the examples above
  • Possible problems to handle: if there are more than one file in the GZip archive, and if the second file starts within the block of a few thousand bytes we wish to decompress.

Sorry to provide neither an simple procedure nor a ready-to-go snippet, however decoding the file with the indication above should be relatively quick and simple.

12
ответ дан 5 December 2019 в 04:58
поделиться

Я не вижу никакой возможной причины, по которой вы хотели бы распаковать первые 2000 сжатых байтов. В зависимости от данных, это может быть распаковано до любого количества выходных байтов.

Конечно, вы хотите распаковать файл и остановиться, когда вы распаковали столько файла, сколько вам нужно, что-то вроде:

f = gzip.GzipFile(fileobj=open('postcode-code.tar.gz', 'rb'))
data = f.read(4000)
print data

AFAIK, this не приведет к чтению всего файла. Он будет читать ровно столько, сколько необходимо для получения первых 4000 байт.

10
ответ дан 5 December 2019 в 04:58
поделиться
Другие вопросы по тегам:

Похожие вопросы: