I'm currently having the problem that the DotNetZip-library does not work as expected. I have a treeview that contains different nodes:
Each of these nodes is filled with folders and files:
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
- Audio
and then in the Program-directory if the ZIP it should be like that:
- Program
- Audio
- File1.wav
- File2.mp3
- Audio
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!