The Zip library provides classes that are designed to compress uploaded files into your database and thereby reduce the saved data size. You can also decompress zip files and restore it to its original state with C1.Zip.
In this topic, you will learn how to compress and decompress files using C1.Zip library.
The snapshot depicts an application that uses two button controls and a list view control. The button control events are used to show the compress and decompression of files, while the list view displays the content of the zip file.
Note that with the Zip library, a user can compress and decompress different types of files, be it in image file format (jpg, png, gif, tif, or bmp), .docx file format, .xlsx,file format, .txt file format or .pdf file formats.
The C1.Zip library provides the Create method in C1ZipFile class, which creates an empty zip file on the disk. This method uses the fileName parameter, which is the name of the zip file to create, including the path. If a zip file by the same name already exists, the Create method deletes the old zip files and adds the new one.
Let's say, we want to compress files at a location to this zip file. For this, the user will need to get the location of application directory by using the ExecutablePath property of the Application class using the System.Windows.Forms namespace. All the files in the specified directory are populated in the zip file using the GetFile property of the Directory class using System.IO namespace and the Add method of C1ZipEntryCollection class that uses the fileName parameter, which is nothing but the name of the file to add to the zip file. The user can also call other overload Add methods depending upon the parameters and data types required.
The UpdateDisplay utility function is called to display the contents of the zip file in the List View control.
The Zip library provides the Extract method in C1ZipEntryCollection class to extract files from the current zip file, which uses the fileName parameter, which denotes the names of the entry to be extracted. The class also provides many other overloads of the Extract method.