解码Git对象/"块长度不匹配它的补"错误 [英] Decoding git objects / "Block length does not match with its complement" error

查看:337
本文介绍了解码Git对象/"块长度不匹配它的补"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我坚持用很简单的,但恼人的问题,不能在互联网上找到答案。希望你能够指出我的,我做了什么错。

I'm stuck with very simple but annoying issue, and cannot find answer on the Internet. Hope you will be able to point me, what I've done wrong.

我想从对象Git仓库进行解码。据 ProGit ,文件的名称和它的内容已经提交过程中放气。

I'm trying to decode object from Git repository. According to ProGit, file name and it's contents have been deflated during commit.

我使用C#来读取SHA1表示对象到一个流,它充气,并转换成字节数组。下面是代码:

I'm using C# to read object indicated by SHA1 into a stream, inflate it and convert into byte array. Here is the code:

using System.IO.Compression;

static internal byte[] GetObjectBySha(string storagePath, string sha)
{
    string filePath = Path.Combine(storagePath, "objects", sha.Substring(0, 2), sha.Substring(2, 38));
    byte[] fileContent = null;

    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
        using (MemoryStream ms = new MemoryStream())
        {
            using (DeflateStream gs = new DeflateStream(fs, CompressionMode.Decompress))
            {
                gs.CopyTo(ms);
            }

            fileContent = ms.ToArray();
        }
    }

    return fileContent;
}

gs.CopyTo(MS); 到达发生运行时错误:阻止长度不符合其补充

When gs.CopyTo(ms); is reached the runtime error occurs: Block length does not match with its complement.

为什么会这样?

关于我想读...这是二进制的,它是由git的可执行文件创建的内容。原来的文件名是 TESTFILE.TXT ,它的内容是示例文本。的SHA1是 51d0be227ecdc0039698122a1513421ce35c1dbe

Regarding the content of the file I'm trying to read... It's binary and it was created by git executable. The original file name is testfile.txt, it's content is Sample text. the SHA1 is 51d0be227ecdc0039698122a1513421ce35c1dbe.

任何想法,将不胜感激!

Any idea would be greatly appreciated!

推荐答案

DeflateStream 的zlib 是两个不同的东西在的this回答

DeflateStream and zlib are two different things as explained in this answer:

有在.NET基础没有ZlibStream类库 - 没有什么
生产或消费ZLIB

There is no ZlibStream in the .NET base class library - nothing that produces or consumes ZLIB

所以,你需要的是一个ZLIB消费者。该 DotNetZip 库提供1:

So what you need is a ZLIB consumer. The DotNetZip library provides one:

static internal byte[] GetObjectBySha(string storagePath, string sha)
{
    string filePath = Path.Combine(storagePath, "objects", sha.Substring(0, 2), sha.Substring(2, 38));
    byte[] compressed = File.ReadAllBytes(filePath);
    return Ionic.Zlib.ZlibStream.UncompressBuffer(compressed);
}

这篇关于解码Git对象/"块长度不匹配它的补"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