我有一个应该提取 .gz 文件并将其加载到内存中的函数,我的问题是,如何在文件加载到内存后读取文件的内容而无需先将其保存到磁盘?
public static void Decompress2(FileInfo fileToDecompress)
{
using (FileStream fileStream = fileToDecompress.OpenRead())
{
using (var memStream = new MemoryStream())
{
string currentFileName = fileToDecompress.FullName;
string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);
using (FileStream decompressedFileStream = File.Create(newFileName))
{
using (GZipStream decompressionStream = new GZipStream(fileStream, CompressionMode.Decompress))
{
byte[] bytes = new byte[4096];
int n;
while ((n = decompressionStream.Read(bytes, 0, bytes.Length)) != 0)
{
memStream.Write(bytes, 0, n);
}
}
}
}
}
}