我们有一个要求提取大尺寸的.zip文件(约3-4 GB大小)在斑点容器到其他斑点容器和提取的文件是杰森文件(约35 -50 GB大小)。
参考此链接的实现代码:https://msdevzone.wordpress.com/2017/07/07/extract-a-zip-file-stored-in-azure-blob/,能够提取较小的文件,大小为40MB,解压缩到400MB只需几分钟,但由于2 GB文件大小解压到30 GB的JSON文件而卡住了一个多小时。
有没有人能建议他们在不使用文件操作的情况下遇到更好的解决方案?
请参考下面我们编写的代码:
CloudBlockBlob blockBlob = container.GetBlockBlobReference(filename);
BlobRequestOptions options = new BlobRequestOptions();
options.ServerTimeout = new TimeSpan(0, 20, 0);
// Save blob(zip file) contents to a Memory Stream.
using (MemoryStream zipBlobFileStream = new MemoryStream())
{
//blockBlob.Properties.LeaseDuration
blockBlob.DownloadToStream(zipBlobFileStream, null, options);
zipBlobFileStream.Flush();
zipBlobFileStream.Position = 0;
//use ZipArchive from System.IO.Compression to extract all the files from zip file
using (ZipArchive zip = new ZipArchive(zipBlobFileStream, ZipArchiveMode.Read, true))
{
//Each entry here represents an individual file or a folder
foreach (var entry in zip.Entries)
{
//creating an empty file (blobkBlob) for the actual file with the same name of file
var blob = extractcontainer.GetBlockBlobReference(entry.FullName);
using (var stream = entry.Open())
{
//check for file or folder and update the above blob reference with actual content from stream
if (entry.Length > 0)
blob.UploadFromStream(stream);
}
}
}
}发布于 2019-05-31 23:44:55
使用Azure存储文件共享,这是我使用它的唯一方式,而无需将整个ZIP加载到内存中。我测试了一个3 3GB的ZIP文件(有数千个文件,或者里面有一个大文件),内存/CPU很低,而且很稳定。也许你可以适应BlockBlobs。我希望它能有所帮助!
var zipFiles = _directory.ListFilesAndDirectories()
.OfType<CloudFile>()
.Where(x => x.Name.ToLower().Contains(".zip"))
.ToList();
foreach (var zipFile in zipFiles)
{
using (var zipArchive = new ZipArchive(zipFile.OpenRead()))
{
foreach (var entry in zipArchive.Entries)
{
if (entry.Length > 0)
{
CloudFile extractedFile = _directory.GetFileReference(entry.Name);
using (var entryStream = entry.Open())
{
byte[] buffer = new byte[16 * 1024];
using (var ms = extractedFile.OpenWrite(entry.Length))
{
int read;
while ((read = entryStream.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
}
}
}
}
}发布于 2019-03-10 19:04:00
您引用的方法将不起作用,因为它使用内存流,而下面的行将所有数据加载到内存中时会导致内存不足。
blob.DownloadToStream(memoryStream);为了解决这个问题,我遵循了this blog post的说明。我对代码所做的唯一更改是将await添加到此行,
await blockBlob.UploadFromStreamAsync(fileStream);希望这能有所帮助。
发布于 2018-03-06 17:55:50
如果你需要解压Azure Storage中的大量文件,那么一个选择就是使用。
Azure Batch使您能够在云中高效地运行大规模并行和高性能计算 (HPC)应用程序。
它将为您管理计算集群,您只需创建逻辑并提交到批处理服务以跨节点执行即可。
您可以使用blob流函数,将blob下载为流,使用ZipArchive类提取它,然后将其上传到输出容器。
using (Stream memoryStream = new MemoryStream())
{
blob.DownloadToStream(memoryStream);
memoryStream.Position = 0; //Reset the stream
ZipArchive archive = new ZipArchive(memoryStream);
Console.WriteLine("Extracting {0} which contains {1} files", blobName, archive.Entries.Count);
foreach (ZipArchiveEntry entry in archive.Entries)
{
CloudBlockBlob blockBlob = outputContainer.GetBlockBlobReference(entry.Name);
blockBlob.UploadFromStream(entry.Open());
Console.WriteLine("Uploaded {0}", entry.Name);
}
}有关更详细的代码,您可以参考此sample。
https://stackoverflow.com/questions/49105491
复制相似问题