첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ 문자열 압축/해제하기

----------------------------------------------------------------------------------------------------


TestProject.zip


Program.cs

 

 

using System;

using System.Text;

 

namespace TestProject

{

    /// <summary>

    /// 프로그램

    /// </summary>

    class Program

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 프로그램 시작하기 - Main()

 

        /// <summary>

        /// 프로그램 시작하기

        /// </summary>

        private static void Main()

        {

            StringBuilder stringBuilder = new StringBuilder();

            

            for(int i = 0; i < 1000; i++)

            {

                stringBuilder.AppendLine("abcdefghijklmnopqrstuvwxyz");

            }

 

            string source = stringBuilder.ToString();

 

            Console.WriteLine("소스 문자열 : " + source.Length.ToString());

 

            string target = StringCompressionHelper.Compress(stringBuilder.ToString());

 

            Console.WriteLine("압축 문자열 : " + target.Length.ToString());

 

            string restore = StringCompressionHelper.Decompress(target);

 

            Console.WriteLine("복원 문자열 : " + restore.Length.ToString());

        }

 

        #endregion

    }

}

 

 

StringCompressionHelper.cs

 

 

using System;

using System.IO;

using System.IO.Compression;

using System.Text;

 

namespace TestProject

{

    /// <summary>

    /// 문자열 압축 헬퍼

    /// </summary>

    public static class StringCompressionHelper

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 압축하기 - Compress(source)

 

        /// <summary>

        /// 압축하기

        /// </summary>

        /// <param name="source">소스 문자열</param>

        /// <returns>압축 문자열</returns>

        public static string Compress(string source)

        {

            byte[] sourceArray = Encoding.UTF8.GetBytes(source);

 

            MemoryStream memoryStream = new MemoryStream();

 

            using(GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Compress, true))

            {

                gZipStream.Write(sourceArray, 0, sourceArray.Length);

            }

 

            memoryStream.Position = 0;

 

            byte[] temporaryArray = new byte[memoryStream.Length];

 

            memoryStream.Read(temporaryArray, 0, temporaryArray.Length);

 

            byte[] targetArray = new byte[temporaryArray.Length + 4];

 

            Buffer.BlockCopy(temporaryArray, 0, targetArray, 4, temporaryArray.Length);

 

            Buffer.BlockCopy(BitConverter.GetBytes(sourceArray.Length), 0, targetArray, 0, 4);

 

            return Convert.ToBase64String(targetArray);

        }

 

        #endregion

        #region 압축 해제하기 - Decompress(source)

 

        /// <summary>

        /// 압축 해제하기

        /// </summary>

        /// <param name="source">소스 문자열</param>

        /// <returns>압축 해제 문자열</returns>

        public static string Decompress(string source)

        {

            byte[] sourceArray = Convert.FromBase64String(source);

 

            using(MemoryStream memoryStream = new MemoryStream())

            {

                int dataLength = BitConverter.ToInt32(sourceArray, 0);

 

                memoryStream.Write(sourceArray, 4, sourceArray.Length - 4);

 

                byte[] targetArray = new byte[dataLength];

 

                memoryStream.Position = 0;

 

                using(GZipStream gZipStream = new GZipStream(memoryStream, CompressionMode.Decompress))

                {

                    gZipStream.Read(targetArray, 0, targetArray.Length);

                }

 

                return Encoding.UTF8.GetString(targetArray);

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요