Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不支持写入压缩流。使用System.IO.GZipStream_C#_.net_Compression_Gzipstream - Fatal编程技术网

C# 不支持写入压缩流。使用System.IO.GZipStream

C# 不支持写入压缩流。使用System.IO.GZipStream,c#,.net,compression,gzipstream,C#,.net,Compression,Gzipstream,我在尝试使用.NET framework中包含的GZipStream类解压缩(.gz)文件时遇到异常。我正在使用MSDN文档。这是一个例外: 不支持写入压缩流 以下是应用程序来源: try { var infile = new FileStream(@"C:\TarDecomp\TarDecomp\TarDecomp\bin\Debug\nick_blah-2008.tar.gz", FileMode.Open, FileAccess.Read, FileShare.Read);

我在尝试使用.NET framework中包含的GZipStream类解压缩(.gz)文件时遇到异常。我正在使用MSDN文档。这是一个例外:

不支持写入压缩流

以下是应用程序来源:

 try
 {
     var infile = new FileStream(@"C:\TarDecomp\TarDecomp\TarDecomp\bin\Debug\nick_blah-2008.tar.gz", FileMode.Open, FileAccess.Read, FileShare.Read);
     byte[] buffer = new byte[infile.Length];
     // Read the file to ensure it is readable.
     int count = infile.Read(buffer, 0, buffer.Length);
     if (count != buffer.Length)
     {
         infile.Close();
         Console.WriteLine("Test Failed: Unable to read data from file");
         return;
     }
     infile.Close();
     MemoryStream ms = new MemoryStream();
     // Use the newly created memory stream for the compressed data.
     GZipStream compressedzipStream = new GZipStream(ms, CompressionMode.Decompress, true);
     Console.WriteLine("Decompression");
     compressedzipStream.Write(buffer, 0, buffer.Length); //<<Throws error here
     // Close the stream.
     compressedzipStream.Close();
     Console.WriteLine("Original size: {0}, Compressed size: {1}", buffer.Length, ms.Length);
} catch {...}
试试看
{
var infle=new FileStream(@“C:\TarDecomp\TarDecomp\TarDecomp\bin\Debug\nick_blah-2008.tar.gz”,FileMode.Open,FileAccess.Read,FileShare.Read);
字节[]缓冲区=新字节[infle.Length];
//读取文件以确保其可读性。
int count=infle.Read(buffer,0,buffer.Length);
if(计数!=缓冲区长度)
{
infle.Close();
Console.WriteLine(“测试失败:无法从文件中读取数据”);
返回;
}
infle.Close();
MemoryStream ms=新的MemoryStream();
//将新创建的内存流用于压缩数据。
GZipStream compressedzipStream=新的GZipStream(ms,CompressionMode.decompresse,true);
控制台写入线(“解压”);

compressedzipStream.Write(buffer,0,buffer.Length);//它告诉您应该调用
Read
,而不是
Write
,因为它是解压!内存流也应该用数据构造,或者您应该直接将文件流传递给GZipStream构造函数

应如何完成的示例(尚未尝试编译):


一开始,压缩流是如何工作的可能令人费解

读取采用压缩数据,写入采用未压缩数据。总之,流确保您始终只能“看到”未压缩数据


正确的方法是使用
GZipStream
进行读取,然后使用
GZipStream
进行写入。

压缩代码的工作方式与加密不同-您无法通过写入压缩数据从一个流解压缩到另一个流。您必须提供包含压缩的流sed数据,并让
GZipStream
从中读取。类似如下:

using (Stream file = File.OpenRead(filename))
using (Stream gzip = new GZipStream(file, CompressionMode.Decompress))
using (Stream memoryStream = new MemoryStream())
{
   CopyStream(gzip, memoryStream);
   return memoryStream.ToArray();
}
static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[8192];
    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}
CopyStream
是一种简单实用的方法,可以从一个流中读取数据并将所有数据复制到另一个流中。如下所示:

using (Stream file = File.OpenRead(filename))
using (Stream gzip = new GZipStream(file, CompressionMode.Decompress))
using (Stream memoryStream = new MemoryStream())
{
   CopyStream(gzip, memoryStream);
   return memoryStream.ToArray();
}
static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[8192];
    int bytesRead;
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, bytesRead);
    }
}