How to compress and decompress a string C# in ASP.NET Core XlsIO?
This article explains how to compress and decompress a string using the Syncfusion® Compression Library. To achieve this, the Syncfusion.XlsIO.Net.Core NuGet package is required, and this package is available on nuget.org.
The complete code snippet to compress and decompress a string is given below.
C#
using Syncfusion.Compression; using Syncfusion.Compression.Zip; using System; using System.IO; namespace ConsoleSample { class Program { static void Main(string[] args) { string text = "Essential XlsIO is a native .NET class library that can be used to create and modify Microsoft Excel files by using C#, VB.NET, and managed C++ code. It is a non-UI component that provides a full-fledged object model that facilitates accessing and manipulating the spreadsheets without any dependency on Microsoft Office COM libraries and Microsoft Office." + "The library can be used in Windows Forms, WPF, UWP, ASP.NET Web Forms, ASP.NET MVC, ASP.NET Core, Xamarin and Blazor applications."; MemoryStream stream = new MemoryStream(); StreamWriter sw = new StreamWriter(stream); sw.WriteLine(text); sw.Flush(); MemoryStream compressed = new MemoryStream(); CompressedStreamWriter compresser = new CompressedStreamWriter(compressed, false, CompressionLevel.Normal, false); stream.Position = 0; byte[] compressedData = new byte[Constants.BufferSize]; int writeBytes = 0; while ((writeBytes = stream.Read(compressedData, 0, Constants.BufferSize)) > 0) { compresser.Write(compressedData, 0, writeBytes, true); } compressed.Position = 0; CompressedStreamReader decompresser = new CompressedStreamReader(compressed); MemoryStream decompressed = new MemoryStream(); byte[] decompressedBuffer = new byte[Constants.BufferSize]; int iReadBytes = 0; while ((iReadBytes = decompresser.Read(decompressedBuffer, 0, Constants.BufferSize)) > 0) { decompressed.Write(decompressedBuffer, 0, iReadBytes); } decompressed.Position = 0; StreamReader reader = new StreamReader(decompressed); Console.WriteLine("Decompressed Text: " + reader.ReadToEnd()); Console.ReadLine(); } } }
A complete working sample can be downloaded here Compress and Decompress a String.zip
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and, protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.
Conclusion
I hope you enjoyed learning about how to compress and decompress a string C# in ASP.NET Core XlsIO.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!