Corrupt Zip: ZipEntry::ReadHeader(): Bad signature (0x3F3F483F) at position 0x00000080

Apr 21, 2010 at 12:47 PM
Edited Apr 21, 2010 at 2:01 PM

Hi,

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: Ionic.Zip.BadReadException: ZipEntry::ReadHeader(): Bad signature (0x3F3F483F) at position  0x00000080

Edit:

I just tested the output of the serialization by calling:

File.WriteAllBytes(Core.ConfigSettings.TempDir + filename ,feedZipper.GenerateSerializedFile(feedModel, filename));

and this was fine.

 

If anyone can shed any light on this, it would be an enormous help. I'm using version 1.9.1.4

Thanks

--

Code:

 

 

var streamToOutput = feedZipper.GenerateZippedAndSerializedFile(feedModel, filename);


 const int start = 64;
		    long size;
            using (FileStream f = File.OpenRead(Core.ConfigSettings.TempDir + filename + ".zip"))
            {
                f.Seek(start, SeekOrigin.Begin);
                ZipFile zip = ZipFile.Read(f, Encoding.UTF8);
                Assert.Equal(1, zip.Entries.Count);
            }

//FeedZipper.cs

 public byte[] GenerateZippedAndSerializedFile(IFeedModel feedModel, string filename)
        {
            var streamToOutput = new MemoryStream();

            using (var zip = new ZipFile())
            {
                zip.AddEntry(filename, SerializeToByteArray(feedModel));
                zip.Save(streamToOutput);
            }

            return streamToOutput.GetBuffer();
        }

 public static byte[] SerializeToByteArray(IFeedModel feedModel)
        {
            var memoryStream = new MemoryStream();
            var xs = new XmlSerializer(feedModel.GetType());
            var xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

            var ns = new XmlSerializerNamespaces();
            ns.Add("", "");

            xs.Serialize(xmlTextWriter, feedModel, ns);
            memoryStream = (MemoryStream)xmlTextWriter.BaseStream;
            return memoryStream.ToArray();
        }

 

Coordinator
Apr 21, 2010 at 4:16 PM

I see you're having a problem reading a zip file. 

I don't see the code that actually creates the zip file.  Nor is it clear which code you're  using to read it.

Perhentian wrote:

I just tested the output of the serialization by calling:

File.WriteAllBytes(Core.ConfigSettings.TempDir + filename ,feedZipper.GenerateSerializedFile(feedModel, filename));

and this was fine.

I don't know what you mean by that. "this was fine" - does that mean the write succeeded? That you can read the resulting zip file? If so, how does the part that was "fine" differ from what you were doing in the prior case

Boil it down to a simpler test case that shows all the code - writing and reading.

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.

Apr 21, 2010 at 9:21 PM

You shouldn't seek to position 64 for reading.

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.

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.

And in your usecase a simple zip stream instead of a full zipfile might be appropriate.