C#利用SharpZipLib库压缩解压字符串

在MemoryStream中压缩与解压

using ICSharpCode.SharpZipLib.Core;
using ICSharpCode.SharpZipLib.Zip;
using System;
using System.IO;
using System.Text;

namespace Common
{
    public class ZipHelper
    {
       
        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[] BytesCompass(byte[] bytes,string pwd="")
        {
            MemoryStream memStreamIn = new MemoryStream(bytes);
            MemoryStream outputMemStream = new MemoryStream();
            using (ZipOutputStream zipStream = new ZipOutputStream(outputMemStream))
            {

                if (pwd != null)
                {
                    zipStream.Password = pwd;
                }
                zipStream.SetLevel(5); //0-9, 9 being the highest level of compression

                ZipEntry newEntry = new ZipEntry("tmp.zip");
                newEntry.DateTime = DateTime.Now;

                zipStream.PutNextEntry(newEntry);

                StreamUtils.Copy(memStreamIn, zipStream, new byte[4096]);
                zipStream.CloseEntry();

                zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
                zipStream.Close();          // Must finish the ZipOutputStream before using outputMemStream.

                outputMemStream.Position = 0;
                return outputMemStream.ToArray();
            }

        }
        /// <summary>
        /// 解压
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static byte[] BytesDeCompass(byte[] bytes ,string pwd="")
        {
            MemoryStream ms = new MemoryStream(bytes);

            
            ZipInputStream zipInputStream = new ZipInputStream(ms);
            if (pwd != null)
            {
                zipInputStream.Password = pwd;
            }
            ZipEntry zipEntry = zipInputStream.GetNextEntry();
            if (zipEntry != null)
            {

                String entryFileName = zipEntry.Name;

                byte[] buffer = new byte[4096];     // 4K is optimum
                using (MemoryStream streamWriter = new MemoryStream())
                {
                    StreamUtils.Copy(zipInputStream, streamWriter, buffer);
                    return streamWriter.ToArray();
                }
            }
            return null;
        }
        /// <summary>
        /// 压缩字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string StringCompass(string str,string pwd="")
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            byte[] zipbs = BytesCompass(bytes,pwd);
            return (string)(Convert.ToBase64String(zipbs));
        }
        /// <summary>
        /// 解压字符串
        /// </summary>
        /// <param name="zipstr"></param>
        /// <returns></returns>
        public static string StringDeCompass(string zipstr, string pwd = "")
        {
            byte[] zipbs = BytesDeCompass(Convert.FromBase64String(zipstr),pwd);
            return Encoding.UTF8.GetString(zipbs);
        }

    }
}