Extract from zip file to string

Jul 6, 2009 at 11:05 PM
Edited Jul 6, 2009 at 11:34 PM

I have a zip file with some xml serialized data in different files (file1.xml, file2.xml, etc.)

Can someone show me how to extract from from the zip file into a string so i can deserialize the data please? I already have the following as deserialization code...can I avoid saving to string and deserialize directly into my List<T> ?

 

        public static T DeserializeObject<T>(Encoding encoding, string xml)
        {
            try
            {
                using (MemoryStream memoryStream = new MemoryStream(StringToByteArray(encoding, xml)))
                {
                    using (XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, encoding))
                    {
                        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

                        return (T)xmlSerializer.Deserialize(memoryStream);
                    }
                }
            }
            catch
            {
                return default(T);
            }
        }

 

Thanks

 

EDIT: Nevermind, I figured it out myself. I did something like this:

using (ZipFile zip = ZipFile.Read(txtFile.Text.ToString()) )
            {

                ZipEntry treeHier = zip["TreeHierarchy.xml"];
                using (MemoryStream ms = new MemoryStream())
                {
                    treeHier.Extract(ms);

                    importTreeHierarchy = Helpers.DeserializeObject<List<TreeHierarchy>>(Encoding.UTF8 ,Helpers.GetStringFromMemoryStream(ms));
                }

            }

 

Coordinator
Jul 6, 2009 at 11:54 PM

Extracting to a MemoryStream is probably the best way to go.