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.

 

Quick Tip: How to ZIP/Unzip files using .NET/C# (www.kunal-chowdhury.com)

 

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.

 

// Creates a zip archive that contains the files and directories from the specified
// directory, uses the specified compression level, and optionally includes
// the base directory.
//
// SOURCE_FOLDER:
//      The path to the directory to be archived, specified as a relative or absolute
//      path. A relative path is interpreted as relative to the current working directory.
//
// ZIP_FILE_PATH:
//      The path of the archive to be created, specified as a relative or absolute
//      path. A relative path is interpreted as relative to the current working directory.
//
// CompressionLevel:
//      One of the enumeration values that indicates whether to emphasize speed or
//      compression effectiveness when creating the entry.
//      It can be either "Optimal", "Fastest" or "NoCompression".
//
// IncludeBaseDirectory:
//      Set true to include the directory name from sourceDirectoryName at the root of
//      the archive; set false to include only the contents of the directory.
ZipFile.CreateFromDirectory(SOURCE_FOLDER, ZIP_FILE_PATH, CompressionLevel.Optimal, true);

 

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:

 

// Extracts all the files in the specified zip archive to a directory on the file system.
//
// ZIP_FILE_PATH:
//      The path of the archive to be created, specified as a relative or absolute
//      path. A relative path is interpreted as relative to the current working directory.
//
// DESTINATION_FOLDER:
//      The path to the directory in which to place the extracted files, specified
//      as a relative or absolute path. A relative path is interpreted as relative
//      to the current working directory.
ZipFile.ExtractToDirectory(ZIP_FILE_PATH, DESTINATION_FOLDER);

 

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!

 

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.