You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#从Azure Blob解压下载文件失败及代码报错求助

Troubleshooting Azure Blob Zipped File Download & Extraction Issues

Looking at your code snippet, there are a few common pitfalls that could be causing your error. Let's break down the problems and fix them one by one:

1. Reset the Stream Position

When you retrieve the stream from Azure Blob Storage, the stream pointer might be stuck at the end of the file. The ZipArchive needs to read from the start of the stream to parse the zip content correctly. Add this line right after getting your stream:

Stream fs = GetFile(strPath);
fs.Position = 0; // Reset stream to the beginning before parsing

2. Decouple the Blob Stream with a MemoryStream

The using block around ZipArchive will automatically dispose the underlying fs stream when it exits. If your GetFile method returns a stream that shouldn't be disposed immediately, or if the blob stream isn't seekable, wrap it in a MemoryStream first:

Stream fs = GetFile(strPath);
using (var ms = new MemoryStream())
{
    fs.CopyTo(ms);
    ms.Position = 0; // Reset position after copying
    using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Read))
    {
        // Your extraction logic goes here
    }
}

This ensures you have a seekable, self-contained stream for the zip parser, without risking premature disposal of the original blob stream.

3. Fix FileStream Disposal & Return Logic

Your current code wraps the FileStream in a using block, which will dispose the stream before you return it. This means the caller will receive a disposed stream, leading to errors when trying to use it.

If you want to save the extracted file to disk (and not return the stream), adjust your method to return void and complete the copy properly:

public void DownloadAndExtract(string strPath) 
{ 
    Stream fs = GetFile(strPath);
    using (var ms = new MemoryStream())
    {
        fs.CopyTo(ms);
        ms.Position = 0;
        using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Read)) 
        { 
            var entry = zip.Entries.First(); 
            // Skip directory entries (they have empty Name properties)
            if (string.IsNullOrEmpty(entry.Name)) return;
            
            string filename = $"Report_{GetUploadTime()}"; 
            using (var fileStream = new FileStream(filename, FileMode.CreateNew, FileAccess.ReadWrite)) 
            { 
                entry.Open().CopyTo(fileStream); // Copy the zip entry content to the file
            }
        }
    }
}

4. Validate the Zip File & Entries

  • First, manually download the zip file from Azure Blob Storage and try unzipping it locally. This will rule out corruption during upload.
  • Add a check to skip directory entries (as shown above) since zip.Entries.First() might pick up a folder instead of a file.

If you're still getting an error, share the exact exception message and stack trace—this will help narrow down the issue even further!

内容的提问来源于stack exchange,提问作者raghav

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:39:08