Zlib как распаковать linux

Обновлено: 05.07.2024

/test
Убедимся, что файл создан
$ cat

/test
Т Е С Т
упакуем этот файл, например, архиватором gzip
$ gzip

/test
и посмотрим на упакованный файл утилитой hexdump, точнее посмотрим 1-ые 16 байт
$ hexdump -C -n 16

/test.gz
00000000 1f 8b 08 08 9e 52 18 59 00 03 74 65 73 74 00 bb |. R.Y..test..|
Первые 3 байта — это так называемая сигнатура (HEX код) архиватора gzip - 1F 8B 08, которая является составной частью упакованного файла (его началом) и присуща только архиватору gzip, что позволяет отличить этот файл от других и, главное, видно, что архиватор, точнее сигнатура начинается с самого начала (с нулевого байта, смещение равно 0).
UPD - если быть точным, то сама сигнатура два байта (0x1f, 0x8b), а 3-ий байт определяет compression method (08 - deflate)
При распаковке этого файла, утилита gzip сначала проверит наличие сигнатуры, присущей gzip, и только после этого примет решение о распаковке.
Проверим это, а для чего изменим сигнатуру файла, точнее, изменим 1-ый байт — вместо 1F запишем 1E (я проделал это в hex редакторе)
$ hexdump -C -n 16

/test.gz
00000000 1e 8b 08 08 9e 52 18 59 00 03 74 65 73 74 00 bb |. R.Y..test..|
Ну и попытаемся рапаковать
$ gzip -d

/test.gz: not in gzip format
И то же самое будет, если сигнатура будет правильная, но начинаться будет не с начала файла (смещение не равно 0) — вернем все на место, но перед сигнатурой запишем 1 байт — 00 (я сохранил этот файл, как test1.gz).
$ hexdump -C -n 16

/test1.gz
00000000 00 1f 8b 08 08 9e 52 18 59 00 03 74 65 73 74 00 |. R.Y..test.|
$ gzip -d

/test1.gz: not in gzip format
И значит распаковывать такие файлы нужно по-другому, а именно, нужно указать при распаковке смещение, откуда начинать, т.е. где начинается сигнатура.
Самый простой вариант — удалить байты до сигнатуры, но это не этично, да и иногда это выливается в мегабайты и десятки мегабайт.
Лучше поступить грамотно — указать утилите смещение, с которго начинать распаковку (делаем пропуск с помощью dd в 1 байт прежде чем начать распаковку)
$ dd if=

/test1.gz skip=1 bs=1 | gzip -d > test1
Проверим, что мы получили
$ cat

Теперь я хочу распаковать данные в оболочке. Ни zcat , ни uncompress работают:

Кажется, что я создал gzip-подобный файл, но без заголовков. К сожалению, я не вижу возможности распаковывать такие необработанные данные на странице gzip man, а пакет zlib не содержит исполняемой программы.

Есть ли утилита для распаковки необработанных данных zlib?

Редактирование:
@ d0sboots прокомментировал: для данных RAW Deflate вам нужно добавить еще 2 пустых байта:
â † ' "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00"

Пользователи @ Vitali-Kushner и @ mark-bessey сообщили об успехе даже с усеченными файлами, поэтому нижний колонтитул gzip не кажется строго обязательным.

Я нашел решение (одно из возможных), используя openssl :

* ПРИМЕЧАНИЕ. Функциональность zlib, по-видимому, доступна в последних версиях openssl> = 1.0.0 (OpenSSL должен быть сконфигурирован /построен с параметром zlib или zlib-dynamic, последний по умолчанию)

Пользователь @tino прокомментировал ниже ответ OpenSSL, но я думаю, что это должно быть отдельным:

Я попробовал это, и это сработало для меня.

zlib-flate можно найти в пакете qpdf (в Debian Squeeze и Fedora 23, согласно комментариям в других ответах)

Я рекомендую pigz от Марк Адлер , соавтор библиотеки сжатия zlib. Выполните pigz , чтобы увидеть доступные флаги.

-z --zlib Compress to zlib (.zz) instead of gzip format.

Вы можете распаковать, используя флаг -d :

-d --decompress --uncompress Decompress the compressed input.

Предполагая файл с именем 'test':

  • pigz -z test - создает сжатый файл zlib с именем test.zz
  • pigz -d -z test.zz - преобразует test.zz в распакованный тестовый файл

В OSX вы можете выполнить brew install pigz

zlib реализует сжатие, используемое gzip, но а не формат файла. Вместо этого вы должны использовать gzip модуль , который сам использует zlib .

Это может сделать это:

Затем запустите его следующим образом:

Пример программы zpipe.c найден здесь от Mark Сам Адлер (поставляется с исходным распределением библиотеки zlib) очень полезен для этих сценариев с необработанными данными zlib. Скомпилируйте с помощью cc -o zpipe zpipe.c -lz и выполните распаковку: zpipe -d < raw.zlib > decompressed . Он также может выполнять сжатие без флага -d .

