try {

  //---the ms is used for storing the compressed data---

  MemoryStream ms = new MemoryStream();

  Stream zipStream = null;

  switch (algo) {

  case 'Gzip':

   zipStream =

    new GZipStream(ms, CompressionMode.Compress, true);

   break;

  case 'Deflat':

   zipStream =

    new DeflateStream(ms, CompressionMode.Compress, true);

   break;

  default:

   return null;

  }

  //---compress the data stored in the data byte array---

  zipStream.Write(data, 0, data.Length);

  zipStream.Close();

  //---store the compressed data into a byte array---

  ms.Position = 0;

  byte[] c_data = new byte[ms.Length];

  //---read the content of the memory stream into the byte array---

  ms.Read(c_data, 0, (int)ms.Length);

  return c_data;

 } catch (Exception ex) {

  Console.WriteLine(ex.ToString());

  return null;

 }

}

Decompression

The following Decompress() function decompresses the data compressed by the Compress() function. The first parameter specifies the algorithm to use, while the byte array containing the compressed data is passed in as the second parameter, which is then copied into a MemoryStream object.

static byte[] Decompress(string algo, byte[] data) {

 try {

  //---copy the data (compressed) into ms---

  MemoryStream ms = new MemoryStream(data);

  Stream zipStream = null;

  //---decompressing using data stored in ms---

  switch (algo) {

  case 'Gzip':

   zipStream =

    new GZipStream(ms, CompressionMode.Decompress, true);

   break;

  case 'Deflat':

   zipStream =

    new DeflateStream(ms, CompressionMode.Decompress, true);

   break;

  default:

   return null;

  }

  //---used to store the de-compressed data---

  byte[] dc_data;

  //---the de-compressed data is stored in zipStream;

  // extract them out into a byte array---

  dc_data = RetrieveBytesFromStream(zipStream, data.Length);

  return dc_data;

 } catch (Exception ex) {

  Console.WriteLine(ex.ToString());

  return null;

 }

}

The compression classes then decompress the data stored in the memory stream and store the decompressed data into another Stream object. To obtain the decompressed data, you need to read the data from the Stream object. This is accomplished by the RetrieveBytesFromStream() function, which is defined next:

static byte[] RetrieveBytesFromStream(Stream stream, int bytesblock) {

 //---retrieve the bytes from a stream object---

 byte[] data = null;

 int totalCount = 0;

 try {

  while (true) {

   //---progressively increase the size of the data byte array---

   Array.Resize(ref data, totalCount + bytesblock);

   int bytesRead = stream.Read(data, totalCount, bytesblock);

   if (bytesRead == 0) {

    break;

   }

   totalCount += bytesRead;

  }

  //---make sure the byte array contains exactly the number

  // of bytes extracted---

  Array.Resize(ref data, totalCount);

  return data;

 } catch (Exception ex) {

  Console.WriteLine(ex.ToString());

  return null;

 }

}

The RetrieveBytesFromStream() function takes in two parameters — a Stream object and an integer — and returns a byte array containing the decompressed data. The integer parameter is used to determine how many bytes to read from the stream object into the byte array at a time. This is necessary because you do not know the exact size of the decompressed data in the stream object.

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату