[
  {
    "Id": "444889",
    "ThreadId": "213166",
    "Html": "<p>Hi</p>\r\n<p><span style=\"font-size:12pt\">I am trying to obtain a byte array from a newly generated zip file and generate the zipfile from these bytes at a remote server. For this I tried to use a Memory stream and construct the zip file from the bytes obtained from this stream. However, if I open the zipfile, Windows will report that the file is corrupt. <br> <br> My temporary solution is to first write the zip file to the file system and read the file using a file stream. From the file stream I can then obtain the bytes for transportation and reconstruction at the remote server. But for us it is not desirable to write to the file system.<br> <br>So my question is why can't I use the bytes obtained from the memory stream? Is there a solution to this problem?</span></p>",
    "PostedDate": "2010-05-19T07:12:08.54-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "444893",
    "ThreadId": "213166",
    "Html": "<p>Without seeing your code, I Can't say for sure what the problem is.</p>\r\n<p>By far the most common problem when saving a zipfile to a memorystream , is that developers try to read the memorystream before resetting the cursor in it, back to the beginning. In other words, they write to the stream, then immediate try to read.&nbsp; Of course, that means the cursor is at the end-of-stream, and no bytes can be read at all.&nbsp; The solution is to call Seek() on the stream to reset the cursor to the beginning.&nbsp;&nbsp; This problem gets reported, oh, I'd say,&nbsp;every week or two, here on the forums .</p>\r\n<p>Without seeing your code there's no way for me to know if this is the problem you're seeing.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-05-19T07:16:23.237-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "444950",
    "ThreadId": "213166",
    "Html": "<p>&nbsp;</p>\r\n<div style=\"color:black;background-color:white\">\r\n<pre><span style=\"font-family:'Segoe UI', 'Microsoft Sans Serif', Arial, Geneva, sans-serif\"><span style=\"white-space:normal\"><span style=\"font-family:Consolas, 'Courier New', Courier, monospace\"><span style=\"white-space:pre\"><div style=\"color:black;background-color:white\"><pre>Thanks for the reply!</pre>\r\n<pre>Below is the code I am using:</pre>\r\n<pre> <span style=\"color:blue\">using</span> (MemoryStream ms = <span style=\"color:blue\">new</span> MemoryStream())\r\n                {\r\n                    <span style=\"color:blue\">using</span> (ZipOutputStream zos = <span style=\"color:blue\">new</span> ZipOutputStream(ms))\r\n                    {\r\n                        zos.PutNextEntry(<span style=\"color:blue\">string</span>.Format(<span style=\"color:#a31515\">&quot;TMC{0}.jpg&quot;</span>, orderID));\r\n                        zos.Write(cardImage, 0, cardImage.Length);\r\n                        \r\n                        zos.Close();\r\n                    }\r\n\r\n                    bytes = ms.GetBuffer();\r\n                }\r\n\r\n                <span style=\"color:blue\">using</span> (FileStream output = File.Create(path + <span style=\"color:#a31515\">@&quot;\\potest.zip&quot;</span>))\r\n                {\r\n                    output.Write(bytes, 0, bytes.Length);\r\n                }\r\n\r\n                <span style=\"color:blue\">using</span> (ZipFile zipFile = ZipFile.Read(path + <span style=\"color:#a31515\">@&quot;\\potest.zip&quot;</span>))\r\n                {\r\n                    <span style=\"color:blue\">int</span> count = zipFile.Count;\r\n                    input.Text = count.ToString();\r\n                }\r\n</pre>\r\n</div>\r\n<br></span></span></span></span></pre>\r\n<pre><span style=\"font-family:'Segoe UI', 'Microsoft Sans Serif', Arial, Geneva, sans-serif\"><span style=\"white-space:normal\"><span style=\"font-family:Consolas, 'Courier New', Courier, monospace\"><span style=\"white-space:pre\">Indeed I didn't reset the stream, but even if I do as below I still get the same error.</span></span></span></span></pre>\r\n<pre><span style=\"font-family:'Segoe UI', 'Microsoft Sans Serif', Arial, Geneva, sans-serif\"><span style=\"white-space:normal\"><span style=\"font-family:Consolas, 'Courier New', Courier, monospace\"><span style=\"white-space:pre\"><div style=\"color:black;background-color:white\"><pre><span style=\"color:blue\">using</span> (MemoryStream ms = <span style=\"color:blue\">new</span> MemoryStream())\r\n                {\r\n                   <span style=\"color:blue\">using</span> (ZipFile zipfile = <span style=\"color:blue\">new</span> ZipFile())\r\n                    {\r\n                        zipfile.AddEntry(<span style=\"color:blue\">string</span>.Format(<span style=\"color:#a31515\">&quot;TMC{0}.jpg&quot;</span>, orderID), cardImage);\r\n                        zipfile.Save(ms);\r\n                    }\r\n                    ms.Seek(0, SeekOrigin.Begin);\r\n                    bytes = ms.GetBuffer();\r\n                }\r\n\r\n                <span style=\"color:blue\">using</span> (FileStream output = File.Create(path + <span style=\"color:#a31515\">@&quot;\\potest.zip&quot;</span>))\r\n                {\r\n                    output.Write(bytes, 0, bytes.Length);\r\n                }\r\n</pre>\r\n</div>\r\n</span></span></span></span></pre>\r\n<pre><span style=\"font-family:'Segoe UI', 'Microsoft Sans Serif', Arial, Geneva, sans-serif\"><span style=\"white-space:normal\"><span style=\"font-family:Consolas, 'Courier New', Courier, monospace\"><span style=\"white-space:pre\"><br></span></span></span></span></pre>\r\n<pre><span style=\"font-family:'Segoe UI', 'Microsoft Sans Serif', Arial, Geneva, sans-serif\"><span style=\"white-space:normal\"><span style=\"font-family:Consolas, 'Courier New', Courier, monospace\"><span style=\"white-space:pre\"><br></span></span></span></span></pre>\r\n</div>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-05-19T08:40:40.957-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "444961",
    "ThreadId": "213166",
    "Html": "<p>One issue might be to do with using GetBuffer. See the &quot;Remarks&quot;&nbsp;section in <a href=\"http://msdn.microsoft.com/en-us/library/system.io.memorystream.getbuffer.aspx\">http://msdn.microsoft.com/en-us/library/system.io.memorystream.getbuffer.aspx</a>.</p>\r\n<p>&quot;Note that the buffer contains allocated bytes which might be unused. For example, if the string &quot;test&quot; is written into the <span><a href=\"http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx\">MemoryStream</a></span> object, the length of the buffer returned from <span><span>GetBuffer</span></span> is 256, not 4, with 252 bytes unused.&quot;</p>\r\n<p>You could try changing this:</p>\r\n<p><span style=\"color:blue\">using</span> (FileStream output = File.Create(path + <span style=\"color:#a31515\">@&quot;\\potest.zip&quot;</span>))<br>{<br>&nbsp;&nbsp;&nbsp; output.Write(bytes, 0, <strong><em>bytes</em></strong>.Length);<br>}</p>\r\n<p>to this</p>\r\n<p><br><span style=\"color:blue\">using</span> (FileStream output = File.Create(path + <span style=\"color:#a31515\">@&quot;\\potest.zip&quot;</span>))<br>{<br>&nbsp;&nbsp;&nbsp; output.Write(bytes, 0, <strong><em>ms</em></strong>.Length);<br>}</p>\r\n<p>but I haven't&nbsp;tested that myself so it's just a guess. There might also be something else going on - if you can post the original and compressed file it would help.</p>\r\n<p>M</p>",
    "PostedDate": "2010-05-19T09:03:13.9-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "444962",
    "ThreadId": "213166",
    "Html": "<p>I think you're doing it wrong.</p>\r\n<p>MemoryStream.GetBuffer returns the array of bytes from which the memorystream was created. I don't know what you would get if you create the memory stream with no argument.</p>\r\n<p>What you want is the ToArray() to get the byte array.</p>\r\n<div style=\"border:solid .1em #ccc;color:black;background-color:white;margin:.25em 0.5em 0 0.5em;padding:0.25em 1.75em 0.25em 1.25em\">\r\n<pre><span style=\"color:blue\">using</span> (MemoryStream ms = <span style=\"color:blue\">new</span> MemoryStream())\r\n{\r\n    <span style=\"color:blue\">using</span> (ZipFile zipfile = <span style=\"color:blue\">new</span> ZipFile())\r\n    {\r\n        zipfile.AddEntry(<span style=\"color:blue\">string</span>.Format(<span style=\"color:#a31515\">&quot;TMC{0}.jpg&quot;</span>, orderID), cardImage);\r\n        zipfile.Save(ms);\r\n    }\r\n    bytes = ms.ToArray();\r\n}\r\n\r\n<span style=\"color:blue\">using</span> (FileStream output = File.Create(path + <span style=\"color:#a31515\">@&quot;\\potest.zip&quot;</span>))\r\n{\r\n    output.Write(bytes, 0, bytes.Length);\r\n}</pre>\r\n</div>",
    "PostedDate": "2010-05-19T09:03:22.003-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "444969",
    "ThreadId": "213166",
    "Html": "<p>Thanks Cheeso!!!! That solved my problem indeed.</p>",
    "PostedDate": "2010-05-19T09:10:33.623-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]