Click here to Skip to main content
Click here to Skip to main content

.NET Framework Zip / UnZip Tool Using the Packaging Namespace

, 10 Sep 2008
Rate this:
Please Sign up or sign in to vote.
An UI tool that can be used to zip and unzip a file.

Introduction

This tool can be used to create a Zip file, view a Zip file, and to unzip a selected Zip file at a user specified location. The creation of a Zip file does support for adding a file, creating a folder within the Zip, and deleting an existing file and folder.

Background

For this tool, I have used the System.IO.Packaging.Package namespace of the .NET Framework 3.5. Also, I have used Extension Methods to extend the features of the NodesCollection class. If you are not aware of this, please refer to MSDN.

Using the code

To use this tool, you need to have the .NET Framework 3.5 installed on your machine. If you are planning to view the code, then you need to have Visual Studio 2008. This tool is very simple to use. It has a root node by default, and it cannot be deleted. If the user wants to create a Zip file, the first step is to clear the tree view control. This is done by using the “Clear” push button provided on the left of the screen. The second step is to add files / folders. This functionality is provided by using a context menu of the tree view node. Once the user prepares the virtual Zip file, use the “Zip” push button to select a physical folder and file for creating the physical zip file. To view the Zip file, the user can click on the “View” push button on the left. This will prompt for a file dialog window to select the Zip file. Once the user selects the Zip file, a virtual structure of the complete Zip file will be represented in tree format. Please note, currently, this tool does not support modifying an existing Zip file. The design of this tool does support to integrate this functionality. To unzip a Zip file, click on the “Unzip” push button. This will allow you to select a Zip file and the location where the files need to be unzipped.

Note: This tool can be extended for advance functionality. But, currently, this tool is developed to know the code-behind process used for Microsoft Office 2007 Document creation and updating.

For this prototype version of code using the System.IO.Packaging.Package namespace, processing of each push button is written within the code-behind of UI form. The best approach would be to have a separate service class that supports these functionality.

