How to Compress Excel Files to ZIP in C#

How to Compress Excel Files to ZIP in C#

The ZIP archives can contain multiple files, commonly used to reduce the size of files. At the same time, each file compresses individually, which helps to extract them and add new ones without compressing or decompressing the entire Zip archive. Nowadays, the use of Excel (XLSX or XLS) spreadsheets has increased in daily business activities. Sometimes, Excel files can get quite large, but we can reduce their size by compressing them into a ZIP archive. In this article, you will learn how to compress Excel files to a ZIP archive in C# without writing complex code. Apart from it, you will see how to get the report of compression progress while zipping the large excel files.

The following points will be covered in this article:

C# .NET API to compress Excel files to a ZIP archive

To compress the Excel files into a ZIP archive, we will use Aspose.ZIP for .NET. The API consists of numerous archiving features. Therefore, you can download the DLL or install it using NuGet.

Install-Package Aspose.ZIP

Add XLS or XLSX files to ZIP in C#

Suppose you have multiple Excel (XLSX or XLS) files for compressing into a ZIP archive. You can choose one of the following ways to do the job.

C# compress Excel files to ZIP using FileStream

In this way, we will use the FileStream class to add files to the ZIP archive by calling Archive.CreateEntry(String, FileStream) function.

The following are the steps to compress Excel spreadsheets to ZIP using FileStream:

  1. First, create an object of FileStream class for the output ZIP archive.
  2. Secondly, create objects of FileStream for the Excel files to be compressed.
  3. Then, initialize Archive class and pass it an instance of ArchiveEntrySettings class.
  4. After that, add FileStream objects created in step 2 using Archive.CreateEntry method.
  5. Finally, call Archive.Save method and use the objects of FileStream (created in step 1) and instance of ArchiveSaveOptions as parameters.

The following code shows how to compress Excel files to ZIP using FileStream.

C# compress XLSX or XLS files into ZIP using FileInfo

Alternatively, we can use the FileInfo class to add files to the ZIP archive. In this case, the files will be loaded using the FileInfo class and added to the ZIP archive by calling Archive.CreateEntry(String, FileInfo) method.

Please follow the steps mentioned below:

  1. Create an object of FileStream class for the output ZIP archive.
  2. Create objects of FileInfo for the Excel files to be compressed.
  3. Initialize Archive class and pass it an instance of ArchiveEntrySettings class.
  4. Add FileStream objects created in step 2 using Archive.CreateEntry method.
  5. To compress the files, call Archive.Save method and use the objects of FileStream (created in step 1) and instance of ArchiveSaveOptions as parameters.

The following code shows how to Compress XLSX or XLS files into ZIP using FileInfo.

Compress Excel spreadsheets to ZIP using the file path

The most simple way is to add an Excel file path directly to Archive.CreateEntry method:

The following are the steps to compress Excel spreadsheets to ZIP using a file path:

The following code demonstrates how to Compress Excel spreadsheet files to ZIP using a file path.

Get the report of compression progress in C#

In the case of large Excel files, the compression process may take a long time to complete. To deal with it, the C# ZIP compression library provides [CompressionProgressed] event to post updates continuously.

The following are the steps to get the report of compression progress in C#:

The following code sample shows how to get the report of compression progress in C#:

Get a Free License

You can get a free temporary license to test the API without evaluation limitations.

Conclusion

In this article, you have learned how to compress Excel files to a ZIP archive in C# using a C# ZIP compression library. Aside from it, you got sufficient knowledge about getting compression progress report while zipping large excel files. You may visit the documentation to explore other features of C# ZIP compression API.

Ask a question

You can let us know your questions or queries on our forum.

See Also