dotnetzip 创建空的损坏文件
dotnetzip creating empty corrupted file
我正在尝试创建一个 HTTP 触发函数,该函数将从 Azure 存储中提取一个 blob,将其压缩,然后将 zip 重新上传到 blob 存储。在这个过程中的某个地方出了问题。它成功压缩并重新上传,但压缩文件的内容为 0KB,无法打开。任何帮助,将不胜感激。在下面找到代码。
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
try
{
string fileName = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "fileName", true) == 0)
.Value;
log.Info($"fileName: {fileName}");
dynamic request = await req.Content.ReadAsAsync<object>();
var container = (string)request.body;
string connectionString = Environment.GetEnvironmentVariable("blobstorageconnectionstring");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
MemoryStream outputStream = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry($"{fileName}.xlsx", DownloadFromBlobStorage(fileName, connectionString, container));
zip.CompressionMethod = CompressionMethod.Deflate;
zip.Save(outputStream);
}
UploadToBlobStorage(fileName + "ZIP", outputStream, connectionString, container);
return req.CreateResponse(HttpStatusCode.OK, "OK");
}
catch (Exception ex)
{
log.Error("error", ex: ex);
return req.CreateResponse(HttpStatusCode.BadRequest);
}
}
private static void UploadToBlobStorage(string name, MemoryStream dataStream, string storageConnectionString, string blobContainerName)
{
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(blobContainerName);
container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(name);
blob.Properties.ContentType = "application/x-zip-compressed";
dataStream.Position = 0;
blob.UploadFromStream(dataStream);
}
private static MemoryStream DownloadFromBlobStorage(string fileName, string storageConnectionString, string blobContainerName)
{
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(blobContainerName);
container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
MemoryStream memStream = new MemoryStream();
blob.DownloadToStream(memStream);
return memStream;
}
您似乎也应该将从 DownloadFromBlobStorage()
返回的 MemoryStream
对象定位为零(您已经在 [=15= 上使用 outputStream
/dataStream
]), 然后将其传递给 zip.AddEntry()
:
MemoryStream inputStream = DownloadFromBlobStorage(fileName, connectionString, container);
inputStream.Position = 0;
zip.AddEntry($"{fileName}.xlsx", inputStream);
看看这个答案:
Creating Zip file from stream and downloading it - Stack Overflow
经过更多研究后,我发现通常您应该始终在将流传递给函数之前对其进行定位:
Who is responsible for stream positioning? - Software Engineering Stack Exchange
我正在尝试创建一个 HTTP 触发函数,该函数将从 Azure 存储中提取一个 blob,将其压缩,然后将 zip 重新上传到 blob 存储。在这个过程中的某个地方出了问题。它成功压缩并重新上传,但压缩文件的内容为 0KB,无法打开。任何帮助,将不胜感激。在下面找到代码。
public static async Task<HttpResponseMessage> Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
try
{
string fileName = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "fileName", true) == 0)
.Value;
log.Info($"fileName: {fileName}");
dynamic request = await req.Content.ReadAsAsync<object>();
var container = (string)request.body;
string connectionString = Environment.GetEnvironmentVariable("blobstorageconnectionstring");
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient client = storageAccount.CreateCloudBlobClient();
MemoryStream outputStream = new MemoryStream();
using (ZipFile zip = new ZipFile())
{
zip.AddEntry($"{fileName}.xlsx", DownloadFromBlobStorage(fileName, connectionString, container));
zip.CompressionMethod = CompressionMethod.Deflate;
zip.Save(outputStream);
}
UploadToBlobStorage(fileName + "ZIP", outputStream, connectionString, container);
return req.CreateResponse(HttpStatusCode.OK, "OK");
}
catch (Exception ex)
{
log.Error("error", ex: ex);
return req.CreateResponse(HttpStatusCode.BadRequest);
}
}
private static void UploadToBlobStorage(string name, MemoryStream dataStream, string storageConnectionString, string blobContainerName)
{
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(blobContainerName);
container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(name);
blob.Properties.ContentType = "application/x-zip-compressed";
dataStream.Position = 0;
blob.UploadFromStream(dataStream);
}
private static MemoryStream DownloadFromBlobStorage(string fileName, string storageConnectionString, string blobContainerName)
{
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient client = account.CreateCloudBlobClient();
CloudBlobContainer container = client.GetContainerReference(blobContainerName);
container.CreateIfNotExists();
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);
MemoryStream memStream = new MemoryStream();
blob.DownloadToStream(memStream);
return memStream;
}
您似乎也应该将从 DownloadFromBlobStorage()
返回的 MemoryStream
对象定位为零(您已经在 [=15= 上使用 outputStream
/dataStream
]), 然后将其传递给 zip.AddEntry()
:
MemoryStream inputStream = DownloadFromBlobStorage(fileName, connectionString, container);
inputStream.Position = 0;
zip.AddEntry($"{fileName}.xlsx", inputStream);
看看这个答案:
Creating Zip file from stream and downloading it - Stack Overflow
经过更多研究后,我发现通常您应该始终在将流传递给函数之前对其进行定位:
Who is responsible for stream positioning? - Software Engineering Stack Exchange