1

I try to do the following:

var mem = new MemoryStream();

var xmlWriter = new XmlTextWriter(mem, System.Text.Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
var xmlSerializer = new XmlSerializer(typeof(Project));
xmlSerializer.Serialize(xmlWriter, this);
xmlWriter.Flush();
mem.Seek(0, SeekOrigin.Begin);


using (var zip = new ZipFile())
{
   ZipEntry e = zip.AddEntry("file.xml",  mem);
   e.Comment = "XML file";
   zip.AddFile("file.xml");
   zip.Save(filename);
}

mem.Close();

But is throws an exception when the zip.Save is called.

What am I doing wrong here?

The basic idea is to serialize the class Project to an XmlFile in a memorystream. Then use the memorystream in DotNetZip and zip it to file.

Enrico
  • 1,937
  • 3
  • 27
  • 40

1 Answers1

1

What exception did you receive? This code worked for me:

    using (ZipFile zip = new ZipFile())
        using (MemoryStream memStream = new MemoryStream())
        using(XmlTextWriter xmlWriter = new XmlTextWriter(memStream, System.Text.Encoding.UTF8))
        {

            xmlWriter.Formatting = Formatting.Indented;

            var xmlSerializer = new XmlSerializer(typeof (Project));
            xmlSerializer.Serialize(xmlWriter, new Project());


            xmlWriter.Flush();
            memStream.Seek(0, SeekOrigin.Begin);

            zip.AddEntry("xmlEntry.xml", memStream);


            var myDir = @"C:\myfolder\";
            Directory.CreateDirectory(myDir);
            zip.Save(Path.Combine(myDir, "myfile.zip"));
        }
Kim
  • 1,068
  • 13
  • 25
  • I am sorry, it is too long ago. No idea how it was solved in the end. Sorry – Enrico Jan 24 '14 at 08:43
  • No problem. I was just trying to figure out the same thing which led me to your question so I thought I'd post what worked for me! – Kim Jan 24 '14 at 16:41