C#-为什么新的ZipArchive-to-MemoryStream在ZipArchive-to-FileStream工作时失败?

C#-为什么新的ZipArchive-to-MemoryStream在ZipArchive-to-FileStream工作时失败?,c#,.net,C#,.net,我正在尝试从Azure Blob存储设置zip下载。我已经设法创建了一个有效的文件流,但当我尝试对一个以.ToArray()结尾的MemoryStream执行相同的操作时,生成的.zip文件已损坏 如果您对这里出现的问题有任何想法,我们将不胜感激 代码 [路由(“测试下载zip”)] [HttpGet] 公共异步任务TestDownloadZip() { //工作 等待BlobLogic.DownloadZip_文件流(连接); //失败 byte[]bytes=等待BlobLogic.Down

我正在尝试从Azure Blob存储设置zip下载。我已经设法创建了一个有效的文件流,但当我尝试对一个以.ToArray()结尾的MemoryStream执行相同的操作时,生成的.zip文件已损坏

如果您对这里出现的问题有任何想法,我们将不胜感激

代码
[路由(“测试下载zip”)]
[HttpGet]
公共异步任务TestDownloadZip()
{
//工作
等待BlobLogic.DownloadZip_文件流(连接);
//失败
byte[]bytes=等待BlobLogic.DownloadZip_字节(连接);
返回新的FileContentResult(字节,“应用程序/zip”)
{
FileDownloadName=“bytes.zip”
};
}
公共异步任务下载ZIP\U文件流(IEnumerable p\U连接)
{
目录.CreateDirectory(“./testdata/”);
使用FileStream s_ZipStream=newfilestream(“./testdata/FileStream.zip”,FileMode.Create);
使用ZipArchive s_ZipArchive=new ZipArchive(s_ZipStream,ZipArchiveMode.Create,true);
foreach(p_连接中的IBlobConnection连接)
{
ZipArchiveEntry entry=s_ZipArchive.CreateEntry($“{Guid.NewGuid()}{connection.FileEnding}”);
CloudBlockBlob s_Block=GetBlockReference(连接);
使用Stream entrySteram=entry.Open();
等待s_Block.下载到Streamasync(entrySteram);
}
}
公共异步任务下载ZIP_字节(IEnumerable p_连接)
{
使用MemoryStream s_ZipStream=new MemoryStream();
使用ZipArchive s_ZipArchive=new ZipArchive(s_ZipStream,ZipArchiveMode.Create,true);
foreach(p_连接中的IBlobConnection连接)
{
ZipArchiveEntry entry=s_ZipArchive.CreateEntry($“{Guid.NewGuid()}{connection.FileEnding}”);
CloudBlockBlob s_Block=GetBlockReference(连接);
使用Stream entrySteram=entry.Open();
等待s_Block.下载到Streamasync(entrySteram);
}
s_ZipStream.Seek(0,SeekOrigin.Begin);
返回s_ZipStream.ToArray();
}
结果


在调用MemoryStream.ToArray()之前处理ZipArchive为我解决了这个问题。谢谢马修·沃森:)

公共异步任务下载ZIP_字节(IEnumerable p_连接)
{
使用MemoryStream zipStream=新的MemoryStream();
使用(ZipArchive-ZipArchive=new-ZipArchive(zipStream,ZipArchiveMode.Create,true))
{
foreach(p_连接中的IBlobConnection连接)
{
ZipArchiveEntry entry=zipArchive.CreateEntry($“{Guid.NewGuid()}{connection.FileEnding}”);
CloudBlockBlob block=GetBlockReference(连接);
使用Stream entrySteram=entry.Open();
等待block.DownloadToStreamAsync(entrySteram);
}
}
返回zipStream.ToArray();
}

是否在
szipstream.ToArray()之前执行
szipschive.Flush()
修复它?在调用
ToArray
之前,尝试将
位置设置为0。或者调用
sziparchive.Seek(0,SeekOrigin.Begin)
我尝试过:zipStream.Flush();zipStream.Seek(0,SeekOrigin.Begin);zipStream.Position=0;没有结果:(Sry的格式,你说的是zipStream而不是ZipArchive引用,对吗?
我试过:zipStream.Flush();zipStream.Seek(0,SeekOrigin.Begin);zipStream.Position=0;没有结果
但是你试过我建议的吗?ZipArchive没有.Flush()。我试过zipStream.Flush()没有其他修改。
[Route("test-download-zip")]
[HttpGet]
public async Task<FileContentResult> TestDownloadZip()
{
    // Works
    await BlobLogic.DownloadZip_FileStream(Connections);

    // Fails
    byte[] bytes = await BlobLogic.DownloadZip_Bytes(Connections);

    return new FileContentResult(bytes, "application/zip")
    {
        FileDownloadName = "bytes.zip"
    };
}

public async Task DownloadZip_FileStream(IEnumerable<IBlobConnection> p_Connections)
{
    Directory.CreateDirectory("./test-data/");

    using FileStream s_ZipStream = new FileStream("./test-data/file-stream.zip", FileMode.Create);
    using ZipArchive s_ZipArchive = new ZipArchive(s_ZipStream, ZipArchiveMode.Create, true);

    foreach (IBlobConnection connection in p_Connections)
    {
        ZipArchiveEntry entry = s_ZipArchive.CreateEntry($"{Guid.NewGuid()}{connection.FileEnding}");

        CloudBlockBlob s_Block = GetBlockReference(connection);

        using Stream entrySteram = entry.Open();

        await s_Block.DownloadToStreamAsync(entrySteram);
    }
}

public async Task<byte[]> DownloadZip_Bytes(IEnumerable<IBlobConnection> p_Connections)
{
    using MemoryStream s_ZipStream = new MemoryStream();
    using ZipArchive s_ZipArchive = new ZipArchive(s_ZipStream, ZipArchiveMode.Create, true);

    foreach (IBlobConnection connection in p_Connections)
    {
        ZipArchiveEntry entry = s_ZipArchive.CreateEntry($"{Guid.NewGuid()}{connection.FileEnding}");

        CloudBlockBlob s_Block = GetBlockReference(connection);

        using Stream entrySteram = entry.Open();

        await s_Block.DownloadToStreamAsync(entrySteram);
    }

    s_ZipStream.Seek(0, SeekOrigin.Begin);

    return s_ZipStream.ToArray();
}
public async Task<byte[]> DownloadZip_Bytes(IEnumerable<IBlobConnection> p_Connections)
{
    using MemoryStream zipStream = new MemoryStream();

    using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
    {
        foreach (IBlobConnection connection in p_Connections)
        {
            ZipArchiveEntry entry = zipArchive.CreateEntry($"{Guid.NewGuid()}{connection.FileEnding}");

            CloudBlockBlob block = GetBlockReference(connection);

            using Stream entrySteram = entry.Open();

            await block.DownloadToStreamAsync(entrySteram);
        }
    }

    return zipStream.ToArray();
}