您的位置:首页 > 其它

GZipStream压缩解压字符串

2009-08-24 07:17 323 查看
///
使用GZipStream压缩数据

    
public

byte
[] CompressionData(
byte
[] input)

    {

           
byte
[] temp
=

null
;

           
try

            {

               
using
(MemoryStream ms
=

new
MemoryStream())

                {

                   
using
(GZipStream compressStream
=

new
GZipStream(ms, CompressionMode.Compress,
true
))

                    {

                       
//
写入目标流

                        compressStream.Write(input,
0
, input.Length);

                    }

                    temp
=
ms.ToArray();

                }

            }

           
catch
(Exception ex)

            {

               
throw
ex;

            }

           
return
temp;

        }

       
///
解压缩数据

       
public

byte
[] DecompressionData(
byte
[] input)

        {

           
byte
[] temp
=

null
;

           
try

            {

               
using
(MemoryStream baseData
=

new
MemoryStream())

                {

                    MemoryStream rmstemp
=

new
MemoryStream(input);

                   
using
(GZipStream DecompressString
=

new
GZipStream(rmstemp, CompressionMode.Decompress))

                    {

                       
byte
[] buff
=

new

byte
[
4096
];

                       
int
n;

                       
while
((n
=
DecompressString.Read(buff,
0
, buff.Length))
!=

0
)

                        {

                            baseData.Write(buff,
0
, n);

                        }

                    }

                    rmstemp.Dispose();

                    rmstemp.Close();

                    temp
=
baseData.ToArray();

                }

            }

           
catch
(Exception ex)

            {

                temp
=

null
;

            }

           
return
temp;

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  byte exception input null