编程语言
首页 > 编程语言> > c# – 将ZipArchive条目提取到blob存储

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

作者:互联网

使用普通的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,net,azure,azure-storage-blobs,azure-storage
来源: https://codeday.me/bug/20190624/1281995.html