[
  {
    "Id": "433578",
    "ThreadId": "210033",
    "Html": "<p>Hi,</p>\r\n<p>I am serializing a dot net class (c#) into a stream, adding this as an entry into a zip file as an xml file, and then persisting this to disk. In windows, it just tells me that the zip file is corrupt. When I wrap this in a unit test and try to read the zip file using ZipFile I get the error:&nbsp;Ionic.Zip.BadReadException:&nbsp;ZipEntry::ReadHeader():&nbsp;Bad&nbsp;signature&nbsp;(0x3F3F483F)&nbsp;at&nbsp;position&nbsp;&nbsp;0x00000080</p>\r\n<p>Edit:</p>\r\n<p>I just tested the output of the serialization by calling:</p>\r\n<p>File.WriteAllBytes(Core.ConfigSettings.TempDir + filename ,feedZipper.GenerateSerializedFile(feedModel, filename));</p>\r\n<p>and this was fine.</p>\r\n<p>&nbsp;</p>\r\n<p>If anyone can shed any light on this, it would be an enormous help. I'm using version&nbsp;1.9.1.4</p>\r\n<p>Thanks</p>\r\n<p>--</p>\r\n<p>Code:</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:black;background-color:white\">\r\n<pre><span style=\"color:blue\">var</span> streamToOutput = feedZipper.GenerateZippedAndSerializedFile(feedModel, filename);\r\n\r\n\r\n <span style=\"color:blue\">const</span> <span style=\"color:blue\">int</span> start = 64;\r\n\t\t    <span style=\"color:blue\">long</span> size;\r\n            <span style=\"color:blue\">using</span> (FileStream f = File.OpenRead(Core.ConfigSettings.TempDir + filename + <span style=\"color:#a31515\">&quot;.zip&quot;</span>))\r\n            {\r\n                f.Seek(start, SeekOrigin.Begin);\r\n                ZipFile zip = ZipFile.Read(f, Encoding.UTF8);\r\n                Assert.Equal(1, zip.Entries.Count);\r\n            }\r\n\r\n<span style=\"color:green\">//FeedZipper.cs</span>\r\n\r\n <span style=\"color:blue\">public</span> <span style=\"color:blue\">byte</span>[] GenerateZippedAndSerializedFile(IFeedModel feedModel, <span style=\"color:blue\">string</span> filename)\r\n        {\r\n            <span style=\"color:blue\">var</span> streamToOutput = <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.AddEntry(filename, SerializeToByteArray(feedModel));\r\n                zip.Save(streamToOutput);\r\n            }\r\n\r\n            <span style=\"color:blue\">return</span> streamToOutput.GetBuffer();\r\n        }\r\n\r\n public <span style=\"color:blue\">static</span> <span style=\"color:blue\">byte</span>[] SerializeToByteArray(IFeedModel feedModel)\r\n        {\r\n            <span style=\"color:blue\">var</span> memoryStream = <span style=\"color:blue\">new</span> MemoryStream();\r\n            <span style=\"color:blue\">var</span> xs = <span style=\"color:blue\">new</span> XmlSerializer(feedModel.GetType());\r\n            <span style=\"color:blue\">var</span> xmlTextWriter = <span style=\"color:blue\">new</span> XmlTextWriter(memoryStream, Encoding.UTF8);\r\n\r\n            <span style=\"color:blue\">var</span> ns = <span style=\"color:blue\">new</span> XmlSerializerNamespaces();\r\n            ns.Add(<span style=\"color:#a31515\">&quot;&quot;</span>, <span style=\"color:#a31515\">&quot;&quot;</span>);\r\n\r\n            xs.Serialize(xmlTextWriter, feedModel, ns);\r\n            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;\r\n            <span style=\"color:blue\">return</span> memoryStream.ToArray();\r\n        }\r\n</pre>\r\n</div>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-04-21T04:47:26.557-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "433683",
    "ThreadId": "210033",
    "Html": "<p>I see you're having a problem reading a zip file.&nbsp;</p>\r\n<p>I don't see the code that actually creates the zip file.&nbsp; Nor is it clear which code you're&nbsp; using to read it.</p>\r\n<blockquote style=\"border:solid .1em #ccc;font-style:italic;margin:.25em 1em 0 1em;padding:0 .25em 0 .25em\"><strong>Perhentian wrote:</strong>\r\n<p>I just tested the output of the serialization by calling:</p>\r\n<p>File.WriteAllBytes(Core.ConfigSettings.TempDir + filename ,feedZipper.GenerateSerializedFile(feedModel, filename));</p>\r\n<p>and this was fine.</p>\r\n</blockquote>\r\n<p>I don't know what you mean by that. &quot;this was fine&quot; - does that mean the write succeeded? That you can read the resulting zip file? If so, how does the part that was &quot;fine&quot; differ from what you were doing in the prior case</p>\r\n<p>Boil it down to a simpler test case that shows <em>all the code</em> - writing and reading.</p>\r\n<p>I suspect you're writing the zip file in an unusual way, otherwise you would show it. And I suspect the writing is the problem.</p>",
    "PostedDate": "2010-04-21T08:16:25.167-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "433850",
    "ThreadId": "210033",
    "Html": "<p>You shouldn't seek to position 64 for reading.</p>\r\n<p>I only did that in my code(which you apparently copied) to demonstrate a problem which occurs when you write data in not at the beginning of the stream and then try to read it back.</p>\r\n<p>But you didn't write a 64 byte header, so you start reading at a position different from where you started writing. This obviously can't work.</p>\r\n<p>And in your usecase a simple zip stream instead of a full zipfile might be appropriate.</p>",
    "PostedDate": "2010-04-21T13:21:39.083-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]