Вы можете использовать это для сжатия с помощью zlib:

И это для дефляции:

В macOS, который является полностью совместимым с POSIX UNIX (официально сертифицированным!), OpenSSL не поддерживает zlib , нет zlib-flate

Вот решение на основе Perl, которое можно использовать в качестве однострочного интерфейса командной строки, получает его вход через STDIN-канал и работает из коробки со свеже установленным macOS:

I have created zlib-compressed data in Python, like this:

Now, I want to uncompress the data in shell. Neither zcat nor uncompress work:

It seems that I have created gzip-like file, but without any headers. Unfortunately I don't see any option to uncompress such raw data in gzip man page, and the zlib package does not contain any executable utility.

Is there a utility to uncompress raw zlib data?


61k 32 32 gold badges 94 94 silver badges 216 216 bronze badges 2,582 2 2 gold badges 16 16 silver badges 16 16 bronze badges

14 Answers 14

Edits:
@d0sboots commented: For RAW Deflate data, you need to add 2 more null bytes:
→ "\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00"

This Q on SO gives more information about this approach. An answer there suggests that there is also an 8 byte footer.

Users @Vitali-Kushner and @mark-bessey reported success even with truncated files, so a gzip footer does not seem strictly required.

@tobias-kienzler suggested this function for the bashrc:
zlibd() (printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" | cat - "$@" | gzip -dc)

1,924 1 1 gold badge 12 12 silver badges 3 3 bronze badges

I tried this and it worked for me.

zlib-flate can be found in package qpdf (in Debian Squeeze, Fedora 23, and brew on MacOS according to comments in other answers)

(Thanks to user @tino who provided this as a comment below the OpenSSL answer. Made into propper answer for easy access.)

1,530 1 1 gold badge 12 12 silver badges 18 18 bronze badges In contrast to the other answers, this one works on OS X. brew install qpdf, then the command listed above :-) thank you!

I have found a solution (one of the possible ones), it's using openssl:

*NOTE: zlib functionality is apparently available in recent openssl versions >=1.0.0 (OpenSSL has to be configured/built with zlib or zlib-dynamic option, the latter is default)

2,582 2 2 gold badges 16 16 silver badges 16 16 bronze badges On Debian Squeeze (which has OpenSSL 0.9.8) there is zlib-flate in the qpdf package. It can be used like zlib-flate -uncompress < FILE . zlib got removed from the latest versions of OpenSSL so this tip is is very helpful @Tino Thanks. This solution provides a better experience in decompressing short input files than the answer using "gzip" ("openssl" decompressed as much as it could while "gzip" aborted printing "unexpected end of file"). @Tino, it is also available via the package qpdf on Fedora 23. Alexandr Kurilin, zlib is still available in 1.0.2d-fips.

I recommend pigz from Mark Adler, co-author of the zlib compression library. Execute pigz to see the available flags.

You will notice:

-z --zlib Compress to zlib (.zz) instead of gzip format.

You can uncompress using the -d flag:

-d --decompress --uncompress Decompress the compressed input.

Assuming a file named 'test':

  • pigz -z test - creates a zlib compressed file named test.zz
  • pigz -d -z test.zz - converts test.zz to the decompressed test file

On OSX you can execute brew install pigz

Good find! It looks like it can detect zlib files by itself, so unpigz test.zz will work as well.

zlib implements the compression used by gzip, but not the file format. Instead, you should use the gzip module, which itself uses zlib .

45.5k 36 36 gold badges 168 168 silver badges 324 324 bronze badges ok, but my situation is that i have tens/hundreds thousands of those files created, so.. :) @mykhal, why did you create ten/hundred thousands of files before checking that you could actually uncompress them?

On macOS, which is a full POSIX compliant UNIX (formally certified!), OpenSSL has no zlib support, there is no zlib-flate either and while the first solution works as well as all the Python solutions, the first solution requires the ZIP data to be in a file and all the other solutions force you to create a Python script.

Here's a Perl based solution that can be used as a command line one-liner, gets its input via STDIN pipe and that works out of the box with a freshly installed macOS:

Nicer formatted, the Perl script looks like this:


This might do it:

Then run it like this:

The file that is created by expanded still checks out as "zlib compressed data" for me, using the shell file command? How is that?

The example program zpipe.c found here by Mark Adler himself (comes with the source distribution of the zlib library) is very useful for these scenarios with raw zlib data. Compile with cc -o zpipe zpipe.c -lz and to decompress: zpipe -d < raw.zlib > decompressed . It can also do the compression without the -d flag.

During development of eIDAS related code, i've came up with bash script, that decodes SSO (SingleSignOn) SAMLRequest param, which is usually encoded by base64 and raw-deflate (php gzdeflate)

You can use it like

You can use this to compress with zlib:

And this to deflate:


61.7k 9 9 gold badges 168 168 silver badges 264 264 bronze badges

I have an addition to @Alex Stragies conversion for those who need a proper header and footer (an actual conversion from zlib to gzip).

It would probably be easier to use one of the above methods, however if the reader has a case like mine which requires conversion of zlib to gzip without decompression and recompression, this is the way to do it.

According to RFC1950/1952, A zlib file can only have a single stream or member. This is different from gzip in that:

A gzip file consists of a series of "members" (compressed data sets). . The members simply appear one after another in the file, with no additional information before, between, or after them.

This means that while a single zlib file can always be converted to a single gzip file, the converse is not strictly true. Something to keep in mind.

zlib has both a header (2 bytes) and a footer (4 bytes) which must be removed from the data so that the gzip header and footer can be appended. One way of doing that is as follows:

Now we have just raw data and may append the gzip header (from @Alex Stragies)

The gzip footer is 8 bytes long. It consists the CRC32 of the uncompressed file, plus the size of the file uncompressed mod 2^32, both in big endian format. If you don't know these but have means of getting an uncompressed file:

Есть файл прошивки demo.bin Linux/MIPS, как его распаковать а потом заново собрать в bin? Необходимо добавить драйвер, возможности скопировать через флешку или по сети нет.


Ну а что говорит


Попробуй поставить пакет u-boot-tools (так он в Debian называется, по крайней мере). Там есть утилита dumpimage, ей можно вытащить куски. Сначала запросить состав образа, так как там может быть несколько частей.

Zubok ★★★★★ ( 16.10.20 18:55:51 )
Последнее исправление: Zubok 16.10.20 18:57:03 (всего исправлений: 1)

Что то я не понимаю как распаковать, position что писать и outfile - это директория или что?


Ну я точно не знаю. У меня тут нет таких файлов. Надо скачать. Я так понимаю, что тебе надо сначала посмотреть, что там лежит. Это опцией -l делаешь. Там он покажет, скажем, что там одна-две-три части (образ ядра, файловое дерево и т. д.). Я так понимаю, что -p будет указывать номер этой части (0, 1, 2. ). Вот все их последовательно надо вытащить опцией -i и сохранить в разные файлы. Потом эти файлы отдельно смотреть, что там.

Zubok ★★★★★ ( 16.10.20 19:34:55 )
Последнее исправление: Zubok 16.10.20 19:37:42 (всего исправлений: 3)

Информации мало и все тоже самое


Ну, попробуй тогда просто без -p . Мне трудно вслепую советовать определенно. Попробуй просто:

Если там только одна часть - нулевая, то на остальные он ругнется. Неужели тебе страшно попробовать без совета? Файл не испортится. Потом скажешь, что получается.

Вообще, конечно, уже мог бы поиском воспользоваться, а не на каждую закорючку вопросы задавать. :)

Zubok ★★★★★ ( 16.10.20 19:53:54 )
Последнее исправление: Zubok 16.10.20 19:54:17 (всего исправлений: 1)


Если какие-то файлы вылезут, то потом сделай file image-0 и посмотри что там. Если этот файл один всего. то это, скорее всего, kernel image пожатый. Что-то типа LZMA или gzip. Наверное.

Zubok ★★★★★ ( 16.10.20 20:24:39 )
Последнее исправление: Zubok 16.10.20 20:43:43 (всего исправлений: 1)

Получается полная ерудна не зависимо от значения -p (1,2,100,200. ) выходной файл получается один и тот же

Мне кажется что -p нужно задавать в байтах как начало и конец, но как узнать где начало и конец?


Можно binwalk использовать, натравить его и получить структуру. Файл этот, скорее всего, слеплен из нескольких разделов squashfs и их надо вытащить по отдельности, зная смещения. А что за файл. Я его могу где-то взять? Гляну.


Я тут сам быстро поискал. Это какая-то камера Xiaomi Dafang? скачал я какую-то прошивку. Если так, то по binwalk я думаю, что тут четыре части в образе после снятия заголовка при помощи dumpimage:

0x000000 - пожатое ядро.

0x200000 - rootfs. (squashfs, сжатие xz)

0x550000 - это драйвера (squashfs, сжатие xz)

0x5f0000 - а тут и до конца приложения начинаются, должно быть (размер 0x4a0000).

Zubok ★★★★★ ( 17.10.20 09:54:59 )
Последнее исправление: Zubok 17.10.20 10:04:49 (всего исправлений: 1)


То есть тебе нужно разобрать образ на кусочки: kernel, rootfs, drivers, apps, распаковать при помощи unsquashfs файл drivers, добавить свой драйвер, запаковать при помощи mksquashfs с правильными параметрами дерево драйверов назад, собрать из кусочков образ назад.

