1

I'm currently having the problem that the DotNetZip-library does not work as expected. I have a treeview that contains different nodes:

enter image description here

Each of these nodes is filled with folders and files:

enter image description here

As the name already suggests, the Add folder button can be used to add a directory. For example, the "Audio" folder was added to the treeview, using a recursive algorithm which adds all subfolders and files of the "Audio" folder.

The next step is to create a zip file which contains all elements of treeview. To create the zip file, I am using the following code:

private void InitializeArchiveContents(int nodeIndex, string currentDirectory)
    {
        foreach (TreeNode node in explorerTreeView1.Nodes[nodeIndex].Nodes)
        {
            FileAttributes attr = File.GetAttributes(node.Tag.ToString());
            if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
            {
                zip.AddDirectory(node.Tag.ToString(), currentDirectory);
                currentDirectory += String.Format("{0}/", Path.GetDirectoryName(node.Text));
            }
            else
            {
                zip.AddFile(node.Tag.ToString(), currentDirectory);
            }
        }
    }

Problem: Unfortunately, the zip file created by the code above does not contain the the "Audio" folder.

Here is a short explanation of the code above:

It takes the current node index (for example 0 stands for the program-node above) and then checks if it is a file or a directory. I added the whole path to the files to the Tag-property of the TreeNodes. The currentDirectory stands for the current folder in the archive. So at the beginning of my class I added a folder by name (AddDirectoryByName) to the zip. It's then called Program and in this folder inside the zip all the content from the TreeView's program node should be filled in. So every time a new directory comes to it I update the currentDirectory to keep the path inside the ZIP up-to-date to make sure the files are in the correct folders in the end.

So, if the path is a directory it adds a directory into the archive and if it is a file, it adds the file.

To make it clearer:

TreeView:

  • Program directory
    • Audio
      • File1.wav
      • File2.mp3

and then in the Program-directory if the ZIP it should be like that:

  • Program
    • Audio
      • File1.wav
      • File2.mp3

So the same structure. The problem now is that it does something like that in the end:

  • Program
    • File1.wav
    • File2.mp3

So, the Audio-folder is not added by the zip.AddDirectory-method, only its content. But I want that it also creates that folder to make it look like in the treeview.

Could you tell me how I could do that? I just want the DotNetZip-lib to create that subfolder.

I found this: Add Folders to Root of Zip Using Ionic Zip Library

The problem is, that the OP there has a problem which is my aim. Create a sub folder! Thanks ;)

If something is not clear, ask!

Community
  • 1
  • 1
Dominic B.
  • 1,897
  • 1
  • 16
  • 31
  • I think something wrong with `currentDirectory`. When you call for the first time `zip.AddDirectory(node.Tag.ToString(), currentDirectory);` files are added to the root folder, because `currentDirectory` is empty (or root folder). Can you check value of `current Directory` by the debugger? And this line makes no sense for me `currentDirectory += String.Format("{0}/", Path.GetDirectoryName(node.Text));` – shfire Jul 07 '14 at 20:09
  • Yes, it is the root folder at the beginning. I will check that with breakpoints tomorrow. The last line is there to keep the current directory in the archive up-to-date to paste the files into the right directory. – Dominic B. Jul 07 '14 at 20:24
  • If you have another folder in the program directory (Video for example), `currentDirectory` at the end will by `Audio/Video/`. – shfire Jul 07 '14 at 20:34
  • Well, right. So, I will check that tomorrow and then it would be great if you help me to find a good solution. – Dominic B. Jul 07 '14 at 21:24
  • @shfire So when calling the function "InitializeArchiveContents" the currentDirectory-parameter is the root directory of course. It has the value "Program/" at the beginning. – Dominic B. Jul 08 '14 at 11:52

1 Answers1

1

You need to change recursion logic. And you dont need to use AddDirectory to add directory with entire content, because some files can be deleted from Treeview. Instead of this, you can add files to archive directly from Treeview.

InitializeArchiveContents(explorerTreeView1[nodeIndex], "Program/")

......

private void InitializeArchiveContents(TreeNode node, string currentDirectory)
{
    foreach (var node in node.Nodes)
    {
        var attr = File.GetAttributes(node.Tag.ToString());
        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
        {
            var tmpDir = string.Format("{0}/{1}", currentDirectory, node.Text);
            zip.AddDirectoryByName(tmpDir);
            InitializeArchiveContents(node, tmpDir);
        }
        else
        {
            zip.AddFile(node.Tag.ToString(), currentDirectory);
        }
    }
}
shfire
  • 837
  • 7
  • 7
  • Ok, thanks! I allowed myself to try the code and it throws me an exception in the marked line: http://www.trade-programming.de/pixelkram/yczdeivgwn.png It sais: An element with the same key was already added. Any ideas how I could fix this? – Dominic B. Jul 08 '14 at 17:58
  • What is the value of `tmpDir`? Are you creating a new archive or updating an existing? – shfire Jul 08 '14 at 18:12
  • So, at the beginning the value is "Program/". I am creating a new archive. – Dominic B. Jul 08 '14 at 18:16
  • `tmpDir` mast be `string.Format("{0}/{1}", currentDirectory, node.Text)` – shfire Jul 08 '14 at 18:37
  • Well ok, I debugged with breakpoints and saw that the problem was that there was a "Path.GetDirectoryName", which you edited, I think. With your help I did it! Thanks so much! – Dominic B. Jul 08 '14 at 19:11