[
  {
    "Id": "274464",
    "ThreadId": "80098",
    "Html": "<p>Can anyone post some&nbsp;sample code that shows how to zip to a stream &amp; save the stream to a table in a database?&nbsp; Thanks...</p>",
    "PostedDate": "2010-01-07T11:39:51.52-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "274470",
    "ThreadId": "80098",
    "Html": "<p>Saving to a MemoryStream is easy, right?&nbsp; You just call zip.Save(memorystream), then you have the zipped content in the memorystream.&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre><span style=\"color:Blue\">using</span> (<span style=\"color:Blue\">var</span> ms = <span style=\"color:Blue\">new</span> MemoryStream())\r\n{\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.AddFile(...);\r\n    zip.AddEntry(...);\r\n    zip.Save(ms);\r\n  }\r\n  <span style=\"color:Blue\">byte</span>[] data = ms.ToArray();\r\n  <span style=\"color:Green\">// upload this byte[] to the database.</span>\r\n}\r\n</pre>\r\n</div>\r\n<p>Uploading to a database is also not too complicated.&nbsp; <a href=\"http://www.dbtutorials.com/advanced/storing-binary-sql-cs.aspx\">Here's a nice article</a>.</p>\r\n<p>Instead of declaring a byte[] and filling it with data from the filesystem, as is done in that article,&nbsp;you just use MemoryStream.ToArray(), which returns a byte array of the zipped content.</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre><span style=\"color:Blue\">using</span> (SqlConnection Conn = <span style=\"color:Blue\">new</span> SqlConnection(ConnectionString))\r\n{ \r\n    <span style=\"color:Blue\">try</span>\r\n    { \r\n        <span style=\"color:Blue\">const</span> <span style=\"color:Blue\">string</span> SQL = <span style=\"color:#A31515\">&quot;INSERT INTO [BinaryTable] ([FileName], [DateTimeUploaded], [MIME], [BinaryData]) VALUES (@FileName, @DateTimeUploaded, @MIME, @BinaryData)&quot;</span>;\r\n        SqlCommand cmd = <span style=\"color:Blue\">new</span> SqlCommand(SQL, Conn);\r\n        cmd.Parameters.AddWithValue(<span style=\"color:#A31515\">&quot;@FileName&quot;</span>, <span style=\"color:#A31515\">&quot;Name-of-file-in-database.zip&quot;</span>);\r\n        cmd.Parameters.AddWithValue(<span style=\"color:#A31515\">&quot;@MIME&quot;</span>, fileType);\r\n        cmd.Parameters.AddWithValue(<span style=\"color:#A31515\">&quot;@BinaryData&quot;</span>, data);\r\n        cmd.Parameters.AddWithValue(<span style=\"color:#A31515\">&quot;@DateTimeUploaded&quot;</span>, DateTime.Now);\r\n        Conn.Open();\r\n        cmd.ExecuteNonQuery();\r\n        Conn.Close();\r\n    }\r\n    <span style=\"color:Blue\">catch</span>\r\n    { \r\n        Conn.Close();\r\n    }\r\n}         \r\n\r\n</pre>\r\n</div>",
    "PostedDate": "2010-01-07T11:46:44.823-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "274504",
    "ThreadId": "80098",
    "Html": "<p>Thanks, I will give it a try...</p>",
    "PostedDate": "2010-01-07T13:01:25.75-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "413533",
    "ThreadId": "80098",
    "Html": "<p>Is it possible to use this method but instead of uploading to a SQL-server, upload to a Webserver with WebClient.DataUpload()?</p>\r\n<p>I tried to do it yeasterday, but my script on the webserver can't find any files in the POST request, it works great when doing a regular.save() and WebClient.UploadFile(), but it would be better to upload the memorystream directly.</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-03-02T23:48:03.633-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "413771",
    "ThreadId": "80098",
    "Html": "<table border=0 width=800>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<p>Yes, you will be able to upload a MemoryStream with WebClient.UploadData().&nbsp; A common problem is that people don't remember to reset the cursor in the MemoryStream, after writing and before reading.&nbsp; If you don't reset the cursor (via MemoryStream.Position = 0, or MemoryStream.Seek(0, SeekOrigin.Begin)) then the call to UploadData will get no data.&nbsp; This has nothing to do with zip files.&nbsp; &nbsp;It's just how the MemoryStream works.</p>\r\n<p>&nbsp;</p>\r\n</td>\r\n</tr>\r\n</tbody>\r\n</table>",
    "PostedDate": "2010-03-03T10:39:22.957-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]