简体   繁体   English

如何从MemoryStream重新创建Zip文件

[英]How to recreate a Zip File from a MemoryStream

I've looked around for a solution to my issue but no-one seems to being aiming for quite what I'm trying to achieve. 我一直在寻找解决问题的方法,但似乎没有人针对我要实现的目标。

My problem is such, I have Zip files stored in Azure Blob storage, now for security's sake we have an API2 controller action that provisions these zip files, rather than allowing direct downloading. 我的问题是这样的,我在Azure Blob存储中存储了Zip文件,为了安全起见,现在我们有一个API2控制器操作来配置这些zip文件,而不是允许直接下载。 This action will retrieve the blob, and download it to a stream, so that it can be packaged within a HTTPResponseMessage . 此操作将检索Blob,并将其下载到流中,以便可以将其打包在HTTPResponseMessage

All of the above works, however, when I attempt to recreate the zip file, I'm informed it's corrupted. 上述所有工作均有效,但是,当我尝试重新创建zip文件时,得知它已损坏。 For now I'm just attempting to have the server (running on localhost) create the zip file, whereas the endgame is to have remote Client applications do this (I'm fairly certain the solution to my issue on the server would be the same. 现在,我只是尝试让服务器(在localhost上运行)创建zip文件,而最终目标是让远程客户端应用程序执行此操作(我很确定服务器上的问题的解决方案是相同的。

public class FileActionResult : IHttpActionResult 
{
  private HttpRequestMessage _request;
  private ICloudBlob _blob;

  public FileActionResult(HttpRequestMessage request, ICloudBlob blob)
  {
    _request = request;
    _blob = blob;
  }

  public async Task<HttpResponseMessage> ExecuteAsync(System.Threading.CancellationToken cancellationToken)
  {
    var fileStream = new MemoryStream();
    await _blob.DownloadToStreamAsync(fileStream);

    var byteTest = new byte[fileStream.Length];
    var test = fileStream.Read(byteTest, 0, (int)fileStream.Length);

    try
    {
      File.WriteAllBytes(@"C:\testPlatFiles\test.zip", byteTest);
    }
    catch(ArgumentException ex)
    {
      var a = ex;
    }

    var response = _request.CreateResponse(HttpStatusCode.Accepted);
    response.Content = new StreamContent(fileStream);
    response.Content.Headers.ContentLength = _blob.Properties.Length;
    response.Content.Headers.ContentType = new MediaTypeHeaderValue(_blob.Properties.ContentType);
    //set the fileName
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
      FileName = _blob.Name,
      Size = _blob.Properties.Length
    };
    return response;  
  }
}

I've looked into Zip libraries to see if any present the solution for converting the stream of a zip back to a zip file, but all I can find is reading zip files into streams, or the creation in order to provision a file download instead of a filecreate. 我研究了Zip库,看是否存在将zip流转换回zip文件的解决方案,但是我所能找到的只是将zip文件读入流,或者是为了提供文件下载而创建的文件创建。

Any help would be much appreciated, thank you. 任何帮助将不胜感激,谢谢。

You use DotNetZip . 您使用DotNetZip Its ZipFile class has a static factory method that should do what you want: ZipFile.Read( Stream zipStream ) reads the given stream as a zip file and gives you back a ZipFile instance (which you can use for whatever. 它的ZipFile类具有静态工厂方法,该方法应该执行您想要的操作: ZipFile.Read( Stream zipStream )将给定的流作为zip文件读取,并为您提供ZipFile实例(您可以将其用于任何用途)。

However, if your Stream contains the raw zip data and all you want to do is persist it to disk, you should just be able to write the bytes straight to disk. 但是,如果您的Stream包含原始zip数据,而您要做的只是将其持久化到磁盘,则您应该能够直接将字节写入磁盘。

If you're getting 'zip file corrupted' errors, I'd look at the content encoding used to send the data to Azure and the content encoding it's sent back with. 如果遇到“ zip文件损坏”错误,我将查看用于将数据发送到Azure的内容编码以及将其发送回的内容编码。 You should be sending it up to Azure with a content type of application/zip or application/octet-stream and possibly adding metadata to the Azure blob entry to send it down the same way. 您应该使用application/zipapplication/octet-stream的内容类型将其发送到Azure,并可能将元数据添加到Azure blob条目中以相同的方式向下发送。


Edited To Note: DotNetZip used to live at Codeplex. 编辑要注意: DotNetZip曾经居住在Codeplex。 Codeplex has been shut down. Codeplex已关闭。 The old archive is still available at Codeplex . 旧的存档仍可在Codeplex上获得 It looks like the code has migrated to Github: 看起来代码已迁移到Github:


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM