How to ZIP/Unzip files using .NET/C# (without 3rd party libraries)?


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!

 


If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter , Facebook , Google+ and LinkedIn . I would love to hear your thoughts and opinions on my articles directly. Also, don't forget to share your views and/or feedback in the comment section below.

8 comments


  1. I feel happy about and I love learning more about this topic. keep sharing your information regularly for my future reference. This content creates a new hope and inspiration with in me. Thanks for sharing article like this. The way you have stated everything above is quite awesome. Keep blogging like this. Thanks.

    Regards,
    Ridhima

    Informatica Training in Adyar

    ReplyDelete
  2. Thanks for offering this useful information with useful tips. This blog is really very informative and to achieve the perfect destination

    SAP ABAP Training in Chennai

    ReplyDelete
  3. I like the fact that .NET framework makes it possible for us to compress files inside Zip and extract them on need basis without using third party libraries. Are you a student and you have been wondering about where you can get homework paraphrasing help? If yes, then feel free to click on the link below:
    People who Paraphrase Homework

    ReplyDelete
  4. dude, this is only available for .net 4.5 and above. .net 3 and 3.5 does not have this file..

    ReplyDelete
  5. dude, this is only available for .net 4.5 and above. .net 3 and 3.5 does not have this file..

    ReplyDelete
  6. Great post. Happy to visit your blog. Thanks for sharing.

    php training in chennai

    ReplyDelete


 
© 2008-2016 Kunal-Chowdhury.com - Microsoft Technology Blog for developers and consumers | Designed by Kunal Chowdhury
Back to top