开发者

How do I parse the gzip file header from a gzip stream in C#?

开发者 https://www.devze.com 2023-01-10 09:37 出处:网络
I am forced to use an older version of the SharpZipLib and the standard Microsoft libraries to perform this. I have a gziped file whose name is different than the filename inside the archive. I need t

I am forced to use an older version of the SharpZipLib and the standard Microsoft libraries to perform this. I have a gziped file whose name is different than the filename inside the archive. I need to parse the gzip file header to return the original filename. Here is documentation on the gzip website:

http://www.gzip.org/zlib/rfc-gzip.html#conventions

And a java example that looks like it might be doing what I want. it looks like it checks for the file header, but doesn't actually read the file name.

(Sorry couldn't post more than 1 hyperlink) (http://开发者_StackOverflowwww).java2s.com/Open-Source/Java-Document/6.0-JDK-Modules/j2me/java/util/zip/GZIPInputStream.java.htm

Any help on this problem would be much appreciated. Thanks!


Well if finally figured it out. Its not the safest way or best but i needed a quick and dirty way to do it and this works. So if anyone else needs to know this or want to improve on it, here you go.

using (FileStream stream = File.OpenRead(filePath))
{
    int size = 2048;
    byte[] data = new byte[2048];
    size = stream.Read(data,0,size);

    if (data[3] == 8)
    {
        List<byte> byteList = new List<byte>();

        int i = 10;
        while (data[i] != 0)
        {
            byteList.Add(data[i]);
            i++;
        }
        string test = System.Text.ASCIIEncoding.ASCII.GetString(byteList.ToArray());
        Console.WriteLine(test);

    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消