Articles in this section
Category / Section

How to compress and decompress a string in C#

2 mins read

This article explains how to compress and decompress a string using the Syncfusion Compression Library. To achieve this, Syncfusion.XlsIO.Net.Core NuGet package is required, and this package is available in 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 & manipulating the spreadsheets without any dependency of Microsoft Office COM libraries & 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

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from 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 trail message.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied