使用普通的 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/