File Zipper : Visual C#

  File Zipper is a Visual C# program for compressing and decompressing files. It creates .zip files and can decompress .zip files as well. The support for Zip files was a very useful feature introduced in .NET Framework 4.5. The DLL named System.IO.Compression.dll provides two new classes named ZipArchive and ZipFile. In this program, we have used the ZipFile class for accomplishing the compression and decompression of data.

If you just want to try the program, you can download File Zipper here or read more about it below. This program requires .NET Framework 4.5



File Zipper

  As mentioned above, we have used the ZipFile class in this program for compressing and decompressing files. This program uses two static methods of the ZipFile class, namely, CreateFromDirectory() and ExtractToDirectory(). The CreateFromDirectory() method creates a .zip file by compressing the contents of the specified directory. The ExtractToDirectory() method extracts the contents of .zip to the specified directory.

  Since the ZipFile class is a part of the System.IO.Compression namespace, we have included the statement using System.IO.Compression; in our program. Also, a reference to System.IO.Compression.dll has been added. We have also added using System.IO; because we had to use some methods of the Path class, which is a part of System.IO.

  As we mentioned above, the actual task of compression and decompression is pretty simple. We just need to call the methods CreateFromDirectory() and ExtractToDirectory() with the appropriate arguments. The real difficulty lies in figuring out the appropriate arguments for these methods. As you may see in the screenshots below, we have chosen to create a .zip file using the directory “C:\Users\Gaurav\ADB” and the output .zip file would be at “C:\Users\Gaurav\ADB.zip”. Also, we have chosen to decompress the “C:\Users\Gaurav\ADB.zip” and extract its contents to “C:\Users\Gaurav\ADB”. Simple as these things may seem, a fair bit of messing with strings is required.

  This is where the Path class plays its part. We have used the following static methods of the Path class : GetFullPath(), GetDirectoryName() and GetFileNameWithoutExtension(). The process of compression and decompression and the role of these methods in these tasks have been described below :



Compression

  First of all, a FolderBrowserDialog is displayed and the users selects the folder which is to be compressed. After this, the following tasks are performed :

  • The full directory path i.e. “C:\Users\Gaurav\ADB” is retrieved using the  GetFullPath() method
  • The container directory path i.e. “C:\Users\Gaurav” is retrieved using the GetDirectoryName() method. This is crucial because the resultant .zip file would have to be stored at the same location as the directory that was compressed.
  • The name of the directory to be compressed is retrieved by removing “C:\Users\Gaurav” from “C:\Users\Gaurav\ADB”. This is done by using the Replace() method of the string class.
  • Finally, the CreateFromDirectory() method is called and the task of compression is performed.

Decompression

  First of all, an OpenFileDialog is displayed and the user selects the .zip file which is to be decompressed. After this, the following tasks are performed :

  • The full path of the .zip file is retrieved using the GetFullPath() method.
  • The name of the .zip file without the extension is retrieved using the GetFileNameWithoutExtension() method. This is very important since the newly created directory should have the same name as the .zip file.
  • The container directory path is retrieved using the GetDirectoryName() method.
  • The full path of the resultant directory is generated by adding the name of the .zip file (without extension) to the container directory path.
  • Finally, the ExtractToDirectory() method is called and the task of decompression is performed.

Screenshots of File Zipper

  We suppose you have understood the above mentioned processes. To fully understand the process, have a look at the screenshots below :

File Zipper - Idle

File Zipper – Idle

Compression

File Zipper - Compression Step 1

File Zipper – Compression Step 1

File Zipper - Compression Step 2

File Zipper – Compression Step 2

Decompression

File Zipper - Decompression Step 1

File Zipper – Decompression Step 1

File Zipper - Decompression Step 2

File Zipper – Decompression Step 2



Download File Zipper

  We believe you like the concept of this program. You can download the source and/or the executable of File Zipper by clicking on the links below :

Click here to download the source code of File Zipper (99 KB)
Click here to download only the executable of File Zipper (16 KB). This program requires .NET Framework 4.5

  If you like this File Zipper program, help others find it by sharing it…

(Visited 228 times, 1 visits today)

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *