tags:

views:

91

answers:

5

I'm looking for a command line wrapper for the DEFLATE algorithm.

I have a file (git blog) that is compressed using DEFLATE, and I want to uncompress it. The gzip command does not seem to have an option to directly use the DEFLATE algorithm, rather than the gzip format.

Ideally I'm looking for a standard Unix/Linux tool that can do this.

edit: This is the output I get when trying to use gzip for my problem:

$ cat .git/objects/c0/fb67ab3fda7909000da003f4b2ce50a53f43e7 | gunzip

gzip: stdin: not in gzip format
A: 

See http://en.wikipedia.org/wiki/DEFLATE#Encoder_implementations

It lists a number of software implementations, including gzip, so that should work. Did you try just running gzip on the file? Does it not recognize the format automatically?

How do you know it is compressed using DEFLATE? What tool was used to compress the file?

Marc van Kempen
felixge
Ah ok, so the data is compressed using the zlib library, then it stands to reason you can uncompress using zlib too! You could try a ruby, perl or other binding to wip up a simple deflate script. Or if you're not afraid to try your hands at compiling a c program, try this:http://www.zlib.net/zlib_how.html
Marc van Kempen
NB I just tried it and zpipe.c works on git objects, compile with 'gcc -o zpipe zpipe.c -I/path/to/zlib.h -L/path/to/zlib -lz'use:./zpipe -d < .git/objects/83/535d1693580f04824a2ddd22bd241fd00533d8(use -d for decompression)
Marc van Kempen
+1  A: 

If I understand the hint in the Wikipedia article mentioned by Marc van Kempen, you can use puff.c from zlib directly.

mkluwe
Yeah, I looked at that. But I would definitely prefer a commonly packaged tool.
felixge
Choosing this as the "right" answer as it is the closest. Otherwise those SO folks who complain about my low acceptance rate will get their forks out again : ).I'm actually using a little PHP script now : /
felixge
@fekixge: Well, thanks. I tried it yesterday on an gzipped file (stripping the gzip header away) but it crashed, unluckily. I'm debugging now as this is quite interesting to me.
mkluwe
A: 

Why don't you just use git's tools to access the data? This should be able to read any git object:

git show --pretty=raw <object SHA-1>
igorw
I'm preparing for a little git-workshop I'm going to give soon. One of the examples involves showing what 'git add' does by hand.De-compressing the blob using git itself doesn't make sense since I want to show the underlaying functionality. I will probably end up using ruby or perl, but I was hoping I could stick with a simple bash oneliner.
felixge
Or `git cat-file -p c0fb67ab3fda7909000da003f4b2ce50a53f43e7`
Jakub Narębski
A: 

It has a header on it - before you deflate it you'll need to strip everything before and including the first null byte from the data, btw.

Scott Chacon
I think the header is compressed using DEFLATE as well. That is unless the example I'm learning from is wrong: http://progit.org/book/ch9-2.html : )
felixge
haha - you're right. I should really read my own stuff before I comment on things.
Scott Chacon
A: 

Something like the following will print the raw content, including the "$type $length\0" header:

perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' < .git/objects/27/de0a1dd5a89a94990618632967a1c86a82d577
araqnid