In my previous post I shared some information on API’s/Classes included as part of System.IO.Compression namespace in .NET Framework 4.5, and given on overview of ZipArchive class. Once such class I would be sharing some insight with post today would be ‘ZipFile‘ class. The ZipFile class provides convenient static methods for working with zip archives:
- CreateFromDirectory (3 overloads) – Creates a zip archive that contains the files and directories from the specified directory, uses the specified compression level and character encoding for entry names, and optionally includes the base directory.
- ExtractToDirectory ( 3 overloads) – Extracts all the files in the specified zip archive to a directory on the file system and uses the specified character encoding for entry names.
- Open (2 overloads) – Opens a zip archive at the specified path, in the specified mode, and by using the specified character encoding for entry names.
- OpenRead – Opens a zip archive for reading at the specified path.
To use these methods, you must reference the System.IO.Compression.FileSystem assembly in your project.
NB:
- The System.IO.Compression.FileSystem assembly is not available for Windows Store apps. Therefore, the ZipFile class and ZipFileExtensions class (which is also in the System.IO.Compression.FileSystem assembly) are not available in Windows Store apps.
- In Windows Store apps, you work with compressed files by using the methods in the ZipArchive, ZipArchiveEntry, DeflateStream, and GZipStream classes.
Now the time for creating some sample application.
For the purpose of explaining how to use the above methods – I followed the below steps :
- Created a console application in visual studio.
- Added Reference to System.IO.Compression and System.IO.Compression.FileSystem assemblies which is part of .NET Framework 4.5.
- Created a folder called “Files” and created some plain text files(.txt extension) , this would be out source folder to zip. And created two additional folder for storing zip files(Output) and ExtractLocation folder to extract the zip files.
- and the below sample code snippet is the implementation/usage.
<code><p>Sample code snippet:</p> <div> <pre class="brush: c#;"> </code>
namespace ConsoleApp02
{
class Program
{
static void Main(string[] args)
{
// AppDomain.CurrentDomain.BaseDirectory refers to the
//folder in which the executable or binaries are executing.
// E.g. ConsoleApp02ConsoleApp02binDebug
string startPath = AppDomain.CurrentDomain.BaseDirectory + "\Files";
string zipPath = AppDomain.CurrentDomain.BaseDirectory + "\Output";
string zipFilePath = zipPath + "\" + System.Guid.NewGuid().ToString() + ".zip";
<code> string extractPath = AppDomain.CurrentDomain.BaseDirectory + &quot;\ExtractLocation&quot;; //just a fail-safe to create folders if not exists. if (!Directory.Exists(zipFilePath)) Directory.CreateDirectory(zipFilePath); if (!Directory.Exists(extractPath)) Directory.CreateDirectory(extractPath); //Creating a zipFile from folder ZipFile.CreateFromDirectory(startPath, zipFilePath); //Unzipping a zipFile to a folder ZipFile.ExtractToDirectory(zipFilePath, extractPath); // Console.WriteLine(&quot;Press Any key to exit...&quot;); Console.ReadKey(); } } </code>
}
Hope this post is informative. Please keep share this post and give your comments/feedback. Happy coding!
Discover more from Cloud Distilled ~ Nithin Mohan
Subscribe to get the latest posts sent to your email.