码迷,mamicode.com
首页 > 其他好文 > 详细

stream操作 z

时间:2015-05-06 09:10:15      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

常见并常用的stream一共有

文件流(FileStream),

内存流(MemoryStream),

压缩流(GZipStream),

加密流(CrypToStream),

网络流(NetworkStream);

1.文件流(读取文件流-输出文件流)FileStream

using(Streamstreamwrite=new FileStream(@"D:\BaiduYunDownload\45.avi",FileMode.OpenOrCreate))
{
using (Stream streamread = new FileStream(@"D:\BaiduYunDownload\xiawu3.avi", FileMode.Open))

byte[] ss=new byte[1024*1024*4];
int len;
while ((len = streamread.Read(ss, 0, ss.Length)) > 0)
{
streamwrite.Write(ss, 0, len);
Thread.Sleep(1000);
}
}
}

2.内存流(MemoryStream)

string strtxt="dasdfdsfsd";
byte[] bytetxt = Encoding.UTF8.GetBytes(strtxt);
Stream memstream = new MemoryStream();
memstream.Write(bytetxt, 0, bytetxt.Length);

3.压缩流(GZipStream),

压缩:

string s = "dfdfdf";
using (FileStream filestream = File.OpenWrite(@"c:\2.txt"))
{
using (GZipStream zipstream = new GZipStream(filestream, CompressionMode.Compress))
{
byte[] bytes = Encoding.UTF8.GetBytes(s);
zipstream.Write(bytes, 0, bytes.Length);
}
}

解压:

using (FileStream filestream = File.OpenRead(@"c:\2.txt"))
{
using (GZipStream zipstream = new GZipStream(filestream, CompressionMode.Decompress))
{
using (FileStream filestreamwrite = new FileStream(@"c:\3.txt", FileMode.OpenOrCreate))
{
byte[] bytes = new byte[1024 * 1024 * 4];
int length;
while ((length = zipstream.Read(bytes, 0, bytes.Length)) > 0)
{
filestreamwrite.Write(bytes, 0, length);
}
}
}
}

stream操作 z

标签:

原文地址:http://www.cnblogs.com/zeroone/p/4480855.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!