I am trying to store an XML file into a zip using dotnetzip using the following method:
private void writeHosts()
{
XmlRootAttribute root = new XmlRootAttribute(ROOTNAME_HOST);
XmlSerializer ser = new XmlSerializer(typeof(Host[]), root);
MemoryStream ms = new MemoryStream();
StreamWriter swriter = new StreamWriter(ms);
//write xml to memory stream
ser.Serialize(swriter, m_hostList.ToArray());
swriter.Flush();
//be kind, rewind (the stream)
ms.Seek(0, SeekOrigin.Begin);
//copy memory stream to zip as a file.
using (m_repo)
{
ZipEntry e = m_repo.AddEntry(FILENAME_HOST, ms);
e.IsText = true;
m_repo.Save();
}
swriter.Close();
}
I then read the XML file back in using this method:
private List<Host> readHosts()
{
XmlRootAttribute root = new XmlRootAttribute(ROOTNAME_HOST);
XmlSerializer ser = new XmlSerializer(typeof(Host[]), root);
MemoryStream ms = new MemoryStream();
StreamReader reader = new StreamReader(ms);
List<Host> retlist = new List<Host>();
//get the vuln list from the zip and read into memory
using (m_repo)
{
ZipEntry e = m_repo[FILENAME_HOST];
e.Extract(ms);
}
//rewind to the start of the stream
ms.Flush();
ms.Seek(0, SeekOrigin.Begin);
//Pull the host list from XML
Host[] ret = (Host[])ser.Deserialize(reader);
retlist.AddRange(ret);
ms.Close();
return retlist;
}
However, this method throws a ZlibException -- Bad state (invalid stored block lengths) -- at the e.Extract(ms) call. I've read through enough documentation and examples to be fairly certain that this should work, but this is also my first time working with dotnetzip so... any thoughts on how to resolve this?