c# - 在 C# 中读取 Gzip 文件的问题
问题描述
使用以下方法,我正在尝试读取 .gz 文件,但出现错误,因为"Stream does not support reading. (Parameter 'stream')"。我对这个错误一无所知,想知道这意味着什么,如果可能的话,有什么修复它的建议。文件是这种格式 test1.csv.gz 并且错误在行返回 using(var unzipper....
public async Task<string> ReadStream(string containerName, string digestFileName,string fileName, string connectionString)
{
string data = string.Empty;
string fileExtension = Path.GetExtension(fileName);
MemoryStream contents = await DownloadBlob(containerName, digestFileName, connectionString);
if (fileExtension == ".gz")
{
using (var unzipper = new GZipStream(contents, CompressionMode.Decompress))
using (StreamReader reader = new StreamReader(unzipper, Encoding.UTF8))
{
data = reader.ReadToEnd();
}
}
else
{
data = Encoding.UTF8.GetString(contents.ToArray());
}
return data;
}
下载Blob:
public async Task<MemoryStream> DownloadBlob(string containerName, string fileName, string connectionString)
{
MemoryStream memoryStream = new MemoryStream();
Microsoft.Azure.Storage.CloudStorageAccount storageAccount = Microsoft.Azure.Storage.CloudStorageAccount.Parse(connectionString);
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = serviceClient.GetContainerReference(containerName);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
if (blob.Exists())
using (memoryStream = new MemoryStream())
{
await blob.DownloadToStreamAsync(memoryStream);
}
return memoryStream;
}
解决方案
通过您的using
声明,您可以处理流。
不要在那里这样做,调用者应该这样做。
推荐阅读
- java - 在 kotlin 中获取两个 ISO 格式时间之间的分钟数
- angularjs - 如何使用 Selenium 和 Python 从 html 中抓取文本 Alpha Esports
- sql-server - SQL AlwaysOn - 如果您不将其用作集群/故障转移怎么办?
- django - 从 django 模型中的选择中获取人类可读名称的实际值
- php - 如何防止登录后返回登录页面
- python - 如何使用 lambda 层包装函数?
- django - Django rest如何不发送序列化程序的特定数据
- excel - 更改已捕获到 Excel 的日期格式
- three.js - 带有 BoxGeometry 的 CubeCamera
- uml - 关系多重性的解释