我正在尝试使用以下方法使用 dotnetzip 将 XML 文件存储到 zip 中:
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();
}
然后我使用这种方法读回 XML 文件:
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;
}
但是,此方法在 e.Extract(ms) 调用时会引发 ZlibException - 错误状态(无效存储块长度)。我已经阅读了足够多的文档和示例,以相当确定这应该有效,但这也是我第一次使用 dotnetzip 所以......关于如何解决这个问题的任何想法?