C# 中的 GZIP 文件总长度

发布于 2024-10-11 06:28:02 字数 150 浏览 6 评论 0原文

我有一个大小为几 GB 的压缩文件,我想获取解压缩内容的大小,但不想在 C# 中实际解压缩该文件,我可以使用什么库?当我右键单击 .gz 文件并转到“属性”时,在“存档”选项卡下有一个属性名称“TotalLength”,它显示了该值。但我想使用 C# 以编程方式获取它..有什么想法吗?

I have a zipped file having size of several GBs, I want to get the size of Unzipped contents but don't want to actually unzip the file in C#, What might be the Library I can use? When I right click on the .gz file and go to Properties then under the Archive Tab there is a property name TotalLength which is showing this value. But I want to get it Programmatically using C#.. Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

唔猫 2024-10-18 06:28:02

gz 文件的最后 4 个字节包含长度。

所以它应该是这样的:

using(var fs = File.OpenRead(path))
{
  fs.Position = fs.Length - 4;
  var b = new byte[4];
  fs.Read(b, 0, 4);
  uint length = BitConverter.ToUInt32(b, 0);
  Console.WriteLine(length);
}

The last 4 bytes of the gz file contains the length.

So it should be something like:

using(var fs = File.OpenRead(path))
{
  fs.Position = fs.Length - 4;
  var b = new byte[4];
  fs.Read(b, 0, 4);
  uint length = BitConverter.ToUInt32(b, 0);
  Console.WriteLine(length);
}
或十年 2024-10-18 06:28:02

.gz 文件的最后一个字节是未压缩的输入大小模 2^32。如果您的未压缩文件不大于 4GB,则只需读取文件的最后 4 个字节。如果您有更大的文件,我不确定是否可以在不解压缩流的情况下获得。

The last for bytes of a .gz file are the uncompressed input size modulo 2^32. If your uncompressed file isn't larger than 4GB, just read the last 4 bytes of the file. If you have a larger file, I'm not sure that it's possible to get without uncompressing the stream.

岁月打碎记忆 2024-10-18 06:28:02

编辑:参见 Leppie 和 Gabe 的答案;我保留它(而不是删除它)的唯一原因是,如果您怀疑长度>,则可能有必要。 4GB


对于 gzip,该数据似乎无法直接获得 - 我查看了 GZipStreamSharpZipLib 等效项 - 两者都不起作用。我能建议的最好的办法是在本地运行它:

    long length = 0;
    using(var fs = File.OpenRead(path))
    using (var gzip = new GZipStream(fs, CompressionMode.Decompress)) {
        var buffer = new byte[10240];
        int count;
        while ((count = gzip.Read(buffer, 0, buffer.Length)) > 0) {
            length += count;
        }
    }

如果它是一个 zip,那么 SharpZipLib:

    long size = 0;
    using(var zip = new ZipFile(path)) {
        foreach (ZipEntry entry in zip) {
            size += entry.Size;
        }
    }

EDIT: See the answers by Leppie and Gabe; the only reason I'm keeping this (rather than deleting it) is that it may be necessary if you suspect the length is > 4GB


For gzip, that data doesn't seem to be directly available - I've looked at GZipStream and the SharpZipLib equivalent - neither works. The best I can suggest is to run it locally:

    long length = 0;
    using(var fs = File.OpenRead(path))
    using (var gzip = new GZipStream(fs, CompressionMode.Decompress)) {
        var buffer = new byte[10240];
        int count;
        while ((count = gzip.Read(buffer, 0, buffer.Length)) > 0) {
            length += count;
        }
    }

If it was a zip, then SharpZipLib:

    long size = 0;
    using(var zip = new ZipFile(path)) {
        foreach (ZipEntry entry in zip) {
            size += entry.Size;
        }
    }
成熟的代价 2024-10-18 06:28:02
public static long mGetFileLength(string strFilePath)
{
    if (!string.IsNullOrEmpty(strFilePath))
    {
        System.IO.FileInfo info = new System.IO.FileInfo(strFilePath);
        return info.Length;
    }

    return 0; 
}
public static long mGetFileLength(string strFilePath)
{
    if (!string.IsNullOrEmpty(strFilePath))
    {
        System.IO.FileInfo info = new System.IO.FileInfo(strFilePath);
        return info.Length;
    }

    return 0; 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文