这两个函数有错吗?

来源:互联网 发布:焊锡烟雾净化器淘宝 编辑:程序博客网 时间:2024/04/26 07:46

        public static string CompressString(string input)
        {
            if (input.Length == 0)
            {
                return string.Empty;
            }
            string str = string.Empty;
            MemoryStream stream = null;
            GZipStream stream2 = null;
            try
            {
                byte[] bytes = encode.GetBytes(input);
                stream = new MemoryStream();
                using (stream2 = new GZipStream(stream, CompressionMode.Compress, true))
                {
                    stream2.Write(bytes, 0, bytes.Length);
                }
                str = Convert.ToBase64String(stream.ToArray());
            }
            catch
            {
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
                if (stream2 != null)
                {
                    stream2.Dispose();
                }
            }
            return str;
        }

 

       public static string DecompressString(string base64String)
        {
            if (base64String.Length == 0)
            {
                return string.Empty;
            }
            string str = string.Empty;
            try
            {
                byte[] buffer = Convert.FromBase64String(base64String);
                int count = BitConverter.ToInt32(new byte[] { buffer[buffer.Length - 4], buffer[buffer.Length - 3], buffer[buffer.Length - 2], buffer[buffer.Length - 1] }, 0);
                using (MemoryStream stream = new MemoryStream(buffer, 0, buffer.Length))
                {
                    using (GZipStream stream2 = new GZipStream(stream, CompressionMode.Decompress))
                    {
                        byte[] buffer3 = new byte[count];
                        stream2.Read(buffer3, 0, count);
                        return encode.GetString(buffer3);
                    }
                }
            }
            catch
            {
            }
            return str;
        }

原创粉丝点击