//1.创建读取文本文件的流
using (FileStream fsRead = File.OpenRead(@"D:YY.TXT"))
{
//2.创建写文件流
using (FileStream fsWrite = File.OpenWrite(@"D:XX.rar"))
{
//3.创建压缩流
using (GZipStream zipStream = new GZipStream(fsWrite, CompressionMode.Compress))
{
byte[] byts = new byte[1024];
int len = 0;
//4.通过读取文件流读取数据
while ((len = fsRead.Read(byts, 0, byts.Length)) > 0)
{
//通过压缩流写入数据
zipStream.Write(byts, 0, len);
}
}
}
}
---------------------------
//1.创建读取流
using (FileStream fsRead = File.OpenRead(@"D:XX.rar"))
{
//2.创建压缩流,把读取流作为参数,
using (GZipStream zip = new GZipStream(fsRead, CompressionMode.Decompress))
{
//创建写入流
using (FileStream fsWrite = File.OpenWrite(@"D:Z.TXT"))
{
byte[] byts = new byte[1024];
int len = 0;//用于表示真是接受到是字节个数
//通过压缩流读取数据
while ((len = zip.Read(byts, 0, byts.Length)) > 0)
{
//MessageBox.Show(Encoding.UTF8.GetString(byts.Take(len).ToArray()));
//通过文件流写入文件
fsWrite.Write(byts, 0, len);//读取的长度为len,这样不会造成数据的错误
}
}
}
}