■ GZipStream 클래스를 사용해 문자열을 압축/해제하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
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 } } |