1。用GzipStream进行压缩文件存储,要小很多.
string s="你好啊你好啊你好啊你好啊你好啊你好啊你好啊";
for(int i=0;i<20;i++)
{
s+=s;
}
using(FileStream fs=File.OpenWrite(@"F:\lili1.txt"))
{
using(Stream zipstream=new GzipStream(fs,CompressionMode.compress))//指定为压缩模式
{
byte[] bytes=Encoding.UTF8.GetBytes(s);//把字符串转成byte数组
zipstream.write(bytes,0,bytes.length);
}
}
2.用GzipStream进行解压
using (FileStream fs = File.OpenRead(@"F:\lili1.txt"))//压缩存储的文件
{
using (FileStream outstream = File.OpenWrite(@"F:\lili2.txt"))//解压后存储的文件
{
using (Stream intputstream = new GZipStream(fs, CompressionMode.Decompress))
{
byte[] bytes=new byte[1024*1024*4];
int readbytes;
while((readbytes=intputstream.Read(bytes,0,bytes.Length))>0)
{
outstream.Write(bytes,0,readbytes);
}
}
}
}