TreeView w/ Checkboxes

May 24, 2009 at 2:35 PM

Hello,

I am trying to use DotNetZip with the TreeView control using checkboxes. I can populate the treeview with files from the file system but I cant grasp the concept of putting them in a zip file if checked.

The following is my code for the treeview:

 void PopulateNode(Object source, TreeNodeEventArgs e)
{
 TreeNode node = e.Node;
 if (node.Value == "Files")
 {
  node.Value = "~/";
 }
 String rootDirectory = Request.MapPath("~/", Request.ApplicationPath, false);
 String fullPath = Request.MapPath(node.Value, Request.ApplicationPath, false);

 if (fullPath.StartsWith(rootDirectory) == false)
 {
  // Mitigate against spoofed callback arguments
  // Requested directory is not under root directory
  return;
 }

 String[] dirs = Directory.GetDirectories(fullPath);

 // Enumerate directories
 foreach (String dir in dirs)
 {
  String virtualDir = node.Value.TrimEnd(_slashArray) + "/" + Path.GetFileName(dir);

  TreeNode newNode = new TreeNode(Path.GetFileName(dir), virtualDir);
  if (Directory.GetFiles(dir).Length > 0
            || Directory.GetDirectories(dir).Length > 0)
  {
   newNode.PopulateOnDemand = true;
  }
  node.ChildNodes.Add(newNode);
 }

 // Enumerate files
 String[] files = Directory.GetFiles(fullPath);
 foreach (String file in files)
 {
  TreeNode newNode = new TreeNode(Path.GetFileName(file), Path.GetFileName(file));
  node.ChildNodes.Add(newNode);
 }
}

 Help would be much appreciated.

Thanks

Coordinator
May 24, 2009 at 9:35 PM

??? Is it ASP.NET or Windows Forms, or something else?

Regardless, it seems like you want to set up a button, with an OnClick event handler. Within the OnClick method, instantiate a ZipFile object.  Then, walk the nodes of the tree, going through each one.  For each node in the tree, if the checkbox is checked, then add the filename for that node to the ZipFile with zip.AddFile().

At the end of looping through all the nodes. call ZipFile.Save().

Is that helpful?

 

May 25, 2009 at 1:28 AM

Cheeso,

Thanks for the reply. I appreciate your help. Of course I am new to this I am learning .NET using C#.  I am not too familiar with the Treeview and its properties. So here is my attempt:

using (ZipFile zip = new ZipFile(Response.OutputStream))

    {

        foreach (TreeNode node in MyTree.CheckedNodes)

        {

            if (MyTree.SelectedNode != null)

            {

                if (Directory.Exists(MyTree.SelectedNode.Checked) == true)

                    {

                        zip.AddFile(MyTree.SelectedNode.Value);

                    }

            }

        }

        zip.Save();

    }

Now I am trying to keep the directory structure as is when zipping as there will be multiple folders and multiple files in them.

I don’t think I am doing this right would you mind helping me out.

Thanks again,

From: Cheeso [mailto:notifications@codeplex.com]
Sent: Sunday, May 24, 2009 2:36 PM
To: kjohnson@kristopherstudios.com
Subject: Re: TreeView w/ Checkboxes [DotNetZip:57316]

From: Cheeso

??? Is it ASP.NET or Windows Forms, or something else?

Regardless, it seems like you want to set up a button, with an OnClick event handler. Within the OnClick method, instantiate a ZipFile object. Then, walk the nodes of the tree, going through each one. For each node in the tree, if the checkbox is checked, then add the filename for that node to the ZipFile with zip.AddFile().

At the end of looping through all the nodes. call ZipFile.Save().

Is that helpful?

Read the full discussion online.

To add a post to this discussion, reply to this email (DotNetZip@discussions.codeplex.com)

To start a new discussion for this project, email DotNetZip@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Coordinator
May 25, 2009 at 8:00 AM

What you are doing seems ok to me.

You asked for help. But what's the problem you're having?