我試圖用dotnetzip使用下面的方法來存儲XML文件壓縮成zip文本/ XML文件時:在使用此方法Dotnetzip狀態不好誤差提取
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;
}
然而,此方法將拋出ZlibException - 壞狀態(無效存儲塊長度) - 在e.Extract(MS)呼叫。我已經閱讀了足夠的文檔和示例以確保它可以正常工作,但這也是我第一次使用dotnetzip,因此......有關如何解決此問題的任何想法?
感謝您的建議。我會給他們一個鏡頭,看看是否有幫助。作爲參考,這是我如何創建拉鍊...... 當創建一個新的實例: 'm_repo =新的ZipFile(文件名);' 當在現有拉鍊閱讀: 'm_repo = ZipFile.Read (文件名);' – mcscrilla 2011-05-25 14:12:51