If you are building a custom installer of your own, you might want to compress the files inside a ZIP and extract them on need basis. You can easily do this by using some 3rd party libraries, among which some are free and some are paid versions. Why to use additional libraries if the .NET framework provides you the set of APIs?
You might be aware of this, but if you are unaware of it, let me tell you that, it was present since .NET 2.0. Let’s explore it in today’s quick blog tips & tricks. Continue reading and share the link in your network.
Microsoft .NET Framework provides a static class named “ZipFile” under the namespace “System.IO.Compression”, which allows you to zip a folder and/or extract a zip file to a directory. To use this class under the namespace, you have to reference the assembly “System.IO.Compression.FileSystem.dll” in your project, without which you will not be able to access this class.
The class provides you a set of methods, which allows you to open a zip archive at the specified path and in the specified mode, create a zip archive that contains the files and directories from the specified directory and also allows you to extract all the files in the specified zip archive to a directory on the file system.
ZipFile.CreateFromDirectory method takes 2-5 parameters to create a ZIP archive from a directory having files and sub directories. First two parameters are compulsory which allows you to set the source folder and zip file path. Third parameter allows you to set the compression level, which can be either Optimal, Fastest or NoCompression.
When you set “Optimal”, that means that the compression operation should be optimally compressed even if the operation takes a longer time to complete. When you set it to “Fastest”, the compression operation should complete as quickly as possible even if the resulting file is not optimally compressed. “NoCompression” will create the archive without compressing the files. The forth parameter allows you to set whether you want to include the root directory in the archive file. If you set it to “false”, only the contents of the directory will be archived. The fifth parameter allows you to set the character encoding for entry names in the archived file.
If you want to extract a ZIP file to a directory, you can call the ZipFile.ExtractToDirectory method by passing the required parameters. This method can accept 2-3 parameters. The first two parameters are mandatory, which allows you to set the ZIP file path and the destination folder path. The third parameter is option and when set, allows you to provide the character encoding for entry names while extracting the file.
Have a quick look into the below code snippet to easily understand the way to extract all the files of a specific ZIP file to a directory of your choice on the file system:
The above two examples shows you how to create and extract a zip archive by using the ZipFile class. It compresses the contents of a folder into a zip archive and then extracts that content to a new folder. I hope that the post was simple and easy to use. Next we will cover more on this topic but in a different thread. Till that time, happy coding!