ZipFile to XmlDocument

Jan 11, 2010 at 4:59 PM

Just wondering if there is any method which would get XmlDocument from ZipFile.

I need to build a TreeView by recursing through a ZipFile.

Thanks

Navin

Coordinator
Jan 11, 2010 at 5:13 PM
using (var zip = ZipFile.Read("c:\\data\\zipfile.zip")) 
{ 
    using (Stream s = zip["NameOfXmlFile.xml"].OpenReader()) 
    { 
        // Create the XmlReader object. 
        using (XmlReader reader = XmlReader.Create(s)) 
        { 
            while (reader.Read())  
            { 
                .... 
            } 
        } 
    } 
} 

Jan 12, 2010 at 10:30 AM
Cheeso wrote:
using (var zip = ZipFile.Read("c:\\data\\zipfile.zip")) 
{ 
    using (Stream s = zip["NameOfXmlFile.xml"].OpenReader()) 
    { 
        // Create the XmlReader object. 
        using (XmlReader reader = XmlReader.Create(s)) 
        { 
            while (reader.Read())  
            { 
                .... 
            } 
        } 
    } 
} 

Thanks Cheeso. Sorry i misphrased my question.

I was wondering if there is something like ZipFile.ToXml() which would return me an xmlDocument of ZipFile contents.

Thanks.

 

Coordinator
Jan 12, 2010 at 3:56 PM

I don't know what ZipFile.ToXml() would do, but, I'm sure you could build one yourself just iterating through the list of entries.

Jan 12, 2010 at 3:59 PM
Cheeso wrote:

I don't know what ZipFile.ToXml() would do, but, I'm sure you could build one yourself just iterating through the list of entries.

I tried but i failed. I agree am not that good. I just need the filenames in zipfile as xmlDocument from which i would like to build a treeview.

Thanks

N

 

Coordinator
Jan 12, 2010 at 4:18 PM

There's an example that populates a treeview from a zipfile, in the source download.

http://dotnetzip.codeplex.com/SourceControl/changeset/view/54012#776852

Jan 12, 2010 at 4:28 PM
Cheeso wrote:

There's an example that populates a treeview from a zipfile, in the source download.

http://dotnetzip.codeplex.com/SourceControl/changeset/view/54012#776852

Thank u.

Navin

 

Jan 12, 2010 at 5:12 PM

I converted it to C# but it is not giving the result expected:

Down below in vb.net default(TreeNodeCollection) is used and i have set it to null.

Not sure if that is right.

Please advise.

 

public static System.Windows.Forms.TreeView PopulateTreeView()
        {
            ZipFile zf = ZipFile.Read(SaiBLL.Resource1.Classes as byte[]);
            System.Windows.Forms.TreeView tv = new System.Windows.Forms.TreeView();
            foreach (ZipEntry ze in zf.Entries)
            {
                AddTreeNode(ze.FileName, tv);
            }
            return tv;
        }
        public static System.Windows.Forms.TreeNode AddTreeNode(string name, System.Windows.Forms.TreeView TreeView1)
        {
            if ((name.EndsWith("/")))
            {
                name = name.Substring(0, name.Length - 1);
            }
            System.Windows.Forms.TreeNode node = FindNodeForTag(name, TreeView1.Nodes);
            if ((node != null))
            {
                return node;
            }
            System.Windows.Forms.TreeNodeCollection pnodeCollection = null;//new System.Windows.Forms.TreeNodeCollection();//default(TreeNodeCollection);
            string parent = System.IO.Path.GetDirectoryName(name);
            if ((string.IsNullOrEmpty(parent)))
            {
                pnodeCollection = TreeView1.Nodes;
            }
            else
            {
                pnodeCollection = AddTreeNode(parent.Replace("\\", "/"), TreeView1).Nodes;
            }
            node = new System.Windows.Forms.TreeNode();
            node.Text = System.IO.Path.GetFileName(name);
            node.Tag = name;
            // full path
            pnodeCollection.Add(node);
            return node;
        }

        public static System.Windows.Forms.TreeNode FindNodeForTag(string name, System.Windows.Forms.TreeNodeCollection nodes)
        {
            foreach (System.Windows.Forms.TreeNode node in nodes)
            {
                if ((name == node.Tag))
                {
                    return node;
                }
                else if ((name.StartsWith(node.Tag + "/")))
                {
                    return FindNodeForTag(name, node.Nodes);
                }
            }
            return null;
        }

Coordinator
Jan 12, 2010 at 6:40 PM

Are you using Visual Studio?  there's a green squiggly on the test in FindNodeForTag :

If you cast to a string, you will get the results you want, I think.

here's the code I used:

    1         private void button1_Click(object sender, EventArgs e)
    2         {
    3             this.textBox1.BackColor = System.Drawing.Color.White;
    4             string txt = this.textBox1.Text;
    5             try
    6             {
    7                 this.treeView1.Nodes.Clear();
    8                 using (var zip = ZipFile.Read(txt))
    9                 {
   10                     foreach (var entry in zip)
   11                     {
   12                         AddTreeNode(entry.FileName);
   13                     }
   14                 }
   15             }
   16             catch
   17             {
   18                 this.textBox1.BackColor = System.Drawing.Color.MistyRose;
   19                 MessageBox.Show("Exception reading that zip file.");
   20             }
   21         }
   22 
   23         private TreeNode AddTreeNode(string name)
   24         {
   25             if (name.EndsWith("/"))
   26                 name = name.Substring(0, name.Length - 1);
   27 
   28             TreeNode node = FindNodeForTag(name, this.treeView1.Nodes);
   29             if (node != null)
   30                 return node;
   31             String parent = Path.GetDirectoryName(name);
   32             TreeNodeCollection pnodeCollection = (parent == "")
   33                 ? this.treeView1.Nodes
   34                 : AddTreeNode(parent.Replace("\\", "/")).Nodes;
   35 
   36             node = new TreeNode()
   37             {
   38                 Text = Path.GetFileName(name),
   39                 Tag = name // ' full path
   40             };
   41             pnodeCollection.Add(node);
   42             return node;
   43         }
   44 
   45         // Returns the TreeNode for a given name 
   46         private TreeNode FindNodeForTag(string name, TreeNodeCollection nodes)
   47         {
   48             foreach (TreeNode node in nodes)
   49             {
   50                 if (name == (string) node.Tag)
   51                     return node;
   52                 else if (name.StartsWith(node.Tag + "/"))
   53                     return FindNodeForTag(name, node.Nodes);
   54             }
   55             return null;
   56         }