C# FileStream复制大文件
2016-07-01 14:40
387 查看
FileStream缓冲读取和写入可以提高性能。每次复制文件的一小段,以节省总内存开销。当然,本机复制也可以采用.NET内部的System.IO.File.Copy方法。
FileStream读取文件的时候,是先讲流放入内存,经Flash()方法后将内存中(缓冲中)的数据写入文件。如果文件非常大,势必消耗性能。特封装在FileHelper中以备不时之需。强制类型转换,如果文件很大,比如4G,就会出现溢出的情况,复制的结果字节丢失严重,导致复制文件和源文件大小不一样。这里修改的代码如下:
public static class FileHelper
{
/// <summary>
/// 复制大文件
/// </summary>
/// <param name="fromPath">源文件的路径</param>
/// <param name="toPath">文件保存的路径</param>
/// <param name="eachReadLength">每次读取的长度</param>
/// <returns>是否复制成功</returns>
public static bool CopyFile(string fromPath, string toPath, int eachReadLength)
{
//将源文件 读取成文件流
FileStream fromFile = new FileStream(fromPath, FileMode.Open, FileAccess.Read);
//已追加的方式 写入文件流
FileStream toFile = new FileStream(toPath, FileMode.Append, FileAccess.Write);
//实际读取的文件长度
int toCopyLength = 0;
//如果每次读取的长度小于 源文件的长度 分段读取
if (eachReadLength < fromFile.Length)
{
byte[] buffer = new byte[eachReadLength];
long copied = 0;
while (copied <= fromFile.Length - eachReadLength)
{
toCopyLength = fromFile.Read(buffer, 0, eachReadLength);
fromFile.Flush();
toFile.Write(buffer, 0, eachReadLength);
toFile.Flush();
//流的当前位置
toFile.Position = fromFile.Position;
copied += toCopyLength;
}
int left = (int)(fromFile.Length - copied);
toCopyLength = fromFile.Read(buffer, 0, left);
fromFile.Flush();
toFile.Write(buffer, 0, left);
toFile.Flush();
}
else
{
//如果每次拷贝的文件长度大于源文件的长度 则将实际文件长度直接拷贝
byte[] buffer = new byte[fromFile.Length];
fromFile.Read(buffer, 0, buffer.Length);
fromFile.Flush();
toFile.Write(buffer, 0, buffer.Length);
toFile.Flush();
}
fromFile.Close();
toFile.Close();
return true;
}
}测试代码:
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
if (FileHelper.CopyFile(@"D:\安装文件\新建文件夹\SQLSVRENT_2008R2_CHS.iso", @"F:\SQLSVRENT_2008R2_CHS.iso", 1024 * 1024 * 5))
{
watch.Stop();
Console.WriteLine("拷贝完成,耗时:" + watch.Elapsed.Seconds + "秒");
}
Console.Read();
}
}
FileStream读取文件的时候,是先讲流放入内存,经Flash()方法后将内存中(缓冲中)的数据写入文件。如果文件非常大,势必消耗性能。特封装在FileHelper中以备不时之需。强制类型转换,如果文件很大,比如4G,就会出现溢出的情况,复制的结果字节丢失严重,导致复制文件和源文件大小不一样。这里修改的代码如下:
public static class FileHelper
{
/// <summary>
/// 复制大文件
/// </summary>
/// <param name="fromPath">源文件的路径</param>
/// <param name="toPath">文件保存的路径</param>
/// <param name="eachReadLength">每次读取的长度</param>
/// <returns>是否复制成功</returns>
public static bool CopyFile(string fromPath, string toPath, int eachReadLength)
{
//将源文件 读取成文件流
FileStream fromFile = new FileStream(fromPath, FileMode.Open, FileAccess.Read);
//已追加的方式 写入文件流
FileStream toFile = new FileStream(toPath, FileMode.Append, FileAccess.Write);
//实际读取的文件长度
int toCopyLength = 0;
//如果每次读取的长度小于 源文件的长度 分段读取
if (eachReadLength < fromFile.Length)
{
byte[] buffer = new byte[eachReadLength];
long copied = 0;
while (copied <= fromFile.Length - eachReadLength)
{
toCopyLength = fromFile.Read(buffer, 0, eachReadLength);
fromFile.Flush();
toFile.Write(buffer, 0, eachReadLength);
toFile.Flush();
//流的当前位置
toFile.Position = fromFile.Position;
copied += toCopyLength;
}
int left = (int)(fromFile.Length - copied);
toCopyLength = fromFile.Read(buffer, 0, left);
fromFile.Flush();
toFile.Write(buffer, 0, left);
toFile.Flush();
}
else
{
//如果每次拷贝的文件长度大于源文件的长度 则将实际文件长度直接拷贝
byte[] buffer = new byte[fromFile.Length];
fromFile.Read(buffer, 0, buffer.Length);
fromFile.Flush();
toFile.Write(buffer, 0, buffer.Length);
toFile.Flush();
}
fromFile.Close();
toFile.Close();
return true;
}
}测试代码:
class Program
{
static void Main(string[] args)
{
Stopwatch watch = new Stopwatch();
watch.Start();
if (FileHelper.CopyFile(@"D:\安装文件\新建文件夹\SQLSVRENT_2008R2_CHS.iso", @"F:\SQLSVRENT_2008R2_CHS.iso", 1024 * 1024 * 5))
{
watch.Stop();
Console.WriteLine("拷贝完成,耗时:" + watch.Elapsed.Seconds + "秒");
}
Console.Read();
}
}
相关文章推荐
- C# 执行cmd命令,以进程的形式执行应用程序,在新的线程中执行耗时的功能逻辑
- C# 14位日期型字符串yyyyMMddHHmmss转变为日期格式
- c#解压
- c#压缩文件
- c#压缩目录
- c#递归压缩文件夹方法
- c#导出Excel文件,转换为可读模式
- c#获取远程主机MAC
- c#获取远程主机IP
- c#获取本机的MAC
- C#之AES256位加密解密
- C#数据导出到Excel文件
- 20 如何在C#中存一批数据,数组
- C#调试跟踪函数的使用及找出最大值及最大值所在索引
- c#数字转换为大写汉字
- C#StreamReader 读取Txt文件
- c#删除指定目录下所有内容:方法二--找到所有文件和子文件夹删除
- c#删除指定目录下所有内容
- C#移动文件夹中的所有文件夹与文件到另一个文件夹
- c# 复制文件夹中的所有文件夹与文件到另一个文件夹