2012-12-18 5 views
2

제가하고 싶은 것은 디스크에 zip 파일을 저장하는 대신 MemoryStream에서 열어 보는 것입니다.C# - DotNetZip을 MemoryStream에서 엽니 다.

DotNetZip 프로그래밍 예제에 대한 설명서를보고 있습니다 : 필자는 필요할 것으로 생각되는 것에 따라 약간 수정했습니다.

var ms = new MemoryStream(); 
    using (ZipFile zip = new ZipFile()) 
    { 
     zip.AddFile("ReadMe.txt"); 
     zip.AddFile("7440-N49th.png"); 
     zip.AddFile("2008_Annual_Report.pdf");   
     zip.Save(ms); // this will save the files in memory steam 
    } 


    // now what I need is for the zip file to open up so that 
    the user can view all the files in it. Not sure what to do next after 
    zip.Save(ms) for this to happen. 
+0

오픈으로 무엇을 의미합니까? 다른 프로그램에서 열리나요? –

+0

환경이란 무엇입니까? 예를 들어 ASP.NET MVC에서이 작업을 수행하고 있습니까? 그렇다면 예제 코드를 게시 할 수 있습니다. –

+0

@Dommer - 예, ASP.NET MVC C#에 있습니다. 매우 도움이 될 것입니다. –

답변

4

이 시도 : 우리가 출력 스트림에 지퍼를 쓸 수

public ActionResult Index() 
{ 
    var memoryStream = new MemoryStream(); 

    using (var zip = new ZipFile()) 
    { 
     zip.AddFile("ReadMe.txt"); 
     zip.AddFile("7440-N49th.png"); 
     zip.AddFile("2008_Annual_Report.pdf"); 
     zip.Save(memoryStream); 
    } 

    memoryStream.Seek(0, 0); 
    return File(memoryStream, "application/octet-stream", "archive.zip"); 
} 
+0

저는 왜 이런 식으로 파일을 반환해야하는지 알고 싶습니다. 왜냐하면 dotnetzip 라이브러리가 zip 파일을 출력 스트림에 직접 쓸 수 있기 때문입니다. 여기에 작은 스 니프'zip.Save (Response.OutputStream);'왜 이런 식으로 클라이언트에 zip 파일을 반환하는지 객관적으로 설명 할 수 있습니다. 'return file (memoryStream, "application/octet-stream", "archive.zip") '제발 여기에 내 게시물을보고 가능한 경우 내 질문에 대답하십시오. 감사 – Mou

0

로컬 인 경우 스트림을 파일에 저장하고 Process.Start을 호출해야합니다.

서버에있는 경우. 적절한 MIME 형식으로 응답에 ms를 작성하십시오.

0

당신은 메모리의 내용을 보낼 수있는 응답으로 다시 스트리밍 할 것 :

using (MemoryStream ms = new MemoryStream()) 
{ 
    using (ZipFile zip = new ZipFile()) 
    { 
     zip.AddFile("ReadMe.txt"); 
     zip.AddFile("7440-N49th.png"); 
     zip.AddFile("2008_Annual_Report.pdf");   
     zip.Save(ms); // this will save the files in memory steam 
    } 

    context.Response.ContentType = "application/zip"; 
    context.Response.AddHeader("Content-Length", ms.Size); 
    context.Response.AddHeader("Content-disposition", "attachment; filename=MyZipFile.zip"); 
    ms.Seek(0, SeekOrigin.Begin); 
    ms.WriteTo(context.Response.OutputStream); 
} 
0

조금이 같은 ActionResult 만들어보십시오 : 나는 라인 확실 100 % 아니에요 을 var fileData = ms; 및 나는 지금 막 dev 환경에 접근이 없다, 그러나 당신이 그것을 운동하는 이젠 그만이어야한다.

public ActionResult DownloadZip() 
{ 
    using (MemoryStream ms = new MemoryStream()) 
    { 
     using (ZipFile zip = new ZipFile()) 
     { 
     zip.AddFile("ReadMe.txt"); 
     zip.AddFile("7440-N49th.png"); 
     zip.AddFile("2008_Annual_Report.pdf");   
     zip.Save(ms); // this will save the files in memory steam 
     } 
     byte[] fileData = ms.GetBuffer();// I think this will work. Last time I did it, I did something like this instead... Zip.CreateZip("LogPosts.csv", System.Text.Encoding.UTF8.GetBytes(csv)); 
     var cd = new System.Net.Mime.ContentDisposition 
     { 
      FileName = "Whatever.zip", 
      // always prompt the user for downloading, set to true if you want 
      // the browser to try to show the file inline 
      Inline = false, 
     }; 
     Response.AppendHeader("Content-Disposition", cd.ToString()); 
     return File(fileData, "application/octet-stream"); 
     } 
    } 
+1

문제의 라인은'byte [] fileData = ms.GetBuffer();'이어야한다고 생각합니다. –

+0

고마워요 Dommer - 나 한 번 시도해 보자! –

+0

Thanks @ThorstenDittmar :-) 나는 나의 대답을 업데이트했다. –

0

이런 식으로. 도움을 받으시겠습니까

ZipFile zip = new ZipFile(); 
    List<Attachment> listattachments = email.Attachments; 
     int acount = attachments.Count; 
     for (int i = 0; i < acount; i++) 
     { 
      zip.AddEntry(attachments[i].FileName, listattachments[i].Content); 
     } 
     Response.Clear(); 
     Response.BufferOutput = false; 
     string zipName = String.Format("{0}.zip", message.Headers.From.DisplayName); 
     Response.ContentType = "application/zip"; 
     Response.AddHeader("content-disposition", "attachment; filename=" + zipName); 
     zip.Save(Response.OutputStream); 
     Response.End();