【问题标题】:decompress a .gz file using GZipStream使用 GZipStream 解压缩 .gz 文件
【发布时间】:2013-01-20 08:13:49
【问题描述】:

我有几个.gz文件,我想一个一个解压。 我在 C# 中使用 GzipStream 编写了一个简单的代码,但失败了。我想知道一个正确而有用的方法来实现我想要的。非常感谢。

private string Extrgz(string infile)
{
    string dir = Path.GetDirectoryName(infile);
    string decompressionFileName = dir + Path.GetFileNameWithoutExtension(infile) + "_decompression.bin";
    using (GZipStream instream = new GZipStream(File.OpenRead(infile), CompressionMode.Compress))// ArgumentException...
    {
        using (FileStream outputStream = new FileStream(decompressionFileName, FileMode.Append, FileAccess.Write))
        {
            int bufferSize = 8192, bytesRead = 0;
            byte[] buffer = new byte[bufferSize];
            while ((bytesRead = instream.Read(buffer, 0, bufferSize)) > 0)
            {
                outputStream.Write(buffer, 0, bytesRead);
            }
        }
    }
    return decompressionFileName;
}

【问题讨论】:

  • @Dan-o 参数“流”中的错误

标签: c# filestream gzipstream


【解决方案1】:

你需要解压但是你设置了CompressionMode.Compress,替换成CompressionMode.Decompress。

例如here。

【讨论】:

    【解决方案2】:

    这里:

    public static void DeCompressFile(string CompressedFile, string DeCompressedFile)
    {
        byte[] buffer = new byte[1024 * 1024];
    
        using (System.IO.FileStream fstrmCompressedFile = System.IO.File.OpenRead(CompressedFile)) // fi.OpenRead())
        {
            using (System.IO.FileStream fstrmDecompressedFile = System.IO.File.Create(DeCompressedFile))
            {
                using (System.IO.Compression.GZipStream strmUncompress = new System.IO.Compression.GZipStream(fstrmCompressedFile,
                        System.IO.Compression.CompressionMode.Decompress))
                {
                    int numRead = strmUncompress.Read(buffer, 0, buffer.Length);
    
                    while (numRead != 0)
                    {
                        fstrmDecompressedFile.Write(buffer, 0, numRead);
                        fstrmDecompressedFile.Flush();
                        numRead = strmUncompress.Read(buffer, 0, buffer.Length);
                    } // Whend
    
                    //int numRead = 0;
    
                    //while ((numRead = strmUncompress.Read(buffer, 0, buffer.Length)) != 0)
                    //{
                    //    fstrmDecompressedFile.Write(buffer, 0, numRead);
                    //    fstrmDecompressedFile.Flush();
                    //} // Whend
    
                    strmUncompress.Close();
                } // End Using System.IO.Compression.GZipStream strmUncompress 
    
                fstrmDecompressedFile.Flush();
                fstrmDecompressedFile.Close();
            } // End Using System.IO.FileStream fstrmCompressedFile 
    
            fstrmCompressedFile.Close();
        } // End Using System.IO.FileStream fstrmCompressedFile 
    
    } // End Sub DeCompressFile
    
    
    // http://www.dotnetperls.com/decompress
    public static byte[] Decompress(byte[] gzip)
    {
        byte[] baRetVal = null;
        using (System.IO.MemoryStream ByteStream = new System.IO.MemoryStream(gzip))
        {
    
            // Create a GZIP stream with decompression mode.
            // ... Then create a buffer and write into while reading from the GZIP stream.
            using (System.IO.Compression.GZipStream stream = new System.IO.Compression.GZipStream(ByteStream
                , System.IO.Compression.CompressionMode.Decompress))
            {
                const int size = 4096;
                byte[] buffer = new byte[size];
                using (System.IO.MemoryStream memory = new System.IO.MemoryStream())
                {
                    int count = 0;
                    count = stream.Read(buffer, 0, size);
                    while (count > 0)
                    {
                        memory.Write(buffer, 0, count);
                        memory.Flush();
                        count = stream.Read(buffer, 0, size);
                    }
    
                    baRetVal = memory.ToArray();
                    memory.Close();
                }
    
                stream.Close();
            } // End Using System.IO.Compression.GZipStream stream 
    
            ByteStream.Close();
        } // End Using System.IO.MemoryStream ByteStream
    
        return baRetVal;
    } // End Sub Decompress
    

    【讨论】:

      猜你喜欢
      • 2017-08-06
      • 1970-01-01
      • 1970-01-01
      • 2014-07-31
      • 1970-01-01
      • 2011-03-18
      • 2015-09-10
      相关资源
      最近更新 更多