【问题标题】:Zip Azure Storage Files and Return File from Web Api returns corrupted files when unzipped压缩 Azure 存储文件并从 Web Api 返回文件在解压缩时返回损坏的文件
【发布时间】:2022-11-12 07:12:28
【问题描述】:

压缩 Azure 存储文件并从 Web Api 返回文件解压缩时返回损坏的文件,这是我的代码。

[HttpPost(nameof(DownloadFiles))]
        public async Task<IActionResult> DownloadFiles(List<string> fileNames)
        {

            CloudBlockBlob blockBlob;
            MemoryStream outputMemStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
            Stream blobStream;
            zipStream.SetLevel(3); //0-9, 9 being the highest level of compression

            string blobstorageconnection = _configuration.GetValue<string>("BlobConnectionString");
            CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
            CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
            CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(_configuration.GetValue<string>("BlobContainerName"));

            using (MemoryStream memoryStream = new MemoryStream())
            {
                foreach (string fileName in fileNames)
                {
                    blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
                    await blockBlob.DownloadToStreamAsync(memoryStream);

                    ZipEntry newEntry = new ZipEntry(blockBlob.Name);
                    newEntry.DateTime = DateTime.Now;

                    zipStream.PutNextEntry(newEntry);

                    StreamUtils.Copy(memoryStream, zipStream, new byte[4096]);
                    zipStream.CloseEntry();
                }
            };

            zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
            zipStream.Close();                  // Must finish the ZipOutputStream before using outputMemStream.

            outputMemStream.Position = 0;

            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            result.Content = new StreamContent(outputMemStream);
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
            result.Content.Headers.ContentDisposition.FileName = "Documents.zip";
            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentLength = outputMemStream.Length;
            return File(outputMemStream, "application/octet-stream", "Documents.zip");
        }

我正在使用 SharpZipLib,当我下载压缩文件并解压缩时,此文件中包含的文件已损坏。

有什么建议吗?非常感谢你的帮助

我试图在我的天蓝色存储上压缩文件以将它们下载为 zip,但下载的 zip 中的文件已损坏

【问题讨论】:

    标签: asp.net .net api azure-blob-storage sharpziplib


    【解决方案1】:
    [HttpPost(nameof(DownloadFiles))]
        public async Task<IActionResult> DownloadFiles(List<string> fileNames)
        {
            MemoryStream outputMemStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(outputMemStream);
    
            zipStream.SetLevel(3); //0-9, 9 being the highest level of compression
            byte[] bytes = null;
    
            foreach (string fileName in fileNames)
            {
                var newEntry = new ZipEntry(fileName);
                newEntry.DateTime = DateTime.Now;
    
                zipStream.PutNextEntry(newEntry);
    
                bytes = await CreateFile(fileName);
    
                MemoryStream inStream = new MemoryStream(bytes);
                StreamUtils.Copy(inStream, zipStream, new byte[4096]);
                inStream.Close();
                zipStream.CloseEntry();
            }
    
            zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
            zipStream.Close();          // Must finish the ZipOutputStream before using outputMemStream.
    
            outputMemStream.Position = 0;
    
            return File(outputMemStream.ToArray(), "application/octet-stream", "directory.zip");
        }
        private async Task<byte[]> CreateFile(string fileName)
        {
            byte[] bytes = null;
            await using (MemoryStream ms = new MemoryStream())
            {
                CloudBlockBlob blockBlob;
                string blobstorageconnection = _configuration.GetValue<string>("BlobConnectionString");
                CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(blobstorageconnection);
                CloudBlobClient cloudBlobClient = cloudStorageAccount.CreateCloudBlobClient();
                CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference(_configuration.GetValue<string>("BlobContainerName"));
                blockBlob = cloudBlobContainer.GetBlockBlobReference(fileName);
                await blockBlob.DownloadToStreamAsync(ms);
                bytes = ms.ToArray();
            }
    
            return bytes;
        }
    

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 2015-05-04
      • 1970-01-01
      • 1970-01-01
      • 2019-12-12
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 1970-01-01
      相关资源
      最近更新 更多