gpt4 book ai didi

c# - 将 ZipArchive 条目提取到 blob 存储

转载 作者:太空狗 更新时间:2023-10-30 01:20:24 26 4
gpt4 key购买 nike

使用普通的 Windows 文件系统,ExtractToFile 方法就足够了:

using (ZipArchive archive = new ZipArchive(uploadedFile.InputStream, ZipArchiveMode.Read, true))
{
foreach (var entry in archive.Entries.Where(x => x.Length > 0))
{
entry.ExtractToFile(Path.Combine(location, entry.Name));
}
}

现在我们正在使用 Azure,这显然需要改变,因为我们正在使用 blob 存储。

如何做到这一点?

最佳答案

ZipArchiveEntry类有一个 Open返回流的方法。您可以做的是使用该流创建一个 blob。

static void ZipArchiveTest()
{
storageAccount = CloudStorageAccount.DevelopmentStorageAccount;
CloudBlobContainer container = storageAccount.CreateCloudBlobClient().GetContainerReference("temp");
container.CreateIfNotExists();
var zipFile = @"D:\node\test2.zip";
using (FileStream fs = new FileStream(zipFile, FileMode.Open))
{
using (ZipArchive archive = new ZipArchive(fs))
{
var entries = archive.Entries;
foreach (var entry in entries)
{
CloudBlockBlob blob = container.GetBlockBlobReference(entry.FullName);
using (var stream = entry.Open())
{
blob.UploadFromStream(stream);
}
}
}
}
}

关于c# - 将 ZipArchive 条目提取到 blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19248934/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com