码迷,mamicode.com
首页 > Windows程序 > 详细

zip格式压缩、解压缩(C#)

时间:2015-03-16 16:12:37      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

压缩方法

技术分享
        #region 压缩

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="bytes">未被压缩的字节数据</param>
        /// <returns></returns>
        public static byte[] Compress(byte[] bytes)
        {
            if (bytes == null)
                return null;

            using (MemoryStream oStream = new MemoryStream())   //压缩
            {
                GZipStream zipStream = new GZipStream(oStream, CompressionMode.Compress);
                zipStream.Write(bytes, 0, bytes.Length);
                zipStream.Flush();
                zipStream.Close();

                return oStream.ToArray();
            }
        }

        /// <summary>
        /// 压缩
        /// </summary>
        /// <param name="dataOriginal">未被压缩的数据</param>
        /// <returns></returns>
        public static byte[] Compress(object dataOriginal)
        {
            if (dataOriginal == null)
                return null;

            byte[] bytes = null;
            using (MemoryStream mStream = new MemoryStream())     //序列化成二进制数组
            {
                BinaryFormatter bFormatter = new BinaryFormatter();
                bFormatter.Serialize(mStream, dataOriginal);
                bytes = mStream.ToArray();
            }

            return Compress(bytes);
        }

        /// <summary>
        /// 压缩字符串
        /// </summary>
        /// <param name="unZipData">未被压缩的数据</param>
        /// <returns></returns>
        public static string Compress(string unZipData)
        {
            if (string.IsNullOrEmpty(unZipData))
                return null;
            var bytes = Compress(unZipData as object);
            if (bytes == null)
                return null;

            return Convert.ToBase64String(bytes);   //一定要Base64,否则会出错
        }

        #endregion
View Code

 

解压方法

技术分享
        #region 解压缩

        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static object Decompress(Stream stream)
        {
            stream.Seek(0, SeekOrigin.Begin);
            GZipStream unZipStream = new GZipStream(stream, CompressionMode.Decompress, true);
            object dsResult = null;
            BinaryFormatter bFormatter = new BinaryFormatter();
            dsResult = bFormatter.Deserialize(unZipStream) as object;
            unZipStream.Close();

            return dsResult;
        }

        /// <summary>
        /// 解压缩
        /// </summary>
        /// <param name="bytes">压缩后的字节数组</param>
        /// <returns></returns>
        public static object Decompress(byte[] bytes)
        {
            if (bytes == null) 
                return null;
            using (MemoryStream mStream = new MemoryStream(bytes))
            {
                return Decompress(mStream);
            }
            
        }

        /// <summary>
        /// 解压缩字符串
        /// </summary>
        /// <param name="zipData">被压缩的字符串</param>
        /// <returns></returns>
        public static string Decompress(string zipData)
        {
            if (string.IsNullOrEmpty(zipData))
                return null;
            var bytes = Convert.FromBase64String(zipData);   //一定要Base64,否则会出错

            return Decompress(bytes) as string;
        }

        #endregion
View Code

 

字符串压缩、解压缩 字节数组和字符串的相互装换要用Base64编码!

zip格式压缩、解压缩(C#)

标签:

原文地址:http://www.cnblogs.com/liujinwu-11/p/4341812.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!