[
  {
    "Id": "270846",
    "ThreadId": "79097",
    "Html": "<p>I was trying to use streams to extract files from a compressed zip coming from a browser in a stream. &nbsp; &nbsp; &nbsp;I got the code to work, but the files that were being pulled out of the zip seemed to be corrupted. &nbsp; I was using the ZipEntry.Extract method. &nbsp; I was never able to get it to work. &nbsp; However, using the Zip.Extract to extract the same files to a specific directory and then read them to store them into the database worked just fine. &nbsp; This is the code that did not work. &nbsp; &nbsp;Any ideas? &nbsp; I would prefer to be able to do this using Streams.</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>        <span style=\"color:Green\">//private NameValueCollection ExtractZipAndAddFiles(System.IO.Stream ArchiveStream)</span>\r\n        <span style=\"color:Green\">//{</span>\r\n        <span style=\"color:Green\">//    NameValueCollection FileStatus = new NameValueCollection();</span>\r\n\r\n        <span style=\"color:Green\">//    using (ZipFile zip = ZipFile.Read(ArchiveStream))</span>\r\n        <span style=\"color:Green\">//    {</span>\r\n        <span style=\"color:Green\">//        foreach (ZipEntry entry in zip.Entries)</span>\r\n        <span style=\"color:Green\">//        {</span>\r\n        <span style=\"color:Green\">//            System.IO.MemoryStream FileStream = new System.IO.MemoryStream();</span>\r\n\r\n        <span style=\"color:Green\">//            entry.Extract(FileStream);</span>\r\n\r\n        <span style=\"color:Green\">//            bool Result = AddFile(FileStream, (int)entry.UncompressedSize, entry.FileName, System.IO.Path.GetExtension(entry.FileName));</span>\r\n\r\n        <span style=\"color:Green\">//            FileStream.Close();</span>\r\n\r\n        <span style=\"color:Green\">//            FileStatus.Add(entry.FileName, Result.ToString());</span>\r\n        <span style=\"color:Green\">//        }</span>\r\n        <span style=\"color:Green\">//    }</span>\r\n\r\n        <span style=\"color:Green\">//    return FileStatus;</span>\r\n        <span style=\"color:Green\">//}</span>\r\n</pre>\r\n</div>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-12-26T09:10:32.497-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "270861",
    "ThreadId": "79097",
    "Html": "<p>Some comments&nbsp;for you:</p>\r\n<ul>\r\n<li>when extracting zip files, whether into a stream or into a filesystem file, there's a CRC check that happens.&nbsp; If&nbsp;the check fails, DotNetZip throws an exception.&nbsp; If you haven't gotten an exception, then the corruption is happening in <em>your </em>code. </li>\r\n<li>a possible cause for the corruption is that you haven't performed a Seek() on the MemoryStream after extracting the filedata into it.&nbsp; I don't know what your &quot;AddFile&quot; method does, but if that method tries to read the FileStream parameter without first resetting the pointer on the stream, it will fail.&nbsp; It will not read the uncompressed file data.&nbsp; It will read &lt;nothing&gt;.&nbsp; </li>\r\n<li>It doesn't make sense to extract the entry into a MemoryStream, only to then read from the MemoryStream.&nbsp;&nbsp; Why not just read it directly?&nbsp;&nbsp; You can use ZipEntry.OpenReader() to get the stream directly.&nbsp; It automatically uncompresses as you read. </li>\r\n<li>you should use a using() clause on objects that are IDisposable, like Streams.&nbsp; This eliminates the need to call Close() explicitly, and protects you in case an exception occurs during processing.</li>\r\n</ul>\r\n<p>&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre><span style=\"color:Blue\">private</span> NameValueCollection ExtractZipAndAddFiles(System.IO.Stream ArchiveStream)\r\n{\r\n    NameValueCollection FileStatus = <span style=\"color:Blue\">new</span> NameValueCollection();\r\n\r\n    <span style=\"color:Blue\">using</span> (ZipFile zip = ZipFile.Read(ArchiveStream))\r\n    {\r\n        <span style=\"color:Blue\">foreach</span> (ZipEntry entry <span style=\"color:Blue\">in</span> zip.Entries)\r\n        {\r\n            <span style=\"color:Blue\">using</span> (Stream s = entry.OpenReader())\r\n            {\r\n                <span style=\"color:Blue\">var</span> result = AddFile(s, (<span style=\"color:Blue\">int</span>)entry.UncompressedSize, entry.FileName, System.IO.Path.GetExtension(entry.FileName));\r\n                FileStatus.Add(entry.FileName, result.ToString());\r\n            }\r\n        }\r\n    }\r\n\r\n    <span style=\"color:Blue\">return</span> FileStatus;\r\n}\r\n\r\n\r\n</pre>\r\n</div>",
    "PostedDate": "2009-12-26T10:46:11.377-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "270865",
    "ThreadId": "79097",
    "Html": "<p>Resetting the pointer on the Stream did the trick. &nbsp; I figured that it was going to be something simple like that...</p>\r\n<p>Thanks for the suggestions, I do appreciate it.</p>\r\n<p>Chaitanya</p>",
    "PostedDate": "2009-12-26T11:18:08.133-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]