生成蔚蓝Blob存储文件的Zip文件 [英] generate a Zip file from azure blob storage files

查看:140
本文介绍了生成蔚蓝Blob存储文件的Zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我保存在我的Windows Azure Blob存储一些文件。 IWant采取这些文件并创建一个zip文件,并存储到一个新的文件夹。于是路径返回zip文件。
设置权限zip文件位置,以便我的用户可以通过单击链接

I have some files stored in my windows azure blob storage. IWant to take these files and create a zip file and store to a new folder .Then return the path to the zip file. Set permission to the zip file location so that my users can download the zip file to their local machines by clicking on the link

 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/AppList/isjirleq/mydocs1.doc
 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/tempo/xyz/mymusic.mp3
 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/general/video/myVideo.wmv
 https://mystorage.blob.core.windows.net/myfiles/2b5f8ea6-3dc2-4b77-abfe-4da832e02556/photo/photo1.png

我希望能够循环therough这些文件和zip他们一起创建一个新的zip文件

I want to be able to loop therough these files and zip them all together to create a new zip file

https://mystorage.blob.core.windows.net/myzippedfiles/allmyFiles.zip )和路径返回zip文件

(https://mystorage.blob.core.windows.net/myzippedfiles/allmyFiles.zip ) and return the path to the zip file

我在蔚蓝的BLOB大量文件。所以下载这些和他们荏苒和上传是不是一个好主意。

I have large number of files in my azure blob. So downloading them and zipping them and uploading is not a good idea.

我怎样才能做到这一点。我需要一些示例code做到这一点。

How can I do this .I need some sample code to do this

推荐答案

我们已经使用一滴流荏苒直接将文件输出流解决了这个问题(部分)。这样就避免了下载荏苒然后再送的问题,并避免了延迟,而这种情况发生(我们使用ICSharpZipLib,参考)。但它仍然是指通过Web服务器路由数据流:

We have solved this problem (partially) by zipping the files directly to the output stream using the blob streams. This avoids the issue of downloading zipping then sending and avoids the delay while this happens (we used ICSharpZipLib, reference). But it still means routing the stream through the web server:

  public void ZipFilesToResponse(HttpResponseBase response, IEnumerable<Asset> files, string zipFileName)
    {
        using (var zipOutputStream = new ZipOutputStream(response.OutputStream))
        {
            zipOutputStream.SetLevel(0); // 0 - store only to 9 - means best compression
            response.BufferOutput = false;
            response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
            response.ContentType = "application/octet-stream";

            foreach (var file in files)
            {
                var entry = new ZipEntry(file.FilenameSlug())
                {
                    DateTime = DateTime.Now,
                    Size = file.Filesize
                };
                zipOutputStream.PutNextEntry(entry);
                storageService.ReadToStream(file, zipOutputStream);
                response.Flush();
                if (!response.IsClientConnected)
                {
                   break;
                }
            }
            zipOutputStream.Finish();
            zipOutputStream.Close();
        }
        response.End();
    }

存储服务简单地做到这一点:

The storage service simply does this:

public void ReadToStream(IFileIdentifier file, Stream stream, StorageType storageType = StorageType.Stored, ITenant overrideTenant = null)
    {
        var reference = GetBlobReference(file, storageType, overrideTenant);
        reference.DownloadToStream(stream);
    }
private CloudBlockBlob GetBlobReference(IFileIdentifier file, StorageType storageType = StorageType.Stored, ITenant overrideTenant = null)
        {
            var filepath = GetFilePath(file, storageType);
            var container = GetTenantContainer(overrideTenant);
            return container.GetBlockBlobReference(filepath);
        }

这篇关于生成蔚蓝Blob存储文件的Zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