[
  {
    "Id": "248207",
    "ThreadId": "72684",
    "Html": "<p>I have been looking at your great library, but I do not know if it will be able to do this or do not see a way as yet.&nbsp; I am reading chunks on binary data (actually very large XML document)&nbsp;from a SQL database as reading the data into a single Stream in one go will cause an Out of Memory error.&nbsp; I have the chunking code for downloading to the browser ie.</p>\r\n<p>&nbsp;DataExportStreamer XMLData = new DataExportStreamer(DataExportReportID);<br>while (!XMLData.IsStreamEnd)<br>{<br>&nbsp;&nbsp; if (XMLData.ReadDataSheetStream())<br>&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; byte[] RawData = XMLData.XMLExcelSpreadsheet;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.OutputStream.Write(RawData, 0, RawData.Length);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Response.OutputStream.Flush();<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; XMLDataRead = true;<br>&nbsp;&nbsp; }<br>}</p>\r\n<p>I want to now zip the data to a file location instead of streaming it to the browser,&nbsp; Is there a way to do this?</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-10-21T10:03:59.033-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "248236",
    "ThreadId": "72684",
    "Html": "<p>I don't understand what's special about your requirements.&nbsp;&nbsp; Yes, you can zip the data to a file location.</p>\r\n<p>What's the part you are stuck on?</p>\r\n<p>If on the server side, you can open a stream that you can read from, then you can use <a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/7bc513f6-c21c-6a02-3964-f2a571308a33.htm\">the AddEntry() overload that accepts a stream as input</a>.&nbsp; When you call ZipFile.Save(), the stream is read, and compressed (and maybe encrypted) as it is streamed to the zip archive.&nbsp; Check the code example on the doc page.&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>String zipToCreate = <span style=\"color:#A31515\">&quot;Content.zip&quot;</span>;\r\nString fileNameInArchive = <span style=\"color:#A31515\">&quot;Content-From-Stream.bin&quot;</span>;\r\n<span style=\"color:Blue\">using</span> (System.IO.Stream streamToRead = MyStreamOpener())\r\n{\r\n  <span style=\"color:Blue\">using</span> (ZipFile zip = <span style=\"color:Blue\">new</span> ZipFile())\r\n  {\r\n    ZipEntry entry= zip.AddEntry(fileNameInArchive, streamToRead);\r\n    zip.AddFile(<span style=\"color:#A31515\">&quot;Readme.txt&quot;</span>);\r\n    zip.Save(zipToCreate);  <span style=\"color:Green\">// the stream is read implicitly here</span>\r\n  }\r\n}\r\n\r\n</pre>\r\n</div>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-10-21T11:58:48.693-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "248484",
    "ThreadId": "72684",
    "Html": "<p>Thanks for your reply, When the zip.Save method is called, what methods and properties does the MyStreamOpener object&nbsp;have to support?&nbsp; Do you have an example of&nbsp;a MyStreamOpener class?&nbsp;</p>",
    "PostedDate": "2009-10-22T02:53:23.347-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "248668",
    "ThreadId": "72684",
    "Html": "<p>In the example code, MyStreamOpener() is just a stand-in method - you replace that with whatever opens your stream.&nbsp; It has to return a System.IO.Stream.&nbsp; I thought you said you were reading chunks for a stream, which sounded to me, like you already had a stream.&nbsp; In which case, you already have a method (I guess) that opens the stream.</p>\r\n<p>Let's take a step back.</p>\r\n<p>DotNetZip models the zip file as a container, in the ZipFile class.&nbsp; This container holds entries, each represented by the ZipEntry class.&nbsp; When extracting from a zip file (call ZipEntry.Extract() method), the normal way of operation is that each entry results in the creation of a file or directory in the filesystem.&nbsp; When creating a zip file, you can create file entries from one of various sources.&nbsp; The most obvious source is a filesystem file itself (<a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/b1d9ff87-214d-d219-af0c-8075512cb3a9.htm\">ZipFile.AddFile()</a>).&nbsp; An alternative source is an arbitrary stream, via <a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/7bc513f6-c21c-6a02-3964-f2a571308a33.htm\">ZipFile.AddEntry(string name, Stream stream)</a>.&nbsp; That's what I show in the example above.&nbsp;&nbsp;</p>\r\n<p>Another alternative is to create a ZipEntry using data stored in a byte array as the source.&nbsp; (<a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/6f8514c3-e4f0-235d-def4-48a235ce7694.htm\">ZipFile.AddEntry(string name, byte[] byteContent)</a> ).</p>\r\n<p>Another alternative is to call the <a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/633c4280-51ca-cc82-b5b5-86bbe9b2e947.htm\">ZipFile.AddEntry() method that accepts a WriteDelegate</a>.&nbsp; With this option, the application (your code) writes the data into the ZipEntry, and the data need not come from a stream.&nbsp; The key example here is saving a DataSet into a ZipEntry.</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre><span style=\"color:Blue\">private</span> <span style=\"color:Blue\">void</span> WriteEntry (String filename, Stream output)\r\n{\r\n    DataSet ds1 = ObtainDataSet();\r\n    ds1.WriteXml(stream);\r\n}\r\n\r\n<span style=\"color:Blue\">private</span> <span style=\"color:Blue\">void</span> Run()\r\n{\r\n    Using zip = New ZipFile\r\n    {\r\n        zip.AddEntry(zipEntryName, WriteEntry);\r\n        zip.Save(zipFileName);\r\n    }\r\n}\r\n</pre>\r\n</div>\r\n<p>In this last approach, obviously the data does not have to come from a DataSet.&nbsp; It can come from anywhere.&nbsp; The point is, at the time of ZipFile.Save(), your code&nbsp;(via the WriteEntry method, or more accurately, any WriteDelegate) writes the entry content directly into the ZipFile.</p>\r\n<p>You have to decide the best alternative for yourself. It depends on how your app accesses the entry content. Proabably the AddEntry() method that accepts a Stream or WriteDelegate is best for you.</p>",
    "PostedDate": "2009-10-22T10:19:42.367-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "248703",
    "ThreadId": "72684",
    "Html": "<p>Stu, in looking again at your code, I don't see where the streaming is happening.</p>\r\n<p>It seems to me you write a byte array out to the Response.OutputStream.&nbsp; If that's the case, then all the data for the spreadsheet is in memory at a single time.&nbsp; There is no streaming.</p>\r\n<p>If you have all the data in memory, in a single byte array at one time, then the easiest method to save it into a zip file is to call <a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/6f8514c3-e4f0-235d-def4-48a235ce7694.htm\">the ZipFile.AddEntry() overload that accepts a byte array</a>:</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>DataExportStreamer XMLData = <span style=\"color:Blue\">new</span> DataExportStreamer(DataExportReportID);\r\n<span style=\"color:Blue\">while</span> (!XMLData.IsStreamEnd)\r\n{\r\n   <span style=\"color:Blue\">if</span> (XMLData.ReadDataSheetStream())\r\n   {\r\n      <span style=\"color:Blue\">byte</span>[] RawData = XMLData.XMLExcelSpreadsheet;\r\n      <span style=\"color:Blue\">using</span> (<span style=\"color:Blue\">var</span> zip = <span style=\"color:Blue\">new</span> ZipFile())\r\n      {\r\n          zip.AddEntry(<span style=\"color:#A31515\">&quot;spreadsheetdata.xls&quot;</span>, RawData);\r\n          zip.Save(<span style=\"color:#A31515\">&quot;myArchive.zip&quot;</span>);\r\n      }\r\n      XMLDataRead = <span style=\"color:Blue\">true</span>;\r\n   }\r\n}\r\n\r\n</pre>\r\n</div>",
    "PostedDate": "2009-10-22T11:21:35.51-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]