C# 中的 Zip Azure Blob 文件损坏

时间:2021-01-14 12:57:36

标签: azure azure-storage-blobs

我正在尝试使用 ZipArchive 压缩我的 blob 存储文件并将该 zip 流上传回 blob 存储。

下面是将文件流下载到 Zip Entry 并上传回 Blob 存储的代码,但问题是当我返回存储资源管理器并下载我的 zip 文件时,它给了我这个错误:

存档格式未知或已损坏

    var account = _cloudStorageAccount;
                var blobClient = account.CreateCloudBlobClient();
                CloudBlobContainer container = _blobClient.GetContainerReference("myFiles");
                CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference("myFile.pdf");
                MemoryStream zipStream = new MemoryStream();
                using (var zip = new ZipArchive(zipStream, ZipArchiveMode.Create  , true))
                {
                    ZipArchiveEntry orderEntry = zip.CreateEntry("myFile.pdf");
                    using (Stream entryStream = orderEntry.Open())
                    {
                        var cloudBlockClient = container.GetBlockBlobReference("myFile.pdf");
                        await cloudBlockClient.DownloadToStreamAsync(entryStream);
                    }
                    CloudBlobContainer _blobContainer = _blobClient.GetContainerReference("protocol");
                    CloudBlockBlob cloudBlockBlobZip = _blobContainer.GetBlockBlobReference("Fifth.zip");
                    cloudBlockBlobZip.Properties.ContentType = "zip";
                    zipStream.Seek(0, SeekOrigin.Begin);
                    zipStream.Position = 0;
                    await cloudBlockBlobZip.UploadFromStreamAsync(zipStream);
                    var fileUrl = cloudBlockBlobZip.Uri.AbsoluteUri;
                    return zipStream;
                }
              

1 个答案:

答案 0 :(得分:0)

关于问题,请参考以下代码。我使用新的存储 SDK Azure.Storage.Blobs

 string[] blobs = { "",... };
            string connectionString = "";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            var sourceContainer = blobServiceClient.GetBlobContainerClient("input");
            var desContainer = blobServiceClient.GetBlobContainerClient("output");
            var desBlob= desContainer.GetBlockBlobClient( "my.zip");
            var options = new BlockBlobOpenWriteOptions {
                HttpHeaders = new BlobHttpHeaders {
                    ContentType = MimeMapping.GetMimeMapping("my.zip"),
                },
            };
          using (var outStream = await desBlob.OpenWriteAsync(true, options).ConfigureAwait(false))
            using (var zipArchive = new ZipArchive(outStream, ZipArchiveMode.Create)) {
                foreach (var blob in blobs)
                {
                    var source = sourceContainer.GetBlobClient(blob);
                    var entry = zipArchive.CreateEntry(blob, CompressionLevel.Fastest);

                    // approach 1
                    using (var entryStream = entry.Open())
                    {
                        await source.DownloadToAsync(entryStream).ConfigureAwait(false);
                        entryStream.Close();
                    }

                }
            }
相关问题