상세 컨텐츠

본문 제목

DataSet 압축

C#

by 탑~! 2009. 4. 3. 18:23

본문

닷넷2.0에서 추가된 ADO.NET의 여러 기능 중 DataSet을 바이너리로 Serialize되도록 지원하기 때문에 압축이 가능해졌다. DataSet으로 데이터를 교환할 때 압축된 데이터를 전달해 성능상 큰 효과를 가져다 준다.

    System.IO.Compression이라는 압축 네임스페이스가 닷넷 2.0에서는 새롭게 추가 되었다. 그럼 DataSet을 Serialize하고, 압축하는 메서드를 살펴 보도록 하자(<예제 1> 참조).

   

    데이터 압축 예제

    압축을 풀고 다시 Deserialize하는 메소드를 살펴보도록 하자. 압축을 했던 작업을 반대로 해주면 된다. 압축 해제시 DeflateStream 클래스를 이용하면 간단하게 해제할 수 있다(<예제 2> 참조).

    XML 웹서비스를 이용해서 DataSet을 리턴할 경우 XML로 Serialize되어서 데이터를 전달하게 된다. XML 특성상 DataSet 의 크기는 커져 버릴 수 밖에 없다. 대용량의 데이터를 DataSet으로 운반해야 한다면 DataSet을 압축해서 문자열 변환 후에 데이터를 넘겨주는 방법을 추천한다.

   

    <예제 1> DataSet 압축 메소드

   18

   19 using System.IO;

   20 using System.IO.Compression;

   21 using System.Runtime.Serialization.Formatters.Binary;

   22

   23 public byte[] CompressDataSet(DataSet ds)

   24 {

   25   //1. 데이터셋 Serialize

   26   ds.RemotingFormat = SerializationFormat.Binary;

   27   BinaryFormatter bf = new BinaryFormatter();

   28   MemoryStream ms = new MemoryStream();

   29   bf.Serialize(ms, ds);

   30   byte[] inbyt = ms.ToArray();

   31

   32   //2. 데이터 압축

   33   System.IO.MemoryStream objStream = new MemoryStream();

   34   System.IO.Compression.DeflateStream objZS = new System.IO.

   35   Compression.DeflateStream(objStream,System.IO.Compression.

   36   CompressionMode.Compress);

   37   objZS.Write(inbyt, 0, inbyt.Length);

   38   objZS.Flush();

   39   objZS.Close();

   40

   41   //3. 데이터 리턴

   42   return objStream.ToArray();

   43 }

   44

    <예제 2> DataSet 압축 해제 메소드

   46

   47 public DataSet DecompressDataSet(byte[] bytDs)

   48 {

   49   DataSetoutDs = new DataSet();

   50   MemoryStream inMs = new MemoryStream(bytDs);

   51   inMs.Seek(0, 0); //스트림으로 가져오기

   52

   53   //1. 압축객체 생성- 압축 풀기

   54   DeflateStream zipStream = new DeflateStream(inMs,

   55   CompressionMode.Decompress, true);

   56   byte[] outByt = ReadFullStream(zipStream);

   57   zipStream.Flush();

   58   zipStream.Close();

   59   MemoryStream outMs = new MemoryStream(outByt);

   60   outMs.Seek(0, 0); //2. 스트림으로 다시변환

   61   outDs.RemotingFormat = SerializationFormat.Binary;

   62

   63   //3. 데이터셋으로 Deserialize

   64   BinaryFormatter bf = new BinaryFormatter();

   65   outDs = (DataSet)bf.Deserialize(outMs, null);

   66   returnoutDs;

   67 }

   

   69 public byte[] ReadFullStream(Stream stream)

   70 {

   71   //스트림을 Byte 배열로 변환

   72   byte[] buffer = new byte[32768];

   73   using (MemoryStreamms = new MemoryStream())

   74   {

   75     while (true)

   76     {

   77       intread = stream.Read(buffer, 0, buffer.Length);

   78       if(read <= 0)

   79         returnms.ToArray();

   80       ms.Write(buffer, 0, read);

   81     }

   82   }

   83 }

   84

   85

    출처 : 마이크로소프트웨어 2006년 9월호 (C# 프로그래밍 테크닉)

 

'C#' 카테고리의 다른 글

HDD Serial  (0) 2009.05.25
CPU Serial  (0) 2009.05.25
SQL Injection  (0) 2009.04.03
String to Byte Array Conversion  (0) 2009.03.25
DotNetZip revs to v1.6 – now with Unicode support  (0) 2009.02.20

관련글 더보기