The basic implementation of this functionality is just as simple as zipping a file.
#CREATING A ZIP FOLDER HOW TO#
I will show you below how to extend this functionality to improve how this method works.
#CREATING A ZIP FOLDER CODE#
The problem I see with this is that it will require you to write the same code every time you use this class. One solution would be to check for the existence of the file before creating it. If you ran both of these examples together, the system will throw an error on the second file, since the output file will already exist (since they are named the same). If you think this through, you may see a shortcoming with our current examples: we didn’t check for the existence of the output zip file.
This example is the same as above except that it includes the root director in the zip file. ZipFile.CreateFromDirectory( C:\Temp\Logs", C:\Temp\LogFiles.zip", CompressionLevel.Optimal, true)
Here is an example to help you visualize (this is Example 2): The only other overload for the CreateFromDirectory method includes two additional options: compression and include root directory. We do include all of the folders and files in the root directory, just not the directory itself. The other default here is that we will not include the root directory in the zip file. By default, the method assumes Optimal compression (the other options are Fastest and None). Finally, note that there is no mention of compression. We are telling the method just two pieces of information: what to zip and where to put it. That allows us to only put in one slash for each path segment. First, in case you are not aware, the symbol tells the compiler that the following string is a string literal (no special characters need to be translated – just display whatever is between the quotes). ZipFile.CreateFromDirectory( C:\Temp\Logs", C:\Temp\LogFiles.zip") Here is an example call (this is Example 1):
#CREATING A ZIP FOLDER ZIP FILE#
You need to pass in first the root folder to zip and then the full name of the zip file to be created (which includes a relative or absolute path). To zip up the contents (including sub-folders) of a folder, simply call the CreateFromDirectory method of ZipFile. The code will have a corresponding example number above it in the comments. Whenever you come across an example that is numbered below, you can see that same example in the source code attached to this article. With these steps completed, we are ready to start coding. I also added using statements for these two namespaces, as well as for System.IO for the sake of simplicity.
Simply adding System.IO.Compression as a using statement will not work until you add it as a reference too. Also, you will need to add references to both System.IO.Compression and System.IO.Compression.ZipArchive. I built the examples we are about to see in the beta of Visual Studio 11, so if you download the solution file attached to this article you will need to use the beta as well. In order to use the new functionality of the Compression namespace, you need to have. Finally, I will go over how to extend the current functionality in order to shore up some of the simpler weaknesses that we will discuss. I will demonstrate how to perform these actions and how to avoid the pitfalls in using some of these methods. Now, in a very simple manner, we can perform zip and unzip actions. NET 4.5 is the vast improvement in the System.IO.Compression namespace.