谁有GZipStream 类压缩和解压缩字符串的列子
压缩结果是Stream也可以的。
------解决方案--------------------顶一下.
public static byte[] CompressBytes(byte[] data)
{
ICSharpCode.SharpZipLib.Zip.Compression.Deflater f = new ICSharpCode.SharpZipLib.Zip.Compression.Deflater(ICSharpCode.SharpZipLib.Zip.Compression.Deflater.BEST_COMPRESSION);
f.SetInput(data);
f.Finish();
System.IO.MemoryStream o = new System.IO.MemoryStream(data.Length);
try
{
byte[] buf = new byte[1024];
while (!f.IsFinished)
{
int got = f.Deflate(buf);
o.Write(buf, 0, got);
}
}
finally
{
o.Close();
}
return o.ToArray();
}
public static byte[] DecompressBytes(byte[] data)
{
ICSharpCode.SharpZipLib.Zip.Compression.Inflater f = new ICSharpCode.SharpZipLib.Zip.Compression.Inflater();
f.SetInput(data);
System.IO.MemoryStream o = new System.IO.MemoryStream(data.Length);
try
{
byte[] buf = new byte[1024];
while (!f.IsFinished)
{
int got = f.Inflate(buf);
o.Write(buf, 0, got);
}
}
finally
{
o.Close();
}
return o.ToArray();
}
------解决方案--------------------msdn上有现成的例子,还来问什么。
------解决方案--------------------http://www.cnblogs.com/wengmj/archive/2007/04/27/437147.html
------解决方案--------------------http://blog.advnote.com/article.asp?id=49
public class strngZIP
{
public static string Zip(string tozipstr)
{
MemoryStream mStream = new MemoryStream();
GZipStream gStream = new GZipStream(mStream, CompressionMode.Compress);
BinaryWriter bw = new BinaryWriter(gStream);
bw.Write(Encoding.UTF8.GetBytes(tozipstr));
bw.Close();
gStream.Close();
string outs = Convert.ToBase64String(mStream.ToArray());
mStream.Close();
return outs;
}
public static string UnZip(string zipedstr)
{
byte[] data = Convert.FromBase64String(zipedstr);
MemoryStream mStream = new MemoryStream(data);
GZipStream gStream = new GZipStream(mStream, CompressionMode.Decompress);
StreamReader streamR = new StreamReader(gStream);
string outs = streamR.ReadToEnd();
mStream.Close();
gStream.Close();
streamR.Close();
return outs;
}
}