提取文本/ XML文件时,Dotnetzip错误状态错误

|| 我正在尝试通过以下方法使用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,因此...关于如何解决此问题的任何想法?     
已邀请:
我真的不了解您在做什么,也看不到在zip创建阶段如何创建m_repo。看起来过于复杂。 zip提取部分中也是如此。 您可能以奇怪的方式创建了zip,这会导致解压缩错误。我无法分辨,因为代码还不完整。 我认为,如果您简化操作,可能会避免这些问题。 假设像这样的数据传输对象类:
public class Scrilla
{
    // the parts that get serialized:
    [XmlElement]
    public String Name { get;set; }
    [XmlElement]
    public DateTime FirstReport { get;set; }
    [XmlElement]
    public String MoreInfo { get;set; }
}
此代码将创建一个zip文件(保存到流中),并将该内容的XML表示形式存储在zip文件中作为条目:
    var FILENAME_HOST = \"self.xml\";
    XmlRootAttribute root = new XmlRootAttribute(\"root\");
    XmlSerializer ser = new XmlSerializer(typeof(Scrilla), root);
    MemoryStream zipStream = new MemoryStream();
    var scrilla = new Scrilla
    {
        Name = \"Foop\",
        FirstReport = DateTime.Now,
        MoreInfo = \"see http://example.org\"
    };

    // zip up:
    using (var zip1 = new ZipFile())
    {
        zip1.AddEntry(FILENAME_HOST,(entryName,stream) => {
                using (var swriter = new StreamWriter(stream))
                {
                    ser.Serialize(swriter, scrilla);
                }});
        zip1.Save(zipStream);
    }

    // the content (an XML file) is now held in the MemoryStream
    zipStream.Seek(0, SeekOrigin.Begin);
如果然后要从相同的内存流中解包,则可以执行以下操作:
    Scrilla unpacked;
    // unzip:
    using (var zip2 = ZipFile.Read(zipStream))
    {
        Stream s = zip2[FILENAME_HOST].OpenReader();
        unpacked = (Scrilla) ser.Deserialize(new StreamReader(s));
    }
然后证明它有效:
    // prove that it worked - print out to Console
    var xmlws = new System.Xml.XmlWriterSettings { OmitXmlDeclaration = true, Indent= true };
    using ( var w2 = System.Xml.XmlWriter.Create(Console.Out, xmlws))
    {
        ser.Serialize(w2, unpacked);
    }
上面的代码在创建时使用ZipFile.AddEntry重载,该重载接受WriteDelegate。 WriteDelegate是在ZipFile.Save()上使用条目名称和应在其中写入该条目内容的流调用的函数。有关详细信息,请查看DotNetZip文档。如您所见,此WriteDelegate只是将对象以XML序列化为包裹在该流中的流写入器。 解包还需要一条捷径-它只是从可读流中读取。 在每种情况下,都不需要创建和查找额外的内存流。 这段代码对我有用。 如果这样做没有帮助,也许您可​​以提供更多详细信息。     

要回复问题请先登录注册