从zip返回流文件 [英] Return a stream file from a zip

查看:130
本文介绍了从zip返回流文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一大堆图像存储在zip文件中。我试图从我的Web Api 2应用程序返回图像。任何人都可以建议一个聪明的方法直接从拉链返回流然后处理它(下面的代码)?



我正在考虑废除'zip'计划并将图像存储为平面文件,但实际上有数百万个。我担心他们将在磁盘上占用多少空间。



我正在寻找从请求时开始提供图像的最快方式。这是我对这个PoC的主要关注点。



谢谢^ _ ^

Andy



我尝试过:



目前我正在将流复制到内存流:



我将路径存储为C:\ ImageHib \1045.zip \ GS67801_POS_Image.jpg

Hi,

I have a load of images stored in zip files. I am trying to return the image from my Web Api 2 application. Can anyone suggest a clever way to return the stream directly from the zip and then dispose of it (code below)?

I'm considering scrapping the 'zip' plan and storing the images as flat files, but there are literally millions of them. I worry about how much space they will take on disk.

I am looking for the fastest possible way to deliver the image from the time of request. That is my main concern for this PoC.

Thanks ^_^
Andy

What I have tried:

Currently I am copying the stream to a memory stream:

I store the path as "C:\ImageLib\1045.zip\GS67801_POS_Image.jpg"

private Stream GetImageByItemCode(string itemCode)
        {
            var item = ItemType.SelectByItemCode(itemCode).FirstOrDefault();
            if (item == null)
                return null;

            if (item.PosImagePath == null)
                return null;
            MemoryStream ms = new MemoryStream();
            if (item.PosImagePath.IndexOf(".zip") > 0)
            {
                var zipPath = item.PosImagePath.Substring(0, item.PosImagePath.IndexOf(".zip") + 4);
                var filename = item.PosImagePath.Substring(item.PosImagePath.IndexOf(".zip") + 5);
                var zipFile = ZipFile.Open(zipPath, ZipArchiveMode.Read);

                var entry = zipFile.Entries.Where(e => e.FullName == filename).FirstOrDefault();
                if (entry == null)
                    return null;

                entry.Open().CopyTo(ms);

                ms.Seek(0, SeekOrigin.Begin);
            }
            return ms;
        }



这是一个PoC,所以我没有优化我如何存储文件名atm。这只是我稍后可以处理的细节。



此方法用于创建HttpResponse:


This is a PoC so I haven't optimized how I store the filename atm. That's just detail I can deal with later.

This method is used to create an HttpResponse:

public HttpResponseMessage GetImageImg(string itemCode, int size)
{
    Stream imageStream = GetImageByItemCode(itemCode);

    //var imageArray = ResizeImage(image, size);



    var response = Request.CreateResponse();
    if (imageStream == null)
    {
        return response;
    }
    response.Content = new StreamContent(imageStream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
    return response;
}



由我的控制器在这里返回:




which is returned by my controller here:

[Route("{itemCode}/{size:int?}")]
   public object GetImage(string itemCode, int size = 1)
   {
       var query = Request.GetQueryNameValuePairs().ToDictionary(p => p.Key.ToLower(), p => p.Value.ToLower());
       var format = "img";
       if (query.ContainsKey("format"))
       {
           format = query["format"];
       }
       switch (format)
       {
           default:
               return GetImageImg(itemCode, size);
       }
   }



不要担心所有格式化废话。最终大小/格式将作为单独的文件存储在我的lib中。


Don't worry about all that formatting crap. Eventually the size / format will be stored in my lib as separate files.

推荐答案

JPG已经是一种压缩格式。



.ZIP文件中压缩了多少这些图像?大多数图像格式,如PNG,JPG,TIFF,......都已经过压缩,因此将它们放入.ZIP文件中并没有太大的帮助。你可能会得到1%到2%的压缩。



或者,如果你使用非压缩格式,比如BMP,你可以逃脱转换和使用压缩版本?某些格式和设置将是有损的,而其他格式和设置则不会。这取决于图像。



至于文件的流操作,这取决于你正在使用的ZIP库。如果它是.NET Framework中内置的System.IO.Compression命名空间中的一个,则可以使用 ZipArchive.GetEntry Method(String)(System.IO.Compression) [ ^ ]获取的方法ZipArchiveEntry对象,然后使用 ZipArchiveEntry。 Open Method(System.IO.Compression) [ ^ ]获取流的方法。
JPG is a compressed format already.

How much are these images being compressed in the .ZIP files? Most image formats, like PNG, JPG, TIFF, ... are already compressed so you're not really getting much by putting them into .ZIP files. You might be getting 1% to 2% compression.

Or, if you're using a non-compressed format, like BMP, can you get away with converting and using a compressed version? Some formats and settings will be lossy and others won't. It depends on the images.

As for the stream operation on the file, that depends on the ZIP library you're using. If it's the one in the System.IO.Compression namespace built into the .NET Framework, you can get the entry for the file using the ZipArchive.GetEntry Method (String) (System.IO.Compression)[^] method to get a ZipArchiveEntry object and then use the ZipArchiveEntry.Open Method (System.IO.Compression)[^] method to get the stream.


这篇关于从zip返回流文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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