将GZipStream复制到MemoryStream时,C#无法访问封闭流
我一直在尝试将GZipStream转换为MemoryStream,然后将其转换为字节数组,而无需将任何文件写入硬盘。我一直在尝试将其复制到MemoryStream,但遇到以下错误:未处理的异常:System.ObjectDisposedException:无法访问关闭的流 我已经研究了一些其他的解决方案,但是我还没有成功地将它们实现到我想要实现的目标中将GZipStream复制到MemoryStream时,C#无法访问封闭流,c#,memorystream,gzipstream,C#,Memorystream,Gzipstream,我一直在尝试将GZipStream转换为MemoryStream,然后将其转换为字节数组,而无需将任何文件写入硬盘。我一直在尝试将其复制到MemoryStream,但遇到以下错误:未处理的异常:System.ObjectDisposedException:无法访问关闭的流 我已经研究了一些其他的解决方案,但是我还没有成功地将它们实现到我想要实现的目标中 GZipStream decompressedStream = Decompress(new FileInfo(args[0]));
GZipStream decompressedStream = Decompress(new FileInfo(args[0]));
using (var finalStream = new MemoryStream())
{
decompressedStream.CopyTo(finalStream);
byte[] decompressedBytes = new byte[finalStream.Length];
}
编辑:有人想让我为Decompress()添加代码,所以它就在这里
public static GZipStream Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length) + " (decompressed)";
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
return decompressionStream;
}
}
}
}
问题在于:
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
return decompressionStream;
}
使用
语句(见文章)处理流,不能在块外使用它。将MemoryStream
处理移到此块内,并从方法返回byte[]
像这样的方法应该会奏效:
public static byte[] Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length) + " (decompressed)";
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
using (var finalStream = new MemoryStream())
{
decompressionStream.CopyTo(finalStream);
return finalStream.ToArray();
}
}
}
}
问题在于:
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
{
return decompressionStream;
}
使用
语句(见文章)处理流,不能在块外使用它。将MemoryStream
处理移到此块内,并从方法返回byte[]
像这样的方法应该会奏效:
public static byte[] Decompress(FileInfo fileToDecompress)
{
using (FileStream originalFileStream = fileToDecompress.OpenRead())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length) + " (decompressed)";
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
using (var finalStream = new MemoryStream())
{
decompressionStream.CopyTo(finalStream);
return finalStream.ToArray();
}
}
}
}
您尝试过解压缩的数据流.Close()吗?还可以看一看你能解释一下
解压吗
。它是返回GZipStream
的函数吗?如果是,请添加其代码。@Hamid Reza Mohammadi是的,我将向问题添加解压缩函数。您是否尝试过解压缩流.Close()?还可以看一看你能解释一下解压吗
。它是返回GZipStream
的函数吗?如果是,则添加代码。@Hamid Reza Mohammadi是的,我将在问题中添加解压缩函数