[
  {
    "Id": "106953",
    "ThreadId": "32246",
    "Html": "Ok, sorry if this post&nbsp;is a dupe from somewhere else. I tried searching for what I was looking to do, but I couldn't find it.&nbsp; I reviewed the following two posts, but they didn't seem to help...<br>\r\n<a id=\"ctl00_ctl00_Content_TabContentPanel_Content_ThreadList_ctl15_ThreadLink\" href=\"http://www.codeplex.com/DotNetZip/Thread/View.aspx?ThreadId=20158\"><strong><span style=\"color:#3e62a6\">Support for MemoryStreams</span></strong></a><br>\r\n<a id=\"ctl00_ctl00_Content_TabContentPanel_Content_ThreadList_ctl12_ThreadLink\" href=\"http://www.codeplex.com/DotNetZip/Thread/View.aspx?ThreadId=23147\"><strong><span style=\"color:#3e62a6\">Save ZipFile to MemoryStream, but then the MemoryStream is not readable</span></strong></a><br>\r\n<br>\r\nI have a function that I want to take an array of bytes, the filename to create inside the zip file stream, and the password. I want it to them return the byte array of the zipfile so I can save it out to the filesystem or store it in a database as necessary.&nbsp; The code below isn't working. I have tried multiple variations, but this one gets me the closest.&nbsp; Essentially I want to create a zip file in a memory stream, then add the file (indicated by the byte() array sent to the function) using the AddFileStream function, then write that new zip file stream with the file in it with the zipped password out to an array of bytes and return that from the function. Any ideas on what I am doing wrong?&nbsp; Any help would be greatly appreciated. Thanks!<br>\r\n<br>\r\n<span style=\"font-size:13px\">\r\n<p>&nbsp;</p>\r\n</span>\r\n<p><span style=\"font-size:13px;color:#0000ff\">Public</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">Function</span><span style=\"font-size:13px\"> Compress(</span><span style=\"font-size:13px;color:#0000ff\">ByVal</span><span style=\"font-size:13px\"> oBytes </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">Byte</span><span style=\"font-size:13px\">(), </span><span style=\"font-size:13px;color:#0000ff\">ByVal</span><span style=\"font-size:13px\"> sFilename </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">String</span><span style=\"font-size:13px\">, </span><span style=\"font-size:13px;color:#0000ff\">ByVal</span><span style=\"font-size:13px\"> sPassword </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">String</span><span style=\"font-size:13px\">) </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">Byte</span><span style=\"font-size:13px\">()<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">Dim</span><span style=\"font-size:13px\"> oCompressedStream </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">New</span><span style=\"font-size:13px\"> System.IO.MemoryStream()<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">Dim</span><span style=\"font-size:13px\"> oOriginalStream </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">New</span><span style=\"font-size:13px\"> System.IO.MemoryStream(oBytes.Length - 1)<br>\r\noOriginalStream.Write(oBytes, 0, oBytes.Length)<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">Dim</span><span style=\"font-size:13px\"> iCompressor </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">New</span><span style=\"font-size:13px\"> Ionic.Utils.Zip.ZipFile(oCompressedStream)<br>\r\niCompressor.Password = sPassword<br>\r\niCompressor.Save()<br>\r\niCompressor.AddFileStream(sFilename, </span><span style=\"font-size:13px;color:#a31515\">&quot;&quot;</span><span style=\"font-size:13px\">, oOriginalStream)<br>\r\niCompressor.Save()<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">Dim</span><span style=\"font-size:13px\"> oReturnBytes(oCompressedStream.Length - 1) </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">Byte<br>\r\n</span><span style=\"font-size:13px\">oCompressedStream.Position = 0<br>\r\noCompressedStream.Read(oReturnBytes, 0, oCompressedStream.Length)<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">Dim</span><span style=\"font-size:13px\"> oStream </span><span style=\"font-size:13px;color:#0000ff\">As</span><span style=\"font-size:13px\"> IO.FileStream = IO.File.Create(</span><span style=\"font-size:13px;color:#a31515\">&quot;c:\\t2.zip&quot;</span><span style=\"font-size:13px\">)<br>\r\noStream.Read(oReturnBytes, 0, oReturnBytes.Length)<br>\r\noStream.Close()<br>\r\noOriginalStream.Close()<br>\r\niCompressor = </span><span style=\"font-size:13px;color:#0000ff\">Nothing<br>\r\n</span><span style=\"font-size:13px\">oOriginalStream = </span><span style=\"font-size:13px;color:#0000ff\">Nothing<br>\r\n</span><span style=\"font-size:13px\">oCompressedStream = </span><span style=\"font-size:13px;color:#0000ff\">Nothing<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">Return</span><span style=\"font-size:13px\"> oReturnBytes<br>\r\n</span><span style=\"font-size:13px;color:#0000ff\">End</span><span style=\"font-size:13px\"> </span><span style=\"font-size:13px;color:#0000ff\">Function\r\n<p>&nbsp;</p>\r\n</span></p>\r\n",
    "PostedDate": "2008-07-24T18:32:48.113-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "107578",
    "ThreadId": "32246",
    "Html": "<p>I think you want to reset the position of the oOriginalStream after you write it, and before you read it.&nbsp; The ZipFile class reads the stream when you call .Save().&nbsp; The ZipFile class will not reset the position of the streams it compresses before reading them.&nbsp;&nbsp; The caller is responsible for that.&nbsp; <br>\r\n</p>\r\n<pre>\r\n    Public Function Compress(ByVal oBytes As Byte(), ByVal sFilenameInArchive As String, ByVal sPassword As String, ByVal sZipFile As String ) As Byte()\r\n        Dim oCompressedStream As New System.IO.MemoryStream()\r\n\r\n        Dim oOriginalStream As New System.IO.MemoryStream(oBytes.Length - 1)\r\n        oOriginalStream.Write(oBytes, 0, oBytes. Length)\r\n        oOriginalStream.Seek(System.IO.SeekOrigin.Begin, 0)\r\n\r\n        Try \r\n            Using zip As New ionic.Utils.Zip.ZipFile(oCompressedStream)\r\n                zip.Password = sPassword\r\n                zip.AddFileStream(sFilenameInArchive, &quot;&quot;, oOriginalStream)\r\n                zip.Save()\r\n            End Using\r\n        Catch ex1 As Exception\r\n            Console.Error.WriteLine(&quot;exception: &quot; &amp; ex1.ToString())\r\n        End Try\r\n\r\n        Dim oReturnBytes(oCompressedStream.Length - 1) As Byte\r\n        oCompressedStream.Position = 0\r\n        oCompressedStream.Read(oReturnBytes, 0, oCompressedStream.Length)\r\n        Using oStream As System.IO.FileStream = IO.File.Create(sZipFile)\r\n            oStream.Write(oReturnBytes, 0, oReturnBytes.Length)\r\n            oStream.Close()\r\n        End Using \r\n        oOriginalStream.Close()\r\n\r\n        Return oReturnBytes\r\n\r\n    End Function \r\n</pre>\r\n",
    "PostedDate": "2008-07-28T14:57:18.24-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "107841",
    "ThreadId": "32246",
    "Html": "Ahh, ok, that seems to work. Thanks!&nbsp; I still have two problems though. First, I can't seem to just save the stream out to a file myself without using <br>\r\nyour save function? not sure why that would matter. Second, I want to extract a file that is password protected into a stream. I can't figure<br>\r\nthat one out? I can read the zipfile from the stream, but not extract the file into a stream.&nbsp; Any suggestions?\r\n",
    "PostedDate": "2008-07-29T16:38:23.85-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "107993",
    "ThreadId": "32246",
    "Html": "ah, I don't know what you mean.&nbsp; &quot;I can't seem to just save the stream out to a file myself without using <br>\r\nyour save function?&quot;&nbsp; Why do you want to save it without calling Save().&nbsp; I don't understand.&nbsp;&nbsp; The way the library works is, <br>\r\nif you want to save the archive, you call Save().&nbsp; I am sure I am misunderstanding something.<br>\r\n<br>\r\nI also don't know what you mean by&nbsp;<br>\r\n&quot;Second, I want to extract a file that is password protected into a stream. &quot;<br>\r\n<br>\r\ndid you look at the ZipEntry.OpenReader() methods? <br>\r\n<br>\r\n",
    "PostedDate": "2008-07-30T09:10:33.617-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]