2012-04-04 143 views
5

基本上用戶應該能夠點擊一個鏈接並下載多個PDF文件。但是Catch是我無法在服務器或任何地方創建文件的。一切都必須記憶。從內存流C創建Zip文件#

我能夠創建內存流和Response.Flush()它作爲pdf,但我怎麼壓縮多個內存流而不創建文件。

這裏是我的代碼:

Response.ContentType = "application/zip"; 

      // If the browser is receiving a mangled zipfile, IIS Compression may cause this problem. Some members have found that 
      // Response.ContentType = "application/octet-stream" has solved this. May be specific to Internet Explorer. 
      Response.AppendHeader("content-disposition", "attachment; filename=\"Download.zip\""); 
      Response.CacheControl = "Private"; 
      Response.Cache.SetExpires(DateTime.Now.AddMinutes(3)); // or put a timestamp in the filename in the content-disposition     

      byte[] abyBuffer = new byte[4096]; 

      ZipOutputStream outStream = new ZipOutputStream(Response.OutputStream); 

      outStream.SetLevel(3); 

      #region Repeat for each Memory Stream 
      MemoryStream fStream = CreateClassroomRoster();// This returns a memory stream with pdf document 

      ZipEntry objZipEntry = new ZipEntry(ZipEntry.CleanName("ClassroomRoster.pdf")); 

      objZipEntry.DateTime = DateTime.Now; 

      objZipEntry.Size = fStream.Length; 

      outStream.PutNextEntry(objZipEntry); 

      int count = fStream.Read(abyBuffer, 0, abyBuffer.Length); 

      while (count > 0) 
      { 
       outStream.Write(abyBuffer, 0, count); 

       count = fStream.Read(abyBuffer, 0, abyBuffer.Length); 

       if (!Response.IsClientConnected) 
       { 
        break; 
       } 

       Response.Flush(); 
      } 

      fStream.Close(); 
      #endregion 

      outStream.Finish(); 
      outStream.Close(); 

      Response.Flush(); 
      Response.End(); 

這將創建一個zip文件,但裏面有它沒有文件

我使用 使用iTextSharp.text - 使用ICSharpCode.SharpZipLib.Zip創建PDF - 爲荏苒

感謝, 卡維塔

+0

您是否曾經找到過解決方案?我需要做同樣的事情。 – sbonkosky 2012-12-19 16:02:16

+0

不,我沒有..我嘗試了所有的解決方案,但沒有任何工作..希望你找到它 – Arshya 2012-12-19 17:14:19

+0

這個鏈接可能會有所幫助:http://snipplr.com/view/47762/ – updev 2013-11-19 21:49:31

回答

13

此鏈接介紹如何使用SharpZipLib從MemoryStream創建一個zip文件:https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples#wiki-anchorMemory。使用這個和iTextSharp,我可以壓縮在內存中創建的多個PDF文件。

這裏是我的代碼:

 MemoryStream outputMemStream = new MemoryStream(); 
     ZipOutputStream zipStream = new ZipOutputStream(outputMemStream); 

     zipStream.SetLevel(3); //0-9, 9 being the highest level of compression 
     byte[] bytes = null; 

     // loops through the PDFs I need to create 
     foreach (var record in records) 
     { 
      var newEntry = new ZipEntry("test" + i + ".pdf"); 
      newEntry.DateTime = DateTime.Now; 

      zipStream.PutNextEntry(newEntry); 

      bytes = CreatePDF(++i); 

      MemoryStream inStream = new MemoryStream(bytes); 
      StreamUtils.Copy(inStream, zipStream, new byte[4096]); 
      inStream.Close(); 
      zipStream.CloseEntry(); 

     } 

     zipStream.IsStreamOwner = false; // False stops the Close also Closing the underlying stream. 
     zipStream.Close();   // Must finish the ZipOutputStream before using outputMemStream. 

     outputMemStream.Position = 0; 

     return File(outputMemStream.ToArray(), "application/octet-stream", "reports.zip"); 

的CreatePDF方法:

private static byte[] CreatePDF(int i) 
    { 
     byte[] bytes = null; 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      Document document = new Document(PageSize.A4, 25, 25, 30, 30); 
      PdfWriter writer = PdfWriter.GetInstance(document, ms); 
      document.Open(); 
      document.Add(new Paragraph("Hello World " + i)); 
      document.Close(); 
      writer.Close(); 
      bytes = ms.ToArray(); 
     } 
     return bytes; 

    } 
+0

謝謝你的答案,因爲它爲我工作以及。你能告訴我如何壓縮文本文件嗎? – 2016-10-05 10:10:40

+0

工程就像一個魅力!謝謝。 – ahmet 2017-09-11 13:39:41

0

您可以生成PDF文件並將其保存在IsolatedStorageFileStream那麼你可以壓縮從存儲的內容。

0

此代碼將幫助您創建多個pdf文件的Zip,您將從下載鏈接中獲取每個文件。

 using (var outStream = new MemoryStream()) 
       { 
        using (var archive = new ZipArchive(outStream, ZipArchiveMode.Create, true)) 
        { 
         for (String Url in UrlList) 
         { 
          WebRequest req = WebRequest.Create(Url); 
          req.Method = "GET"; 
          var fileInArchive = archive.CreateEntry("FileName"+i+ ".pdf", CompressionLevel.Optimal); 
          using (var entryStream = fileInArchive.Open()) 
          using (WebResponse response = req.GetResponse()) 
          { 
           using (var fileToCompressStream = response.GetResponseStream()) 
           { 
            entryStream.Flush(); 
            fileToCompressStream.CopyTo(entryStream); 
            fileToCompressStream.Flush(); 
           } 
          } 
          i++; 
         } 

        } 
        using (var fileStream = new FileStream(@"D:\test.zip", FileMode.Create)) 
        { 
         outStream.Seek(0, SeekOrigin.Begin); 
         outStream.CopyTo(fileStream); 
        } 
       } 

命名空間需要: System.IO.Compression; System.IO.Compression.ZipArchive;

0

下面是使用ICSharpCode.SharpZipLib dll中存在的ZipOutputStream類在MemoryStream中創建zip文件的代碼。

FileStream fileStream = File.OpenRead(@"G:\1.pdf"); 
MemoryStream MS = new MemoryStream(); 

byte[] buffer = new byte[fileStream.Length]; 
int byteRead = 0; 

ZipOutputStream zipOutputStream = new ZipOutputStream(MS); 
zipOutputStream.SetLevel(9); //Set the compression level(0-9) 
ZipEntry entry = new ZipEntry(@"1.pdf");//Create a file that is needs to be compressed 
zipOutputStream.PutNextEntry(entry);//put the entry in zip 

//Writes the data into file in memory stream for compression 
while ((byteRead = fileStream.Read(buffer, 0, buffer.Length)) > 0) 
    zipOutputStream.Write(buffer, 0, byteRead); 

zipOutputStream.IsStreamOwner = false; 
fileStream.Close(); 
zipOutputStream.Close(); 
MS.Position = 0; 
相關問題