git 객체를 추출하기 위해 명령 줄 도구로 DEFLATE하는 방법은 무엇입니까?
DEFLATE 알고리즘에 대한 명령 줄 래퍼를 찾고 있습니다.
DEFLATE를 사용하여 압축 된 파일 (git blob)이 있는데 압축을 풀고 싶습니다. gzip 명령에는 gzip 형식이 아닌 DEFLATE 알고리즘을 직접 사용할 수있는 옵션이없는 것 같습니다.
이상적으로 저는 이것을 할 수있는 표준 Unix / Linux 도구를 찾고 있습니다.
편집 : 이것은 내 문제에 gzip을 사용하려고 할 때 얻는 출력입니다.
$ cat .git/objects/c0/fb67ab3fda7909000da003f4b2ce50a53f43e7 | gunzip
gzip: stdin: not in gzip format
업데이트 : Mark Adler는 git blob이 원시 DEFLATE 스트림이 아니라 zlib 스트림이라고 언급했습니다. 이 pigz도구는 여러 Linux 배포판에 미리 패키지로 제공 되는 도구 로 압축을 풀 수 있습니다 .
$ cat foo.txt
file foo.txt!
$ git ls-files -s foo.txt
100644 7a79fc625cac65001fb127f468847ab93b5f8b19 0 foo.txt
$ pigz -d < .git/objects/7a/79fc625cac65001fb127f468847ab93b5f8b19
blob 14file foo.txt!
내 원래 대답은 역사적인 이유로 유지되었습니다.
나는에서 힌트를 이해한다면 위키 백과 문서 마크 반 켐펜 언급을, 당신은 사용할 수 있습니다 puff.c에서 zlib을 직접.
다음은 작은 예입니다.
#include <assert.h>
#include <string.h>
#include "puff.h"
int main( int argc, char **argv ) {
unsigned char dest[ 5 ];
unsigned long destlen = 4;
const unsigned char *source = "\x4B\x2C\x4E\x49\x03\x00";
unsigned long sourcelen = 6;
assert( puff( dest, &destlen, source, &sourcelen ) == 0 );
dest[ 4 ] = '\0';
assert( strcmp( dest, "asdf" ) == 0 );
}
다음과 같은 것은 "$ type $ length \ 0"헤더를 포함하여 원시 콘텐츠를 인쇄합니다.
perl -MCompress::Zlib -e 'undef $/; print uncompress(<>)' \
< .git/objects/27/de0a1dd5a89a94990618632967a1c86a82d577
OpenSSL 명령 줄 도구를 사용하여이 작업을 수행 할 수 있습니다.
openssl zlib -d < $IN > $OUT
불행히도 적어도 Ubuntu에서는 zlib하위 명령이 기본 빌드 구성 ( --no-zlib --no-zlib-dynamic) 에서 비활성화되어 있으므로 openssl이를 사용하려면 소스에서 컴파일해야 합니다. 그러나 예를 들어 아치에서는 기본적으로 활성화되어 있습니다.
편집 : zlib명령이 아치에서도 더 이상 지원되지 않는 것 같습니다 . 이 답변은 더 이상 유용하지 않을 수 있습니다.
파이썬 한 줄짜리 :
$> python -c "import zlib,sys;print \
repr(zlib.decompress(sys.stdin.read()))" < $IN
다음과 같이 zlib-flate를 사용할 수 있습니다.
cat .git/objects/c0/fb67ab3fda7909000da003f4b2ce50a53f43e7 \
| zlib-flate -uncompress; echo
기본적으로 내 컴퓨터에 있지만 qpdf - tools for and transforming and inspecting PDF files설치해야하는 경우 의 일부입니다 .
나는 태어 났죠 한 echo이 방법으로 그 출력을 읽기 쉽게와 같이 명령의 끝에.
다음 명령을 시도하십시오.
printf "\x1f\x8b\x08\x00\x00\x00\x00\x00" | cat - .git/objects/c0/fb67ab3fda7909000da003f4b2ce50a53f43e7 | gunzip
외부 도구가 필요하지 않습니다.
출처 : UNIX에서 zlib 데이터의 압축을 해제하는 방법은 무엇입니까? 유닉스 SE에서
다음은 Ruby 한 줄짜리입니다 (cd .git / 먼저 모든 객체에 대한 경로 식별) :
ruby -rzlib -e 'print Zlib::Inflate.new.inflate(STDIN.read)' < ./74/c757240ec596063af8cd273ebd9f67073e1208
나는 이것에 대한 좋은 해결책이 없다는 것에 지쳐서 NPM에 무언가를 넣었습니다.
https://github.com/jezell/zlibber
이제 파이프를 통해 팽창 / 수축 명령을 수행 할 수 있습니다.
다음은 Python에서 커밋 객체를 분리하는 예입니다.
$ git show
commit 0972d7651ff85bedf464fba868c2ef434543916a
# all the junk in my commit...
$ python
>>> import zlib
>>> file = open(".git/objects/09/72d7651ff85bedf464fba868c2ef434543916a")
>>> data = file.read()
>>> print data
# binary garbage
>>> unzipped_data = zlib.decompress(data)
>>> print unzipped_data
# all the junk in my commit!
What you will see there is almost identical to the output of 'git cat-file -p [hash]', except that command doesn't print the header ('commit' followed by the size of the content and a null byte).
Looks like Mark Adler has us in mind and wrote an example of just how to do this with: http://www.zlib.net/zpipe.c
It compiles with nothing more than gcc -lz and the zlib headers installed. I copied the resulting binary to my /usr/local/bin/zpipe while working with git stuff.
git objects are compressed by zlib rather than gzip, so either using zlib to uncompress it, or git command, i.e. git cat-file -p <SHA1>, to print content.
// save this as deflate.go
package main
import (
"compress/zlib"
"io"
"os"
"flag"
)
var infile = flag.String("f", "", "infile")
func main() {
flag.Parse()
file, _ := os.Open(*infile)
r, err := zlib.NewReader(file)
if err != nil {
panic(err)
}
io.Copy(os.Stdout, r)
r.Close()
}
$ go build deflate.go
$ ./deflate -f .git/objects/c0/fb67ab3fda7909000da003f4b2ce50a53f43e7
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?
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>
I found this question looking for a work-around with a bug with the -text utility in the new version of the hadoop dfs client I just installed. The -text utility works like cat, except if the file being read is compressed, it transparently decompresses and outputs the plain-text (hence the name).
The answers already posted were definitely helpful, but some of them have one problem when dealing with Hadoop-sized amounts of data - they read everything into memory before decompressing.
So, here are my variations on the Perl and Python answers above that do not have that limitation:
Python:
hadoop fs -cat /path/to/example.deflate |
python -c 'import zlib,sys;map(lambda b:sys.stdout.write(zlib.decompress(b)),iter(lambda:sys.stdin.read(4096),""))'
Perl:
hadoop fs -cat /path/to/example.deflate |
perl -MCompress::Zlib -e 'print uncompress($buf) while sysread(STDIN,$buf,4096)'
Note the use of the -cat sub-command, instead of -text. This is so that my work-around does not break after they've fixed the bug. Apologies for the readability of the python version.
git objects are zlib streams (not raw deflate). pigz will decompress those with the -dz option.
pigz can do it:
apt-get install pigz
unpigz -c .git/objects/c0/fb67ab3fda7909000da003f4b2ce50a53f43e7
const zlib = require("zlib");
const adler32 = require("adler32");
const data = "hello world~!";
const chksum = adler32.sum(new Buffer(data)).toString(16);
console.log("789c",zlib.deflateRawSync(data).toString("hex"),chksum);
// or
console.log(zlib.deflateSync(data).toString("hex"));
To add to the collection, here are perl one-liners for deflate/inflate/raw deflate/raw inflate.
Deflate
perl -MIO::Compress::Deflate -e 'undef $/; my ($in, $out) = (<>, undef); IO::Compress::Deflate::deflate(\$in, \$out); print $out;'
Inflate
perl -MIO::Uncompress::Inflate -e 'undef $/; my ($in, $out) = (<>, undef); IO::Uncompress::Inflate::inflate(\$in, \$out); print $out;'
Raw deflate
perl -MIO::Compress::RawDeflate -e 'undef $/; my ($in, $out) = (<>, undef); IO::Compress::RawDeflate::rawdeflate(\$in, \$out); print $out;'
Raw inflate
perl -MIO::Uncompress::RawInflate -e 'undef $/; my ($in, $out) = (<>, undef); IO::Uncompress::RawInflate::rawinflate(\$in, \$out); print $out;'
'Program Club' 카테고리의 다른 글
| 더 나은 객체 지향 프로그래밍을 어떻게 연습 할 수 있습니까? (0) | 2020.10.17 |
|---|---|
| DropDownListFor Not 선택 값 (0) | 2020.10.17 |
| ID 자동 증가를 재설정 하시겠습니까? (0) | 2020.10.17 |
| 다항식 시간 및 지수 시간 (0) | 2020.10.17 |
| Cordova 플랫폼 Android 대상을 나열하는 동안 작동하지 않는 Android 추가 (0) | 2020.10.17 |