Decoding git objects / Error "Block length does not match its complement"

I am stuck in a very simple but annoying problem and cannot find the answer on the Internet. Hope you can tell me what I did wrong.

I am trying to decode an object from a Git repository. According to ProGit , the file name and its contents were deflated during commit.

I use C # to read the object indicated by SHA1 into a stream, inflate it and convert to an array of bytes. 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;
}

When achieved gs.CopyTo(ms);, a runtime error occurs: The length of the block does not match its complement.

Why is that?

, ... Git. testfile.txt, Sample text. SHA1 51d0be227ecdc0039698122a1513421ce35c1dbe.

!

+5
2

DeflateStream zlib - , :

.NET ZlibStream - , ZLIB

, ZLIB. DotNetZip :

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);
}
+7

ZLib Deflate , "" . - , , - , DeflateStream. , .

+2

All Articles