Zubok ★★★★★ ( 17.10.20 11:09:03 )
Последнее исправление: Zubok 17.10.20 11:20:09 (всего исправлений: 1)

Да ты прав это камера Xiaomi Xiafang, прошивку которую я хочу расковырять можно взять от сюда demo.bin


В общем, смотри. Если я с файлом угадал, то разобрать на кусочки можно так.


Welcome to the zlib home page, web pages originally created by Greg Roelofs and maintained by Mark Adler. If this page seems suspiciously similar to the PNG Home Page, rest assured that the similarity is completely coincidental. No, really.

zlib was written by Jean-loup Gailly (compression) and Mark Adler (decompression).

January 15, 2017

  • Fix deflate stored bug when pulling last block from window
  • Permit immediate deflateParams changes before any deflate input
  • Fix bug in deflate_stored() for zero-length input
  • Fix bug in gzwrite.c that produced corrupt gzip files
  • Improve compress() and uncompress() to support large lengths
  • Allow building zlib outside of the source directory
  • Fix bug when level 0 used with Z_HUFFMAN or Z_RLE
  • Fix bugs in creating a very large gzip header
  • Add uncompress2() function, which returns the input size used
  • Dramatically speed up deflation for level 0 (storing)
  • Add gzfread() and gzfwrite(), duplicating the interfaces of fread() and fwrite()
  • Add crc32_z() and adler32_z() functions with size_t lengths
  • Many portability improvements


zlib is designed to be a free, general-purpose, legally unencumbered -- that is, not covered by any patents -- lossless data-compression library for use on virtually any computer hardware and operating system. The zlib data format is itself portable across platforms. Unlike the LZW compression method used in Unix compress (1) and in the GIF image format, the compression method currently used in zlib essentially never expands the data. (LZW can double or triple the file size in extreme cases.) zlib's memory footprint is also independent of the input data and can be reduced, if necessary, at some cost in compression. A more precise, technical discussion of both points is available on another page.

zlib email address

Mark and Jean-loup can be reached by e-mail at . Please read the FAQ and the manual before asking us for help. We are getting too many questions which already have an answer in the zlib documentation.

The deflate and zlib specifications both achieved official Internet RFC status in May 1996, and zlib itself was adopted in version 1.1 of the Java Development Kit (JDK), both as a raw class and as a component of the JAR archive format.

The lovely zlib-vise image above was provided courtesy of Bruce Gardner, art director of Dr. Dobb's Journal. It appears in Mark Nelson's article in the January 1997 issue (see below).


zlib source code, version 1.2.11, tar.gz format (593K, SHA-256 hash c3e5e9fdd5004dcb542feda5ee4f0ff0744628baf8ed2dd5d66f8ca1197cb1a1):

Note that zlib is an integral part of libpng and has been tested extensively as part of many PNG-supporting applications.

zlib Information

zlib Frequently Asked Questions Zlib-announce mailing list New versions of zlib are announced on this list. Zlib-devel mailing list Please do not send questions or comments about zlib to this mailing list. Send those directly to the authors at after checking the FAQ and the manual, of course. The zlib-devel list is for the development of zlib—members are contributors to and testers of new versions of zlib. zlib Manual zlib Usage Example zlib Technical Details zlib-related specifications: Deflate stream disassembler. infgen.c produces a readable description of a gzip, zlib, or raw deflate stream. zlib's Deflate Algorithm zlib's deflate flush modes zlib License All released versions of zlib zlib on github

CRC (Cyclic Redundancy Check) Bonus Information

Ross Williams' classic "A Painless Guide to CRC Error Detection Algorithms" Code to generate any CRC, with a list of CRC descriptions. crcany.c can take a description of a CRC and compute that CRC efficiently. It includes bit-wise, table-driven byte-wise, and table-driven word-wise CRC algorithms. Code to modify a message so that it generates the desired CRC. spoof.c takes an abbreviated description of the CRC, the exclusive-or of the current CRC of the message and the desired CRC, the length of the message, and a list of bit locations in a message, and tells you which of those bits should be inverted in the message to get the desired CRC. Note that it does not need the message itself, due to the linearity property of CRCs.

ZIP File Processing Bonus Software

Code to read a zip file as a stream and extract its contents. sunzip.c will read a zip file from stdin and extract the files therein that use compression methods 0, 8, 9, or 12 (stored, deflate, deflate64, or bzip2). It accepts Zip64 input. Code to merge multiple zip files into a single zip file. zipknit.c accepts Zip64 input files, and will create Zip64 output if the combined size of the merged zip file warrants it. All compression formats are permitted, since no decompression or recompression is performed. Encrypted entries are permitted, and pass through unscathed.

Related External Links

Send comments or questions about zlib to the authors at after checking FAQ and manual.
Please report broken links to (PGP key).

Читайте также: