[
  {
    "Id": "613429",
    "ThreadId": "257805",
    "Html": "\r\n<p>I'm working on a project where we need to extract a single file from a zip file and we would prefer to not write the file out to disk.&nbsp; In searching I found DNZ and it appears to be a very full functioning set of utilities that cover everything that\r\n we would need to process zip files.</p>\r\n<p>The code I am using is:</p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:blue\">Using</span> zip1 <span style=\"color:blue\">As</span> ZipFile = ZipFile.Read(ZipToUnpack)\r\n    <span style=\"color:blue\">Dim</span> i <span style=\"color:blue\">As</span> ZipEntry\r\n    <span style=\"color:blue\">For</span> <span style=\"color:blue\">Each</span> i <span style=\"color:blue\">In</span> zip1\r\n        <span style=\"color:blue\">If</span> i.FileName.Contains(<span style=\"color:#a31515\">&quot;.NAR&quot;</span>) <span style=\"color:blue\">Then</span>\r\n            <span style=\"color:blue\">Dim</span> snar <span style=\"color:blue\">As</span> System.IO.Stream\r\n            i.Extract(snar)\r\n\r\n            <span style=\"color:green\">'Process streamed file</span>\r\n        <span style=\"color:blue\">End</span> <span style=\"color:blue\">If</span>\r\n    <span style=\"color:blue\">Next</span>\r\n<span style=\"color:blue\">End</span> <span style=\"color:blue\">Using</span>\r\n</pre>\r\n</div>\r\n<p>The error I get is &quot;Invalid input. Paramter name: outstream&quot; when the i.Extract is performed.</p>\r\n<p>In researching the issue, I have tried using the following example I found to use the i.OpenReader but it appears to not produce the same type of stream that I need:</p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:blue\">Using</span> zip1 <span style=\"color:blue\">As</span> ZipFile = ZipFile.Read(ZipToUnpack)\r\n    <span style=\"color:blue\">Dim</span> i <span style=\"color:blue\">As</span> ZipEntry\r\n\r\n    <span style=\"color:blue\">For</span> <span style=\"color:blue\">Each</span> i <span style=\"color:blue\">In</span> zip1\r\n        <span style=\"color:blue\">If</span> i.FileName.Contains(<span style=\"color:#a31515\">&quot;.NAR&quot;</span>) <span style=\"color:blue\">Then</span>\r\n            <span style=\"color:blue\">Using</span> s <span style=\"color:blue\">As</span> Ionic.Zlib.CrcCalculatorStream = i.OpenReader\r\n                <span style=\"color:blue\">Dim</span> n <span style=\"color:blue\">As</span> <span style=\"color:blue\">Integer</span>\r\n                <span style=\"color:blue\">Dim</span> buffer <span style=\"color:blue\">As</span> <span style=\"color:blue\">Byte</span>() = <span style=\"color:blue\">New</span> <span style=\"color:blue\">Byte</span>(4096) {}\r\n                <span style=\"color:blue\">Dim</span> totalBytesRead <span style=\"color:blue\">As</span> <span style=\"color:blue\">Integer</span> = 0\r\n                <span style=\"color:blue\">Do</span>\r\n                    n = s.Read(buffer, 0, buffer.Length)\r\n                    totalBytesRead = (totalBytesRead &#43; n)\r\n                    <span style=\"color:blue\">Loop</span> <span style=\"color:blue\">While</span> (n &gt; 0)\r\n                        <span style=\"color:blue\">If</span> (s.Crc &lt;&gt; i.Crc) <span style=\"color:blue\">Then</span>\r\n                            <span style=\"color:blue\">Throw</span> <span style=\"color:blue\">New</span> Exception(<span style=\"color:blue\">String</span>.Format(<span style=\"color:#a31515\">&quot;The Zip Entry failed the CRC Check. (0x{0:X8}!=0x{1:X8})&quot;</span>, s.Crc, i.Crc))\r\n                        <span style=\"color:blue\">End</span> <span style=\"color:blue\">If</span>\r\n                        <span style=\"color:blue\">If</span> (totalBytesRead &lt;&gt; i.UncompressedSize) <span style=\"color:blue\">Then</span>\r\n                            <span style=\"color:blue\">Throw</span> <span style=\"color:blue\">New</span> Exception(<span style=\"color:blue\">String</span>.Format(<span style=\"color:#a31515\">&quot;We read an unexpected number of bytes. ({0}!={1})&quot;</span>, totalBytesRead, i.UncompressedSize))\r\n                        <span style=\"color:blue\">End</span> <span style=\"color:blue\">If</span>\r\n\r\n                    <span style=\"color:green\">'Process docNarr = New Document(s)</span>\r\n            <span style=\"color:blue\">End</span> <span style=\"color:blue\">Using</span>\r\n        <span style=\"color:blue\">End</span> <span style=\"color:blue\">If</span>\r\n    <span style=\"color:blue\">Next</span>\r\n<span style=\"color:blue\">End</span> <span style=\"color:blue\">Using</span>\r\n</pre>\r\n</div>\r\n<p>If I could use the i.Extract(stream) it should solve all of my issues, but for some reason it won't work.&nbsp; Also, if I write the file out to the disk first and then bring it back in the process works as expected.&nbsp;</p>\r\n<p>Any ideas?</p>\r\n<p>&nbsp;</p>\r\n<p>Thanks!</p>\r\n<p>&nbsp;</p>\r\n",
    "PostedDate": "2011-05-16T09:32:37.323-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "615407",
    "ThreadId": "257805",
    "Html": "<p>You have defined snar as a Stream, but it's not assigned a value in your code, as far as I can tell.</p>\r\n<p>You could extract into a MemoryStream, a FileStream, or some other type of writable stream.&nbsp; To use a MemoryStream you'd do something like this:</p>\r\n<div style=\"color: black; background-color: white;\">\r\n<pre><span style=\"color: blue;\">Using</span> zip1 <span style=\"color: blue;\">As</span> ZipFile = ZipFile.Read(ZipToUnpack)\r\n    <span style=\"color: blue;\">Dim</span> e <span style=\"color: blue;\">As</span> ZipEntry\r\n    <span style=\"color: blue;\">For</span> <span style=\"color: blue;\">Each</span> e <span style=\"color: blue;\">In</span> zip1\r\n        <span style=\"color: blue;\">If</span> e.FileName.Contains(<span style=\"color: #a31515;\">\".NAR\"</span>) <span style=\"color: blue;\">Then</span>\r\n            <span style=\"color: blue;\">Using</span> snar <span style=\"color: blue;\">As</span> <span style=\"color: blue;\">new</span> System.IO.MemoryStream\r\n                e.Extract(snar)\r\n                snar.Seek(0,System.IO.SeekOrigin.Begin)\r\n                <span style=\"color: green;\">'Process streamed file here</span>\r\n            <span style=\"color: blue;\">End</span> <span style=\"color: blue;\">Using</span>\r\n        <span style=\"color: blue;\">End</span> <span style=\"color: blue;\">If</span>\r\n    <span style=\"color: blue;\">Next</span>\r\n<span style=\"color: blue;\">End</span> <span style=\"color: blue;\">Using</span>\r\n\r\n</pre>\r\n</div>\r\n<p>But I'm not sure what you want to do about \"processing\" the snar. You said you wanted to extract it but didn't say <em>where</em> you wanted the data to be placed.</p>\r\n<p>I guess that part is up to you.</p>\r\n<p>Good luck.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2011-05-19T11:44:06.917-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "615457",
    "ThreadId": "257805",
    "Html": "<p>In my code, snar is defined as a System.IO.Stream which is what the extract method expects.</p>\r\n<p>When it comes to the processing, I am using a utility from ASPOSE which allows me to read the file (an RTF) and then grab text out of the file.&nbsp; It will accept a stream but I am having problems getting System.IO.Stream back from the Extract method.</p>\r\n<p>Any ideas?</p>",
    "PostedDate": "2011-05-19T12:22:49.48-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "615548",
    "ThreadId": "257805",
    "Html": "<p>I'm not clear on ... System.IO.Stream is an abstract base class. You cannot ever construct one. The way it works: you instantiate a derived type - like a MemoryStream, FileStream, NetworkStream or something else.&nbsp; The System.IO.Stream is just an abstraction, a way to deal with all of those things via the same methods (Read, Seek, Write, etc). <a href=\"http://msdn.microsoft.com/en-us/library/system.io.stream.aspx\">http://msdn.microsoft.com/en-us/library/system.io.stream.aspx</a></p>\r\n<p>In your code, you declare snar as a Stream.&nbsp; But it isn't <em>initialized</em>.&nbsp; Not in the code I saw.&nbsp; It needs to hold reference to an instance of a concrete class.&nbsp; You've only declared it.</p>\r\n<p>I don't know what the ASPOSE thing has to do with it.&nbsp; I'm not clear how you want that to work with DotNetZip.</p>\r\n<p>If you want a readable stream for a ZipEntry - in other words, you want to read an entry out of a zip file (and decompress as you read) - then call ZipEntry.OpenReader() in lieu of ZipEntry.Extract(). &nbsp; OpenReader gives you a readable stream. Extract() writes content into a writable stream, that you must have previously opened. &nbsp;</p>\r\n<p>If you're not clear on that - think of a stream as a conduit, a pipe that carries liquid.&nbsp; Some pipes can emit output - you can draw liquid from them.&nbsp; Some streams cannot emit output, but instead can carry liquid away, in other words they can accept input.&nbsp; The former are like Readable streams, the latter like writeable streams.&nbsp; Which kind of stream do you want?&nbsp; If you want to suck output out of the zipfile, then call ZipEntry.OpenReader.&nbsp; Then invoke stream.Read() on what you get back (See the doc for full examples).&nbsp; If you already have a writable stream - something that accepts input like a MemoryStream or a FileStream opened for output - then call ZipEntry.Extract() , passing your writable stream.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2011-05-19T14:20:11.727-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "620934",
    "ThreadId": "257805",
    "Html": "<p>Thank you, zipentry.OpenReader() solved my problem!&nbsp; I was trying to extract a compressed XML file without using a memory stream or temp file.</p>\n<p>This is what worked for me:</p>\n<p>\n<p>&nbsp;</p>\n<p><code>\n<div style=\"color: black; background-color: white;\">\n<pre><span style=\"color: blue;\"><div style=\"color: black; background-color: white;\"><pre><span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zipfile = ZipFile.Read(fileName)) {\r\n   <span style=\"color: blue;\">foreach</span> (<span style=\"color: blue;\">var</span> zipentry <span style=\"color: blue;\">in</span> zipfile) {\r\n      <span style=\"color: blue;\">var</span> xmlDoc = XElement.Load(zipentry.OpenReader());\r\n      ...\r\n   }\r\n} \r\n</pre>\n</div>\n</span></pre>\n</div>\n<p>\n<p>&nbsp;</p>\n</p>\nI guess what was not clear to me during my attempts (where I tried InputStream) is that OpenReader would stream the uncompressed contents.</code></p>\n</p>",
    "PostedDate": "2011-05-31T11:12:05.783-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "728499",
    "ThreadId": "257805",
    "Html": "<p>I don't get what you mean by \"<span>OpenReader gives you a readable stream.\"</span></p>\n<p>I'm trying to extract a zip file that contains a single txt file, directly to the output stream to make it downloadable. I was able achieve it with the code:</p>\n<div style=\"color: black; background-color: white;\">\n<pre><span style=\"color: blue;\">using</span> (ZipFile zip = ZipFile.Read(path))\n{\n    <span style=\"color: blue;\">var</span> OutputStream = <span style=\"color: blue;\">new</span> MemoryStream();\n    zip[0].Extract(OutputStream);\n    OutputStream.Seek(0, SeekOrigin.Begin);\n\n    Response.AddHeader(<span style=\"color: #a31515;\">\"content-disposition\"</span>, <span style=\"color: #a31515;\">\"attachment; filename=\"</span> + filename);\n    <span style=\"color: blue;\">return</span> File(OutputStream, <span style=\"color: #a31515;\">\"text/plain\"</span>);\n}</pre>\n</div>\n<p>Nevertheless I thought I could do it (in a better way) like this:</p>\n<div style=\"color: black; background-color: white;\">\n<pre><span style=\"color: blue;\">using</span> (ZipFile zip = ZipFile.Read(path))\n{\n    Response.AddHeader(<span style=\"color: #a31515;\">\"content-disposition\"</span>, <span style=\"color: #a31515;\">\"attachment; filename=\"</span> + filename);\n    <span style=\"color: blue;\">return</span> File(zip[0].OpenReader(), <span style=\"color: #a31515;\">\"text/plain\"</span>);\n}\n</pre>\n</div>\n<p>But this code raises the error \"The stream is not readable.\". Shoudn't it be exactly the opposite?</p>",
    "PostedDate": "2012-01-20T15:53:13.24-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]