字符串与byte数组的压缩解压

首先,到http://www.componentace.com/下载zlib.net,找到bin目录中的zlib.net.dll添加引用到项目中。

/// <summary>
/// 压缩字符串
/// </summary>
/// <param name="SourceString">需要被压缩的字符串</param>
/// <returns></returns>
public static string CompressString(string SourceString)
{
    byte[] byteSource = System.Text.Encoding.UTF8.GetBytes(SourceString);
    byte[] byteCompress = CompressBytes(byteSource);
    if (byteCompress != null)
    {
        return Convert.ToBase64String(byteCompress);
    }
    else
    {
        return null;
    }
}

/// <summary>
/// 解压字符串
/// </summary>
/// <param name="SourceString">需要被解压的字符串</param>
/// <returns></returns>
public static string DecompressString(string SourceString)
{
    byte[] byteSource = Convert.FromBase64String(SourceString);
    byte[] byteDecompress = DecompressBytes(byteSource);
    if (byteDecompress != null)
    {
        return System.Text.Encoding.UTF8.GetString(byteDecompress);
    }
    else
    {
        return null;
    }
}
/// <summary> 
/// 将 Stream 转成 byte[] 
/// </summary> 
public static byte[] StreamToBytes(Stream stream)
{
    byte[] bytes = new byte[stream.Length];
    stream.Read(bytes, 0, bytes.Length);

    // 设置当前流的位置为流的开始 
    stream.Seek(0, SeekOrigin.Begin);
    return bytes;
}

/// <summary> 
/// 将 byte[] 转成 Stream 
/// </summary> 
public static Stream BytesToStream(byte[] bytes)
{
    Stream stream = new MemoryStream(bytes);
    return stream;
}         
/// <summary>
/// 字节数组转换成十六进制字符串
/// </summary>
/// <param name="bytes">要转换的字节数组</param>
/// <returns></returns>
private static string ByteArrayToHexStr(byte[] byteArray)
{
  int capacity = byteArray.Length * 2;
  StringBuilder sb = new StringBuilder(capacity);

  if (byteArray != null)
  {
  for (int i = 0; i < byteArray.Length; i++)
  {
      sb.Append(byteArray[i].ToString("X2")+" ");
  }
  }
  return sb.ToString();
}
/// <summary>
/// 压缩byte数组数据
/// </summary>
/// <param name="SourceByte">需要被压缩的Byte数组数据</param>
/// <returns></returns>
public static byte[] CompressBytes(byte[] SourceByte)
{
  try
  {
  MemoryStream stmInput = new MemoryStream(SourceByte);
  Stream stmOutPut = CompressStream(stmInput);
  byte[] bytOutPut = new byte[stmOutPut.Length];
  stmOutPut.Position = 0;
  stmOutPut.Read(bytOutPut, 0, bytOutPut.Length);
  return bytOutPut;
  }
  catch
  {
  return null;
  }
}

/// <summary>
/// 解压byte数组数据
/// </summary>
/// <param name="SourceByte">需要被解压的byte数组数据</param>
/// <returns></returns>
public static byte[] DecompressBytes(byte[] SourceByte)
{
  try
  {
  MemoryStream stmInput = new MemoryStream(SourceByte);
  Stream stmOutPut = DecompressStream(stmInput);
  byte[] bytOutPut = new byte[stmOutPut.Length];
  stmOutPut.Position = 0;
  stmOutPut.Read(bytOutPut, 0, bytOutPut.Length);
  return bytOutPut;
  }
  catch
  {
  return null;
  }
}


/// <summary>
/// 十六进制字符串转换成字节数组 
/// </summary>
/// <param name="hexString">要转换的字符串</param>
/// <returns></returns>
private static byte[] HexStrToByteArray(string hexString)
{
  hexString = hexString.Replace(" ", "");
  if ((hexString.Length % 2) != 0)
  throw new ArgumentException("十六进制字符串长度不对");
  byte[] buffer = new byte[hexString.Length / 2];
  for (int i = 0; i < buffer.Length; i++)
  {
  buffer[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 0x10);
  }
  return buffer;
}
static void Main(string[] args)
{
    string originStr = "测试字符串";
    string compressedStr = CompressString(originStr);
    string decompressedStr = DecompressString(compressedStr);
    Console.WriteLine("原始字符串:"+originStr);
    Console.WriteLine("压缩后字符串:"+compressedStr);
    Console.WriteLine("解压后字符串:" + decompressedStr);
    //string[] tempStrArray={"23","00", "7b" ,"0a", "20", "20", "20", "22", "6f", "70" ,"22", "20", "3a", "20", "31", "30", "38", "2c", "0a", "20", "20", "20", "22", "72", "65", "74", "22", "20", "3a", "20","30", "0a", "7d", "0a", "00"};
    //byte[] origionByteArr=new byte[tempStrArray.Length];
    //int i = 0;
    //foreach (string str in tempStrArray) {
    //    origionByteArr[i] = Convert.ToByte(str, 16);
    //    i++;
    //}
    string originString = "23 00 7b 0a 20 20 20 22 6f 70 22 20 3a 20 31 30 38 2c 0a 20 20 20 22 72 65 74 22 20 3a 20 30 0a 7d 0a 00";
    Console.WriteLine("原始16进制字符串:");
    Console.WriteLine(originString);
    byte[] origionByteArr = HexStrToByteArray(originString);
    Console.WriteLine("原始byte数组:");
    foreach (byte b in origionByteArr)
    {
        Console.Write(b.ToString() + " ");
    }
    Console.WriteLine("\n");
    byte[] compressedByteArr = CompressBytes(origionByteArr);
    Console.WriteLine("压缩后16进制字符串:");
    Console.Write(ByteArrayToHexStr(compressedByteArr));
    Console.WriteLine("\n");
    Console.WriteLine("压缩后byte数组:");
    foreach (byte b in compressedByteArr)
    {
    Console.Write(b.ToString()+" ");
    }
    Console.WriteLine("\n");
    byte[] decompressedByteArr = DecompressBytes(compressedByteArr);
    Console.WriteLine("解压后16进制字符串:");
    Console.WriteLine(ByteArrayToHexStr(decompressedByteArr));
    Console.WriteLine("解压后byte数组:");
    foreach (byte b in decompressedByteArr)
    {
    Console.Write(b.ToString()+" ");
    }
    Console.ReadLine();
}

 

 

posted @ 2013-01-27 14:39  yayadoudou  阅读(976)  评论(0编辑  收藏  举报