This topic covers how to compress CSV file using C#. It discusses the environment setup, the stepwise algorithm, and a runnable sample code for CSV file compression using C#. Moreover, you do not need any extra file compression application installed in your environment to use this feature on your end.
Steps to Compress CSV using C#
- Prepare the system to work with Aspose.ZIP for compressing the CSV file
- Initiate an object of the Archive class
- Pass the input file name as well as the ZIP entry
- Export the output ZIP file by invoking the Save method
The steps above outline how to create a CSV file size reducer using C#. As a first step, the compression process is commenced by parsing the source comma-separated file. Subsequently, create different entries while mentioning the source file paths and file names as well as the target file names in the ZIP archive. Finally, proceed to export the generated ZIP directory to conclude the process.
Code to Compress CSV File using C#
using Aspose.Zip; | |
License lic = new License(); | |
lic.SetLicense("license.lic"); | |
// Create Archive class object | |
var arch = new Aspose.Zip.Archive(); | |
// Specify file names and create entry | |
arch.CreateEntry("input.csv", "sample.csv"); | |
// Save compressed ZIP archive containing CSV data | |
arch.Save("csv_archive.zip"); |
This code snippet demonstrates the feature to compress CSV using C#. However, it is a bare-minimum version that can be improvised further to change the number of files and data security, including encryption or password protection. Moreover, many custom properties, like setting the encodings or comments, can also be modified to meet your requirements.
This tutorial has explained all the relevant details to compress a CSV file using C#. You can also extract the data from RAR archives, as explained in the article on Extract RAR Files using C#.