private byte[] Compression(byte[] data, CompressionMode mode) { DeflateStream zip = null; try { if (mode == CompressionMode.Compress)//压缩函数 { MemoryStream ms = new MemoryStream(); zip = new DeflateStream(ms, mode, true); zip.Write(data, 0, data.Length); zip.Close(); return ms.ToArray(); } else//解压函数 { MemoryStream ms = new MemoryStream(); ms.Write(data, 0, data.Length); ms.Flush(); ms.Position = 0; zip = new DeflateStream(ms, mode, true); MemoryStream os = new MemoryStream(); int SIZE = 1024*100; byte[] buf = new byte[SIZE]; int l = 0; do { l = zip.Read(buf, 0, SIZE); if (l == 0) l = zip.Read(buf, 0, SIZE); os.Write(buf, 0, l); } while (l != 0); zip.Close(); return os.ToArray(); } } catch { if (zip != null) zip.Close(); return null; } finally { if (zip != null) zip.Close(); } }