첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

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
    }
}

 

728x90

 

▶ 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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요