/*
 UI that uses Packaging Namespace of .Net Framework 3.5 for 
 creation of Zip file. This tool does zipping of single and multiple files
 * Developer : Mayur H Chauhan
 * Location  : Iselin USA
 * Date      : 09/02/2008
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Packaging;     

namespace ZipFile
{
    public partial class frmZipTool : Form
    {

        #region " List of member identifier"
            private FolderNode  root = null;
            private ContextMenu _contextMenu = null;
        #endregion

        #region " Methods to Initialize UI"

            public frmZipTool()
            {
                InitializeComponent();
            }

            private void frmZipTool_Load(object sender, EventArgs e)
            {
                ClearTree();

                // Define context menu and set 
                // it to only Folder node and Root node
                _contextMenu = new ContextMenu();

                MenuItem addfile = new MenuItem("Add File(s)", 
                                   new EventHandler(AddFilesMenuClick));
                _contextMenu.MenuItems.Add(addfile);
                MenuItem createfolder = new MenuItem("Create Folder", 
                                        new EventHandler(CreateFolderMenuClick));
                _contextMenu.MenuItems.Add(createfolder);
                MenuItem delete = new MenuItem("Delete", 
                                  new EventHandler(DeleteMenuClick));
                _contextMenu.MenuItems.Add(delete);

                tvFilesAndFolders.ContextMenu = _contextMenu;
                tvFilesAndFolders.SelectedNode = root; 
            }

        #endregion

        #region"Method related to context menu"

            /// <span class="code-SummaryComment"><summary></span>
            /// Method to execute Add File(s) process of menu click
            /// <span class="code-SummaryComment"></summary></span>
            /// <span class="code-SummaryComment"><param name="sender"></param></span>
            /// <span class="code-SummaryComment"><param name="e"></param></span>
            protected void AddFilesMenuClick(object sender, EventArgs e)
            {
                System.Text.StringBuilder filesNotAdded = 
                                          new System.Text.StringBuilder();    
                
                //add all the files that is been selected by the user
                TreeNode parentNode = ((TreeView)((System.Windows.Forms.MenuItem)
                          (sender)).GetContextMenu().SourceControl).SelectedNode;

                // Check for the type of the node selected.
                // If it is Folder Type, then allow user to add file.
                // Else show message and cancel the further process.
                if (parentNode.GetType() == typeof(FolderNode))
                {
                    using (OpenFileDialog openfileDialog = new OpenFileDialog())
                    {
                        openfileDialog.Filter = "All Files|*.*";
                        openfileDialog.Multiselect = true;
                        if (openfileDialog.ShowDialog() != DialogResult.Cancel)
                        {
                            foreach (string file in openfileDialog.FileNames)
                            {
                                //Check for file with these name already exists.
                                //If so then we need to show some kind of message.
                                if (parentNode.Nodes.ContainsKey(
                                    System.IO.Path.GetFileName(file)) == false)
                                {
                                    FileNode newfile = 
                                      new FileNode(System.IO.Path.GetFileName(file));
                                    newfile.Text = System.IO.Path.GetFileName(file);
                                    newfile.Tag = file;
                                    newfile.fileStorageType = 
                                      FileNode.enumFileStorageType.enmPhysicalFile;   
                                    parentNode.Nodes.Add(newfile);
                                    parentNode.ExpandAll(); 
                                }
                                else
                                {
                                    if (filesNotAdded.Length > 0)
                                    {
                                        filesNotAdded.Append("\n");
                                    }
                                    else
                                    { }
                                    filesNotAdded.Append(System.IO.Path.GetFileName(file)); 
                                }
                            }

                            //Check for any file not added. If so, 
                            //then we need so show message to the user
                            if (filesNotAdded.Length > 0)
                            {
                                System.Text.StringBuilder message = 
                                      new System.Text.StringBuilder();

                                message.Append("Following file(s) are not" + 
                                        " added as there already " + 
                                        "exists file with same name");
                                message.Append("\n\n");
                                message.Append(filesNotAdded.ToString());

                                showMessage(message.ToString(), 
                                            MessageBoxButtons.OK);     
                            }
                            else
                            {
                                //do nothing
                            }


                        }
                    }
                }
                else
                {
                    MessageBox.Show("Files can be added only to the Folder Node");  
                }
                
            }

            /// <span class="code-SummaryComment"><summary></span>
            /// Method to execute create Folder process of menu click
            /// <span class="code-SummaryComment"></summary></span>
            /// <span class="code-SummaryComment"><param name="sender"></param></span>
            /// <span class="code-SummaryComment"><param name="e"></param></span>
            protected void CreateFolderMenuClick(object sender, EventArgs e)
            {
                TreeNode parentNode = ((TreeView)((System.Windows.Forms.MenuItem)
                         (sender)).GetContextMenu().SourceControl).SelectedNode;
                // Check for the type of the node selected.
                // If it is Folder Type, then allow user to add file.
                // Else show message and cancel the further process.
                if (parentNode.GetType() == typeof(FolderNode))
                {
                    // Here we need to create node
                    // of type Folder Node and give name to it
                    string folderName = 
                      Microsoft.VisualBasic.Interaction.InputBox(
                      "Enter folder name", ".Net 3.5 Tool", string.Empty, 
                      MousePosition.X, MousePosition.Y);
                    if (folderName.Equals(string.Empty) == false)
                    {
                        //Check for the folder already exists with this name. 
                        //If so then we need to show
                        //message and ignore adding this folder.
                        if (parentNode.Nodes.ContainsKey(folderName))
                        {
                            System.Text.StringBuilder message = 
                                    new System.Text.StringBuilder();

                            message.Append("Folder with name ");
                            message.Append(folderName);
                            message.Append(" already exists");
                            showMessage(message.ToString(), MessageBoxButtons.OK);
                            message = null; 
                        }
                        else
                        {
                            FolderNode newFolder = new FolderNode(folderName);
                            newFolder.Text = folderName;
                            parentNode.Nodes.Add(newFolder);
                            parentNode.ExpandAll(); 
                        }
                        
                        
                    }
                }
                else
                {
                    MessageBox.Show("Folder can be created only to Folder Node");  
                }
            }

            /// <span class="code-SummaryComment"><summary></span>
            /// Method to execute Delete process of Menu click
            /// <span class="code-SummaryComment"></summary></span>
            /// <span class="code-SummaryComment"><param name="sender"></param></span>
            /// <span class="code-SummaryComment"><param name="e"></param></span>
            protected void DeleteMenuClick(object sender, EventArgs e)
            {
                //Here check for the type of the Node. If it is Root node,
                //then do not allow user to delete that node
                TreeNode nodeToDelete = ((TreeView)((System.Windows.Forms.MenuItem)
                        (sender)).GetContextMenu().SourceControl).SelectedNode;
                if (nodeToDelete.Equals(root))
                {
                    showMessage("Root Node cannot be deleted", MessageBoxButtons.OK   );  
                }
                else
                { 
                    //Check for the type of the node. If it is Folder node 
                    //and if it has any child node then show the warning.
                    if (nodeToDelete.GetType() == typeof(FolderNode))
                    {
                        if (nodeToDelete.Nodes.Count > 0)
                        {
                            if (showMessage("Deleting this folder will remove all " + 
                                            "the file(s) and folder(s) under it." + 
                                            " Do you want to continue ?", 
                                            MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                nodeToDelete.Remove();  
                            }
                            else
                            { 
                                //Cancel delete process
                            }
                        }
                        else
                        {
                            if (showMessage("Do you want to delete selected folder ?", 
                                MessageBoxButtons.OKCancel) == DialogResult.OK)
                            {
                                nodeToDelete.Remove();  
                            }
                            else
                            { 
                                //Cancel this process
                            }
                        }
                    }
                    else
                    {
                        if (showMessage("Do you want to delete selected file ?", 
                                        MessageBoxButtons.OKCancel) == DialogResult.OK)
                        {
                            nodeToDelete.Remove();
                        }
                        else
                        {
                            //Cancel this process
                        }
                    }
                }
            }

            #endregion

        #region"Method to clear Tree"

            /// <span class="code-SummaryComment"><summary></span>
        /// Clear all the nodes except Root node
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="sender"></param></span>
        /// <span class="code-SummaryComment"><param name="e"></param></span>
        private void btnClear_Click(object sender, EventArgs e)
        {
            ClearTree();
        }

        /// <span class="code-SummaryComment"><summary></span>
        /// Method to clear Tree
        /// <span class="code-SummaryComment"></summary></span>
        protected void ClearTree()
        {
            // By default have one Root Node
            root = new FolderNode();
            root.Text = "./";
            root.Name = "root";
            tvFilesAndFolders.Nodes.Clear();
            tvFilesAndFolders.Nodes.Add(root);
        }

        #endregion

        #region " Method to create Zip file"

        private void btnZip_Click(object sender, EventArgs e)
        {
            string zipFilePath = string.Empty;

            toolStripStatusLabel2.Text = " In Process"; 
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = "Zip File| *.zip";
                saveFileDialog.DefaultExt = ".zip";
                saveFileDialog.OverwritePrompt = true;
                if (saveFileDialog.ShowDialog() != DialogResult.Cancel)
                {
                    zipFilePath = saveFileDialog.FileName;
                }

            }
            //Create Zip file
            if (zipFilePath != string.Empty)
            {
                try
                {
                    if (System.IO.File.Exists(zipFilePath))
                    {
                        System.IO.File.Delete(zipFilePath);
                    }
                }
                catch
                { }

                System.IO.Packaging.Package ZipFile = 
                  System.IO.Packaging.ZipPackage.Open(zipFilePath, 
                  System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);

                //Itterate through the all the sublings of the root node and 
                //add them within the zip file. There is possible that the child 
                //nodes can be an file node or it can be and Folder node. 
                //In case if it is folder node, then there is possiblity 
                //that it can have child nodes below it.
                AddFilesAndFolder(root, ZipFile);
                
                ZipFile.Close();
            }
            else
            {
                // do nothing
            }
            toolStripStatusLabel2.Text = " Process Completed";
        }

        /// <span class="code-SummaryComment"><summary></span>
        /// Method to add files and folder within the zip file
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="zipFilePackage"></param></span>
        protected void AddFilesAndFolder(TreeNode rootnode, 
                       System.IO.Packaging.Package zipFilePackage)
        {
            //Check for the type of the node. In case of the Root node, 
            //the type would be tree node. In case of user 
            //defined folder node, it would be FolderNode
            
            System.Type nodeType = rootnode.GetType();


            if ((nodeType == typeof(TreeNode)) || (nodeType == typeof(FolderNode)))
            {
                foreach (TreeNode childNode in rootnode.Nodes)
                {
                    if (childNode.GetType() == typeof(FileNode))
                    {
                        //Add the file within the package
                        AddFileToZip(((FileNode)childNode), zipFilePackage);
                    }
                    else if (childNode.GetType() == typeof(FolderNode))
                    {
                        AddFilesAndFolder(childNode, zipFilePackage);
                    }
                }
            }
        }

        /// <span class="code-SummaryComment"><summary></span>
        /// Method to add files and folder within the zip file package
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="parentnode"></param></span>
        /// <span class="code-SummaryComment"><param name="zipFile"></param></span>
        protected void AddFileToZip(FileNode filenode, 
                       System.IO.Packaging.Package zipFilePackage)
        {
            //Here get the file node and add it in the Zip file. Make sure 
            //to maintain the folder structure that is defined within the tree view.
            string physicalfilePath = (string)filenode.Tag;

            //Check for file existing. If file does not exists,
            //then add in the report to generate at the end of the process.
            if (System.IO.File.Exists(physicalfilePath))
            {
                string treeFilePath = filenode.FullPath;

                // Remove the section of the path that has "root defined"
                treeFilePath = treeFilePath.Replace("./", "");

                // Incase if there is space in the file name, then this won't work.
                // So we need to remove space from the file name and replace it with "_"
                string fileName = System.IO.Path.GetFileName(treeFilePath);

                treeFilePath = treeFilePath.Replace(fileName, 
                                  fileName.Replace(" ", "_"));

                //create Packagepart that will represent file that we will add. 
                //Define URI for this file that needs to be added within the Zip file.
                //Define the added file as related to the Zip file in terms of the path.
                //This part will define the location where this file needs 
                //to be extracted while the zip file is getting exteacted.
                Uri partURI = new Uri(treeFilePath, UriKind.Relative);

                string contentType = System.Net.Mime.MediaTypeNames.Application.Zip;

                //Define the Content Type of the file that we will be adding. 
                //This depends upon the file extension
                switch (System.IO.Path.GetExtension(treeFilePath).ToLower())
                {
                    case (".xml"):
                        {
                            contentType = System.Net.Mime.MediaTypeNames.Text.Xml;
                            break;
                        }

                    case (".txt"):
                        {
                            contentType = System.Net.Mime.MediaTypeNames.Text.Plain;
                            break;
                        }

                    case (".rtf"):
                        {
                            contentType = System.Net.Mime.MediaTypeNames.Application.Rtf;
                            break;
                        }

                    case (".gif"):
                        {
                            contentType = System.Net.Mime.MediaTypeNames.Image.Gif;
                            break;
                        }
                    case (".jpeg"):
                        {
                            contentType = System.Net.Mime.MediaTypeNames.Image.Jpeg;
                            break;
                        }

                    case (".tiff"):
                        {
                            contentType = System.Net.Mime.MediaTypeNames.Image.Tiff;
                            break;
                        }
                    case (".pdf"):
                        {
                            contentType = 
                                  System.Net.Mime.MediaTypeNames.Application.Pdf;
                            break;
                        }
                    case (".doc"):
                    case (".docx"):
                    case (".ppt"):
                    case (".xls"):
                        {
                            contentType = System.Net.Mime.MediaTypeNames.Text.RichText;
                            break;
                        }
                }

                System.IO.Packaging.PackagePart newFilePackagePart = 
                       zipFilePackage.CreatePart(partURI, 
                       contentType, CompressionOption.Normal);
                byte[] fileContent = System.IO.File.ReadAllBytes(physicalfilePath);

                newFilePackagePart.GetStream().Write(fileContent, 0, fileContent.Length);

            }
            else
            {
                // Add within the collection and use it to generate report.
            }
        }

        #endregion

        #region " Method(s) to close the application"
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        
        #endregion

        #region "Methods to view Zip file content"

        /// <span class="code-SummaryComment"><summary></span>
        /// Method to view Zip file content
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="sender"></param></span>
        /// <span class="code-SummaryComment"><param name="e"></param></span>
        private void btnView_Click(object sender, EventArgs e)
        {
            //Here we can go though all the subpackage within the Zip file package 
            //and create its node within the Tree view. In case if it has folder, 
            //then we need to create folder node within the tree view and represent it
            toolStripStatusLabel2.Text = "In Process";
            string zipFilePath = string.Empty;
            // Method to unzip file selected to the selected location
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Zip file|*.zip";
                openFileDialog.CheckFileExists = true;
                openFileDialog.Multiselect = false;
                openFileDialog.DefaultExt = ".zip";
                if (openFileDialog.ShowDialog() != DialogResult.Cancel)
                {
                    zipFilePath = openFileDialog.FileName;
                }
            }

            if (zipFilePath != string.Empty)
            {

                System.IO.Packaging.Package zipFilePackage = 
                  System.IO.Packaging.ZipPackage.Open(zipFilePath, 
                  System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);

                //Iterate through the all the files
                //that is added within the collection and 
                foreach (System.IO.Packaging.ZipPackagePart 
                         contentFile in zipFilePackage.GetParts())
                {
                    createFileNode(contentFile);
                }

                zipFilePackage.Close();

                tvFilesAndFolders.ExpandAll();  
            }
            else
            {
                //do nothing
            }
            toolStripStatusLabel2.Text = " Process Completed";
        }

        /// <span class="code-SummaryComment"><summary></span>
        /// Method to create file at the temp folder
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="rootFolder"></param></span>
        /// <span class="code-SummaryComment"><param name="contentFileURI"></param></span>
        /// <span class="code-SummaryComment"><returns></returns></span>
        protected void createFileNode(System.IO.Packaging.ZipPackagePart contentFile)
        {
            // Here we need to create folder and sub folder 
            // for showing the provided package part within the tree.
            // Initially create file under the folder specified
            string contentFilePath = contentFile.Uri.OriginalString;
            
            //Call method to create Directory within the Root directory 
            //and then add the file within that directory
            FolderNode parentFolderNode = createDirectory(
                     System.IO.Path.GetDirectoryName(contentFilePath));

            FileNode file = 
               new FileNode(System.IO.Path.GetFileName(contentFilePath));
            file.fileStorageType = FileNode.enumFileStorageType.enmTextStream;
            file.Text = System.IO.Path.GetFileName(contentFilePath);
            file.Tag = contentFile;  
            parentFolderNode.Nodes.Add(file);  
        }

        /// <span class="code-SummaryComment"><summary></span>
        /// Method to create di
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="directoryPath"></param></span>
        /// <span class="code-SummaryComment"><returns></returns></span>
        protected FolderNode createDirectory(string directoryPath)
        {
            //Here we need to check for the folder an the sub folder exists. 
            //If not then create folder. Once all the folder and sub folder 
            //is created, then return the immediate parent of the file that 
            //is defined within the directory path. 
            //In other words, return last sub folder.
            string[] directoryList = directoryPath.Split('\\');
            FolderNode currentNode = root;
            foreach (string subdirectory in directoryList)
            {
                if (subdirectory!= string.Empty)
                {
                    if (currentNode.Nodes.ContainsKey(subdirectory) == false)
                    {
                        FolderNode subDirectoryNode = new FolderNode(subdirectory);
                        subDirectoryNode.Text = subdirectory;
                        currentNode.Nodes.Add(subDirectoryNode);
                        currentNode = subDirectoryNode;
                    }
                    else
                    {
                        //do nothing as folder is already created
                        currentNode = 
                           (FolderNode)currentNode.Nodes.GetNodeByKey(subdirectory);
                    }
                }
                else
                {
                    //do nothing
                }
            }
            return currentNode;
        }

        #endregion

        #region" Method to Unzip file at selected location"

        /// <span class="code-SummaryComment"><summary></span>
        /// Method to unzip Zip file at specified location
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="sender"></param></span>
        /// <span class="code-SummaryComment"><param name="e"></param></span>
        private void btnUnZip_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel2.Text = "In Process";   
            string zipFilePath = string.Empty;
            // Method to unzip file selected to the selected location
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Zip file|*.zip";
                openFileDialog.CheckFileExists = true;
                openFileDialog.Multiselect = false;
                openFileDialog.DefaultExt = ".zip";
                if (openFileDialog.ShowDialog() != DialogResult.Cancel)
                {
                    zipFilePath = openFileDialog.FileName;
                }
            }

            if (zipFilePath != string.Empty)
            {

                string unZipFolderLocation = string.Empty;
                using (FolderBrowserDialog unzipFolderLocation = 
                       new FolderBrowserDialog())
                {
                    unzipFolderLocation.RootFolder = 
                         Environment.SpecialFolder.DesktopDirectory;
                    unzipFolderLocation.Description = "Select Folder to Unzip " + 
                                System.IO.Path.GetFileName(zipFilePath) + " zip file";
                    if (unzipFolderLocation.ShowDialog() != DialogResult.Cancel)
                    {
                        unZipFolderLocation = unzipFolderLocation.SelectedPath;
                    }
                    else
                    { 
                        //do nothing
                    }
                }

                if ((zipFilePath != string.Empty) && (unZipFolderLocation != string.Empty))
                {

                    System.IO.Packaging.Package zipFilePackage = 
                       System.IO.Packaging.ZipPackage.Open(zipFilePath, 
                       System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);

                    //Iterate through the all the files that 
                    //is added within the collection and 
                    foreach (System.IO.Packaging.ZipPackagePart 
                             contentFile in zipFilePackage.GetParts())
                    {
                        createFile(unZipFolderLocation, contentFile);
                    }

                    zipFilePackage.Close();

                    if (showMessage("Do you want to view selected zip file", 
                                    MessageBoxButtons.OKCancel) == DialogResult.OK)

                    {
                        // Display within the view
                        zipFilePackage = System.IO.Packaging.ZipPackage.Open(zipFilePath, 
                                         System.IO.FileMode.Open, 
                                         System.IO.FileAccess.ReadWrite);

                        //Iterate through the all the files that 
                        //is added within the collection and 
                        foreach (System.IO.Packaging.ZipPackagePart 
                                       contentFile in zipFilePackage.GetParts())
                        {
                            createFileNode(contentFile);
                        }

                        zipFilePackage.Close();

                        tvFilesAndFolders.ExpandAll();  
                    }
                    else
                    { 
                        //do nothing
                    }
                    

                }
                else
                { 
                    // do nothing
                }

            }
            else
            {
                //do nothing
            }
            toolStripStatusLabel2.Text = " Process Completed";
        }
        
            /// <span class="code-SummaryComment"><summary></span>
        /// Method to create file at the temp folder
        /// <span class="code-SummaryComment"></summary></span>
        /// <span class="code-SummaryComment"><param name="rootFolder"></param></span>
        /// <span class="code-SummaryComment"><param name="contentFileURI"></param></span>
        /// <span class="code-SummaryComment"><returns></returns></span>
        protected void  createFile(string rootFolder, 
                        System.IO.Packaging.ZipPackagePart contentFile)
        {
            // Initially create file under the folder specified
            string contentFilePath = string.Empty;
            contentFilePath =contentFile.Uri.OriginalString.Replace('/', 
                             System.IO.Path.DirectorySeparatorChar);

            if (contentFilePath.StartsWith(
                System.IO.Path.DirectorySeparatorChar.ToString()))
            {
                contentFilePath = contentFilePath.TrimStart(
                                         System.IO.Path.DirectorySeparatorChar);
            }
            else
            {
                //do nothing
            }

            contentFilePath = System.IO.Path.Combine(rootFolder, contentFilePath); 
            //contentFilePath =  System.IO.Path.Combine(rootFolder, contentFilePath); 

            //Check for the folder already exists. If not then create that folder

            if (System.IO.Directory.Exists(
                System.IO.Path.GetDirectoryName(contentFilePath)) != true)
            {
                System.IO.Directory.CreateDirectory(
                          System.IO.Path.GetDirectoryName(contentFilePath));
            }
            else
            {
                //do nothing
            }
   
            System.IO.FileStream newFileStream = 
                    System.IO.File.Create( contentFilePath );
            newFileStream.Close(); 
            byte[] content = new byte[contentFile.GetStream().Length];
            contentFile.GetStream().Read(content, 0, content.Length );
            System.IO.File.WriteAllBytes(contentFilePath, content);

        } 

        #endregion 

        protected DialogResult 
                  showMessage(string message,MessageBoxButtons button)
        {
            return MessageBox.Show (message, ".Net Zip / UnZip Tool", 
                                    button, MessageBoxIcon.Information  );     
        }
    } // End of the Class

    /// <span class="code-SummaryComment"><summary></span>
    /// Definition of File Node class
    /// <span class="code-SummaryComment"></summary></span>
    public class FileNode : TreeNode
    {

        //Define Enum for the type in which file is represented 
        //within the Tree Node. This is needed in case user 
        //want to view the file from the Tree view.
        public enum enumFileStorageType 
          { enmPhysicalFile, enmTextStream, enmUnKnown }

        protected enumFileStorageType _fileStorageType = 
                  enumFileStorageType.enmUnKnown; 

        public  FileNode()
        { }

        public FileNode(string key)
            : base(key)
        { base.Name = key; }

        /// <span class="code-SummaryComment"><summary></span>
        /// Returns Storage Type of this node
        /// <span class="code-SummaryComment"></summary></span>
        public enumFileStorageType fileStorageType
        {
            get
            {
                return _fileStorageType;
            }
            set
            {
                _fileStorageType = value;
            }

        }
    }

    /// <span class="code-SummaryComment"><summary></span>
    /// Definition of Folder Node class
    /// <span class="code-SummaryComment"></summary></span>
    public class FolderNode : TreeNode
    {
        public FolderNode()
        { }
        

        public FolderNode(string key)
            : base(key)
        { base.Name = key; }
        
    }
}

Points of Interest

There are a couple of things to know:

  • Creating a Zip file using Packaging.ZipPackage will create a “[Content_Types].xml” file. This defines the content of the zip file and the mapping for the file extension.
  • A file that we add within the Zip file cannot have space within its name. Currently, this tool replaces any space with an underscore (“_”) character.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Mayur Chauhan
Team Leader Orion Systems Integrators
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 PinmemberGnerk Flater21-Jul-09 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 10 Sep 2008
Article Copyright 2008 by Mayur Chauhan
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid