[
  {
    "Id": "209125",
    "ThreadId": "61643",
    "Html": "<p>Cant seem to find any examples on this, I can successfully add all the files into the treeview no problem. Its the child nodes im having issues with is there any way to create a working treeview from the archive using this library or a combination of this library and VB.NET objects?</p>\r\n<p>So far i have tried this ...</p>\r\n<p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>    <span style=\"color:Blue\">Public</span> <span style=\"color:Blue\">Sub</span> LoadFolderTree(<span style=\"color:Blue\">ByVal</span> path <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">String</span>)\r\n        <span style=\"color:Blue\">Dim</span> basenode <span style=\"color:Blue\">As</span> System.Windows.Forms.TreeNode\r\n\r\n        <span style=\"color:Blue\">Using</span> zip <span style=\"color:Blue\">As</span> ZipFile = ZipFile.Read(path)\r\n            <span style=\"color:Blue\">Dim</span> e <span style=\"color:Blue\">As</span> ZipEntry\r\n\r\n            <span style=\"color:Green\">'set main file as master node'</span>\r\n            <span style=\"color:Blue\">Dim</span> Index <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">Integer</span> = path.LastIndexOf(<span style=\"color:#A31515\">&quot;\\&quot;</span>)\r\n            <span style=\"color:Blue\">Dim</span> masterpath <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">String</span> = path.Substring(Index + 1, path.Length - Index - 1)\r\n            <span style=\"color:Blue\">Dim</span> master <span style=\"color:Blue\">As</span> TreeNode = MyTreeView.Nodes.Add(masterpath)\r\n\r\n            <span style=\"color:Blue\">Try</span>\r\n                <span style=\"color:Blue\">For</span> <span style=\"color:Blue\">Each</span> e <span style=\"color:Blue\">In</span> zip\r\n                    <span style=\"color:Blue\">With</span> master\r\n                        <span style=\"color:Blue\">If</span> e.IsDirectory <span style=\"color:Blue\">Then</span>\r\n                            basenode = .Nodes.Add(e.FileName.ToString)\r\n                            basenode.Tag = e.FileName.ToString\r\n\r\n                            <span style=\"color:Blue\">For</span> <span style=\"color:Blue\">Each</span> subE <span style=\"color:Blue\">In</span> e.FileName\r\n                                <span style=\"color:Green\">' need to figure out how to go through each file/folder '</span>\r\n                                <span style=\"color:Green\">' and append to the respective parent node '</span>\r\n                            <span style=\"color:Blue\">Next</span>\r\n                        <span style=\"color:Blue\">Else</span>\r\n                            basenode = .Nodes.Add(e.FileName.ToString)\r\n                            basenode.Tag = e.FileName.ToString\r\n                        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n\r\n                        <span style=\"color:Green\">' auto expand the main node '</span>\r\n                        .Expand()\r\n                    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">With</span>\r\n                <span style=\"color:Blue\">Next</span>\r\n            <span style=\"color:Blue\">Catch</span> excep <span style=\"color:Blue\">As</span> Exception\r\n                MsgBox(excep.Message)\r\n            <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Try</span>\r\n\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Using</span>\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Sub</span>\r\n<br>which is activated with this sub ..<br><br><div style=\"color:Black;background-color:White\"><pre>    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Sub</span> findSource_Click(<span style=\"color:Blue\">ByVal</span> sender <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">Object</span>, <span style=\"color:Blue\">ByVal</span> e <span style=\"color:Blue\">As</span> System.EventArgs) <span style=\"color:Blue\">Handles</span> findSource.Click\r\n        <span style=\"color:Blue\">Dim</span> fdlg <span style=\"color:Blue\">As</span> OpenFileDialog = <span style=\"color:Blue\">New</span> OpenFileDialog()\r\n\r\n        fdlg.Title = <span style=\"color:#A31515\">&quot;Browse for a file&quot;</span>\r\n        fdlg.InitialDirectory = <span style=\"color:#A31515\">&quot;c:\\&quot;</span>\r\n        fdlg.FilterIndex = 2\r\n        fdlg.RestoreDirectory = <span style=\"color:Blue\">True</span>\r\n        <span style=\"color:Blue\">If</span> fdlg.ShowDialog() = DialogResult.OK <span style=\"color:Blue\">Then</span>\r\n            sourceFilePath.Text = fdlg.FileName\r\n            LoadFolderTree(fdlg.FileName)\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Sub</span>\r\n</pre>\r\n</div>\r\n</pre>\r\n</div>\r\nNow i know how to do this type of thing with a regular directory structure, but being a ZIP archive is this even possible? My normal folder structure method fails because it tries to locate the sub folders in the main folder which in the archive do not physically exist to VB.NET at least i don't think. Thanks for any tips that can be uncovered!</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-07-06T13:23:10.783-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "209199",
    "ThreadId": "61643",
    "Html": "<p>Here's the thing - in a zip archive a &quot;directory&quot; is not really a container per se.&nbsp;&nbsp; If a zipfile contains &quot;a directory&quot;, it is just a zero-length entry that is marked as a directory.&nbsp; But it is just fine if a directory entry does not exist in the zip archive.&nbsp; The files in an archive, once extracted, will appear under the corresponding filesystem directory as specified in the filename for that particular entry.&nbsp; This is what I mean by &quot;a directory isn't really a container.&quot;&nbsp;&nbsp; The only reason to include a directory entry explicitly into a zip archive is in the case where there are no files that would appear in that directory, and still you want to ensure that when unpacked, the filesystem contains an empty directory by that name. Does that make sense?&nbsp; And the result is that there are typically no directory entries in a zip archive, even if there are pathnames for the entries.</p>\r\n<p>If you want to produce a treeview, you will have to map between the zip file &quot;model' of seeing things, where there are no directories, and the treeview model.&nbsp;</p>\r\n<p>In your code, you test IsDirectory(), which would make sense if every path segment were represented with a directory, just as with a filesystem.&nbsp; But that's not always the case in a zip file.&nbsp; So I think you need to recurse on each path segment for each entry, and test if there is a node in your treeview corresponding to that partial path. If it does not exist, create it.&nbsp; You may find a zipfile that contains /a/b/c/d/file.txt, and you will have to create 4 nodes in the treeview as you work through the path.&nbsp; Finally you add the full FileName itself to the most deeply nested directory node.</p>\r\n<p>Here's one way to do it:</p>\r\n<pre>    Private Sub PopulateTreeView()\r\n        Try\r\n            zip = ZipFile.Read(Me.TextBox1.Text)\r\n\r\n            Me.TreeView1.Nodes.Clear()\r\n            For Each e As ZipEntry In zip\r\n                AddTreeNode(e.FileName)\r\n            Next\r\n\r\n        Catch ex As Exception\r\n            MessageBox.Show(&quot;Exception: &quot; + ex.ToString(), &quot;Exception during zip processing&quot;, _\r\n                            MessageBoxButtons.OK, MessageBoxIcon.Exclamation)\r\n\r\n        Finally\r\n            If Not (zip Is Nothing) Then\r\n                zip.Dispose()\r\n            End If\r\n\r\n        End Try\r\n    End Sub\r\n\r\n    Private Function AddTreeNode(ByVal name As String) As TreeNode\r\n        ' If the entry is a directory, it ends in a slash.  Let's trim it.\r\n        If (name.EndsWith(&quot;/&quot;)) Then\r\n            name = name.Substring(0, name.Length - 1)\r\n        End If\r\n        Dim node As TreeNode = FindNodeForTag(name, Me.TreeView1.Nodes)\r\n        If Not (node Is Nothing) Then\r\n            Return node\r\n        End If\r\n        ' nodeCollection = where to add the current node.\r\n        Dim nodeCollection As TreeNodeCollection\r\n        Dim parent As String = Path.GetDirectoryName(name)\r\n        If (parent = &quot;&quot;) Then\r\n            ' we will add the named node to the root\r\n            nodeCollection = Me.TreeView1.Nodes\r\n        Else\r\n            ' First add a parent node, and then we will \r\n            ' add the named node as a sub-node of that parent.\r\n            ' NB: must replace backslash with fwd-slash.  The names \r\n            ' in a zip file use fwd slash.  The paths returned from \r\n            ' Path.GetDirectoryName use backslash. \r\n            nodeCollection = AddTreeNode(parent.Replace(&quot;\\&quot;, &quot;/&quot;)).Nodes\r\n        End If\r\n        ' the node to be added\r\n        node = New TreeNode\r\n        ' the text that is displayed: just the last part of the path\r\n        node.Text = Path.GetFileName(name)\r\n        ' we use the tag to find nodes, so use the full path here\r\n        node.Tag = name\r\n        ' add it\r\n        nodeCollection.Add(node)\r\n        ' return the node just added \r\n        Return node\r\n    End Function\r\n\r\n    ' return the node with a tag matching the given name\r\n    Private Function FindNodeForTag(ByVal name As String, _\r\n                                    ByRef nodes As TreeNodeCollection) As TreeNode\r\n        For Each node As TreeNode In nodes\r\n            If (name = node.Tag) Then\r\n                Return node\r\n            ElseIf (name.StartsWith(node.Tag + &quot;/&quot;)) Then\r\n                Return FindNodeForTag(name, node.Nodes)\r\n            End If\r\n        Next\r\n        Return Nothing\r\n    End Function\r\n</pre>\r\n<p>This probably isn't the most efficient way to do it. I'm not a WinForms genius and VB is not my thing. But this works.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-07-06T16:17:24.01-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "209471",
    "ThreadId": "61643",
    "Html": "<p>Great that is exactly what i was trying to do. Executiion time is minimal actually. Little cleanup that VS2008 didnt care for but very little ... if anyone is intrested his example works ... but cleaned up to produce no errors in VS2008 is this ...</p>\r\n<p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Sub</span> PopulateTreeView(<span style=\"color:Blue\">ByVal</span> zipSource <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">String</span>)\r\n        <span style=\"color:Blue\">Using</span> zip <span style=\"color:Blue\">As</span> ZipFile = ZipFile.Read(zipSource)\r\n\r\n            <span style=\"color:Blue\">Try</span>\r\n                MyTreeView.Nodes.Clear()\r\n\r\n                <span style=\"color:Blue\">For</span> <span style=\"color:Blue\">Each</span> e <span style=\"color:Blue\">As</span> ZipEntry <span style=\"color:Blue\">In</span> zip\r\n                    AddTreeNode(e.FileName)\r\n                <span style=\"color:Blue\">Next</span>\r\n\r\n            <span style=\"color:Blue\">Catch</span> ex <span style=\"color:Blue\">As</span> Exception\r\n                MessageBox.Show(<span style=\"color:#A31515\">&quot;Exception: &quot;</span> + ex.ToString(), <span style=\"color:#A31515\">&quot;Exception during zip processing&quot;</span>, _\r\n                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation)\r\n\r\n            <span style=\"color:Blue\">Finally</span>\r\n                <span style=\"color:Blue\">If</span> zip <span style=\"color:Blue\">Is</span> <span style=\"color:Blue\">Nothing</span> <span style=\"color:Blue\">Then</span>\r\n                    zip.Dispose()\r\n                <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n\r\n            <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Try</span>\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Using</span>\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Sub</span>\r\n\r\n    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Function</span> AddTreeNode(<span style=\"color:Blue\">ByVal</span> name <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">String</span>) <span style=\"color:Blue\">As</span> TreeNode\r\n        <span style=\"color:Green\">' If the entry is a directory, it ends in a slash.  Let's trim it.</span>\r\n        <span style=\"color:Blue\">If</span> name.EndsWith(<span style=\"color:#A31515\">&quot;/&quot;</span>) <span style=\"color:Blue\">Then</span>\r\n            name = name.Substring(0, name.Length - 1)\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n\r\n        <span style=\"color:Blue\">Dim</span> node <span style=\"color:Blue\">As</span> TreeNode = FindNodeForTag(name, MyTreeView.Nodes)\r\n        <span style=\"color:Blue\">If</span> <span style=\"color:Blue\">Not</span> node <span style=\"color:Blue\">Is</span> <span style=\"color:Blue\">Nothing</span> <span style=\"color:Blue\">Then</span>\r\n            <span style=\"color:Blue\">Return</span> node\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n\r\n        <span style=\"color:Green\">' nodeCollection = where to add the current node.</span>\r\n        <span style=\"color:Blue\">Dim</span> nodeCollection <span style=\"color:Blue\">As</span> TreeNodeCollection\r\n        <span style=\"color:Blue\">Dim</span> parent <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">String</span> = Path.GetDirectoryName(name)\r\n        <span style=\"color:Blue\">If</span> parent = <span style=\"color:#A31515\">&quot;&quot;</span> <span style=\"color:Blue\">Then</span>\r\n            <span style=\"color:Green\">' we will add the named node to the root</span>\r\n            nodeCollection = MyTreeView.Nodes\r\n        <span style=\"color:Blue\">Else</span>\r\n            <span style=\"color:Green\">' First add a parent node, and then we will </span>\r\n            <span style=\"color:Green\">' add the named node as a sub-node of that parent.</span>\r\n            <span style=\"color:Green\">' NB: must replace backslash with fwd-slash.  The names </span>\r\n            <span style=\"color:Green\">' in a zip file use fwd slash.  The paths returned from </span>\r\n            <span style=\"color:Green\">' Path.GetDirectoryName use backslash. </span>\r\n            nodeCollection = AddTreeNode(parent.Replace(<span style=\"color:#A31515\">&quot;\\&quot;</span>, <span style=\"color:#A31515\">&quot;/&quot;</span>)).Nodes\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n\r\n        <span style=\"color:Green\">' the node to be added</span>\r\n        node = <span style=\"color:Blue\">New</span> TreeNode\r\n\r\n        <span style=\"color:Green\">' the text that is displayed: just the last part of the path</span>\r\n        node.Text = Path.GetFileName(name)\r\n\r\n        <span style=\"color:Green\">' we use the tag to find nodes, so use the full path here</span>\r\n        node.Tag = name\r\n\r\n        <span style=\"color:Green\">' add it</span>\r\n        nodeCollection.Add(node)\r\n\r\n        <span style=\"color:Green\">' return the node just added </span>\r\n        <span style=\"color:Blue\">Return</span> node\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Function</span>\r\n\r\n    <span style=\"color:Green\">' return the node with a tag matching the given name</span>\r\n    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Function</span> FindNodeForTag(<span style=\"color:Blue\">ByVal</span> name <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">String</span>, _\r\n                                    <span style=\"color:Blue\">ByRef</span> nodes <span style=\"color:Blue\">As</span> TreeNodeCollection) <span style=\"color:Blue\">As</span> TreeNode\r\n        <span style=\"color:Blue\">For</span> <span style=\"color:Blue\">Each</span> node <span style=\"color:Blue\">As</span> TreeNode <span style=\"color:Blue\">In</span> nodes\r\n            <span style=\"color:Blue\">If</span> name = node.Tag <span style=\"color:Blue\">Then</span>\r\n                <span style=\"color:Blue\">Return</span> node\r\n            <span style=\"color:Blue\">ElseIf</span> name.StartsWith(node.Tag + <span style=\"color:#A31515\">&quot;/&quot;</span>) <span style=\"color:Blue\">Then</span>\r\n                <span style=\"color:Blue\">Return</span> FindNodeForTag(name, node.Nodes)\r\n            <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n        <span style=\"color:Blue\">Next</span>\r\n        <span style=\"color:Blue\">Return</span> <span style=\"color:Blue\">Nothing</span>\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Function</span>\r\n<br>and this is called upon using<br><br><div style=\"color:Black;background-color:White\"><pre>    <span style=\"color:Blue\">Private</span> <span style=\"color:Blue\">Sub</span> findSource_Click(<span style=\"color:Blue\">ByVal</span> sender <span style=\"color:Blue\">As</span> <span style=\"color:Blue\">Object</span>, <span style=\"color:Blue\">ByVal</span> e <span style=\"color:Blue\">As</span> System.EventArgs) <span style=\"color:Blue\">Handles</span> findSource.Click\r\n        <span style=\"color:Blue\">Dim</span> fdlg <span style=\"color:Blue\">As</span> OpenFileDialog = <span style=\"color:Blue\">New</span> OpenFileDialog()\r\n\r\n        fdlg.Title = <span style=\"color:#A31515\">&quot;Browse for a Archive File&quot;</span>\r\n        fdlg.InitialDirectory = <span style=\"color:#A31515\">&quot;c:\\&quot;</span>\r\n        fdlg.FilterIndex = 2\r\n        fdlg.RestoreDirectory = <span style=\"color:Blue\">True</span>\r\n        <span style=\"color:Blue\">If</span> fdlg.ShowDialog() = DialogResult.OK <span style=\"color:Blue\">Then</span>\r\n            sourceFilePath.Text = fdlg.FileName\r\n            PopulateTreeView(fdlg.FileName)\r\n        <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">If</span>\r\n    <span style=\"color:Blue\">End</span> <span style=\"color:Blue\">Sub</span>\r\n</pre>\r\n</div>\r\nThanks a lot for the help!</pre>\r\n</div>\r\n</p>",
    "PostedDate": "2009-07-07T06:44:41.51-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "209503",
    "ThreadId": "61643",
    "Html": "<p>No problem.</p>\r\n<p>Here's what it looks like:</p>\r\n<p><img src=\"http://i29.tinypic.com/biplvp.jpg\" alt=\"\"></p>\r\n<p>and <a href=\"https://code.msdn.microsoft.com/Release/ProjectReleases.aspx?ProjectName=DotNetZip&ReleaseId=2952\">here's a Visual Studio project</a> that you can download and run - uses VS2008. It's very basic, it shows how to populate a TreeView with the contents of a ZipFile.</p>",
    "PostedDate": "2009-07-07T07:54:05.36-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]