Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# compress string

using System;
using System.IO;
using System.IO.Compression;
using System.Text;

namespace CompressString
{
    internal static class StringCompressor
    {
        /// <summary>
        /// Compresses the string.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <returns></returns>
        public static string CompressString(string text)
        {
            byte[] buffer = Encoding.UTF8.GetBytes(text);
            var memoryStream = new MemoryStream();
            using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))
            {
                gZipStream.Write(buffer, 0, buffer.Length);
            }

            memoryStream.Position = 0;

            var compressedData = new byte[memoryStream.Length];
            memoryStream.Read(compressedData, 0, compressedData.Length);

            var gZipBuffer = new byte[compressedData.Length + 4];
            Buffer.BlockCopy(compressedData, 0, gZipBuffer, 4, compressedData.Length);
            Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gZipBuffer, 0, 4);
            return Convert.ToBase64String(gZipBuffer);
        }

        /// <summary>
        /// Decompresses the string.
        /// </summary>
        /// <param name="compressedText">The compressed text.</param>
        /// <returns></returns>
        public static string DecompressString(string compressedText)
        {
            byte[] gZipBuffer = Convert.FromBase64String(compressedText);
            using (var memoryStream = new MemoryStream())
            {
                int dataLength = BitConverter.ToInt32(gZipBuffer, 0);
                memoryStream.Write(gZipBuffer, 4, gZipBuffer.Length - 4);

                var buffer = new byte[dataLength];

                memoryStream.Position = 0;
                using (var gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))
                {
                    gZipStream.Read(buffer, 0, buffer.Length);
                }

                return Encoding.UTF8.GetString(buffer);
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: cast int to enum type c# 
Csharp :: how to say hello world in c# 
Csharp :: get current playing animation of animator unity 
Csharp :: return json from controller c# 
Csharp :: onkeypressed unity 
Csharp :: c# remove duplicates from datatable 
Csharp :: bytes to httppostedfilebase c# 
Csharp :: create list c# 
Csharp :: c# remove double quotes from string 
Csharp :: xamarin hide back button 
Csharp :: c# shuffle 
Csharp :: c# bytes to image 
Csharp :: c# string replace comma with newline 
Csharp :: c# list string return concatenate 
Csharp :: c# merging two arrays 
Csharp :: difference two list c# 
Csharp :: What is the difference between String and string in C#? 
Csharp :: vscode not showing errors c# 
Csharp :: c# for loop 
Csharp :: press key run code unity c# 
Csharp :: switch case in c# with multiple values 
Csharp :: c# check lenght 
Csharp :: hello world in unity c# 
Csharp :: textbox in xamarin forms 
Csharp :: regex c# 
Csharp :: c# serial port 
Csharp :: unity print vs debug log 
Csharp :: change color of object unity 
Csharp :: c# empty list 
Csharp :: change column name in datatable C# 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =