using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace FileStrem大文件分割复制
{
public partial class Form1 : Form
{
private int WriterByetNub = ;//100M复制速度
//源目标
private FileStream FileToRead;
//复制到文件
private FileStream FileToWrite;
//保存文件的地址
private string SaveFile_Add;
//源文件的名字
private string File_Add;
//设置正常写入字节
private Byte[] byteToWrite;
//设置剩余写入字节
private Byte[] byteToLastWrite;
//循环次数
private long WriteTimes;
//循环后的剩余字节
private int L_Size; public Form1()
{
InitializeComponent();
}
//设置委托
private delegate void OpenFile(); private void Cpy()
{
try
{
label_Add.Text = "源地址"; label_Cpy_Add.Text = "复制到"; label_Cpy_Lc.Text = "复制进程:"; label_Write.Text = "已经写入"; label_FileSize.Text = "源文件总大小";
//文件选取
OpenFileDialog openfileDialog = new OpenFileDialog();
//show文件选取器
openfileDialog.ShowDialog(); File_Add = openfileDialog.FileName; label_Add.Text += ":" + File_Add; //保存地址选取
FolderBrowserDialog savefileDialog = new FolderBrowserDialog(); savefileDialog.ShowDialog(); SaveFile_Add = savefileDialog.SelectedPath; label_Cpy_Add.Text += ":" + SaveFile_Add + File_Add; FileToRead = new FileStream(File_Add, FileMode.Open, FileAccess.Read); FileToWrite = new FileStream(@SaveFile_Add + "\\" + openfileDialog.SafeFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); label_FileSize.Text = "源文件总大小"+(FileToRead.Length/).ToString()+"KB";
if (FileToRead.Length > WriterByetNub)
//设置写入字节数组
{
byteToWrite = new byte[WriterByetNub];
//循环次数
WriteTimes = FileToRead.Length / WriterByetNub;
//多次循环后剩余字节
L_Size = Convert.ToInt32(FileToRead.Length % WriterByetNub);
//多次循环后字节数组
byteToLastWrite = new byte[L_Size]; for (long i = ; i <= WriteTimes; i++)
{
//读源文件
FileToRead.Read(byteToWrite, , WriterByetNub); //写数据到目标文件
FileToWrite.Write(byteToWrite, , WriterByetNub); //设置进度条的值
progressBar.Value = Convert.ToInt32(i * / WriteTimes); Application.DoEvents(); //设置Lable上的进度值
label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32((i * ) / WriteTimes).ToString() + "%"; //设置写入值
label_Write.Text = "已写入" + (FileToRead.Position / ).ToString() + "KB";
} //剩余字节的读和写
if (L_Size != )
{
FileToRead.Read(byteToLastWrite, , L_Size); FileToWrite.Write(byteToLastWrite, , L_Size);
}
}
else
{
progressBar.Maximum =(int) FileToRead.Length;
byteToWrite = new byte[FileToRead.Length];
FileToRead.Read(byteToWrite, , (int)FileToRead.Length);
label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32(FileToRead.Position/FileToRead.Length*).ToString() + "%"; //设置写入值
label_Write.Text = "已写入" + (FileToRead.Position / ).ToString() + "KB";
progressBar.Value =(int )FileToRead.Position;
FileToWrite.Write(byteToWrite, , (int)FileToRead.Length);
}
FileToRead.Flush(); FileToWrite.Flush(); FileToRead.Close(); FileToWrite.Close(); MessageBox.Show("复制完成");
}
catch(Exception ex) {
FileToRead.Flush(); FileToWrite.Flush(); FileToRead.Close(); FileToWrite.Close(); MessageBox.Show(ex.ToString()); }
} private void openFileBtn_Click(object sender, EventArgs e)
{
OpenFile getFile = new OpenFile(Cpy);
this.Invoke(getFile);
} private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
if (f.ShowDialog() == DialogResult.OK)
{
textBox2.Text = f.SelectedPath +@"\"+Path.GetFileName(textBox1.Text.Trim());
}
} private void button1_Click(object sender, EventArgs e)
{
ApiCopyFile.DoCopy(textBox1.Text.Trim(), textBox2.Text.Trim());
} }
public class ApiCopyFile
{
private const int FO_COPY = 0x0002;
private const int FOF_ALLOWUNDO = 0x00044;
//显示进度条 0x00044 // 不显示一个进度对话框 0x0100 显示进度对话框单不显示进度条 0x0002显示进度条和对话框
private const int FOF_SILENT = 0x0002;//0x0100;
//
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = )]
public struct SHFILEOPSTRUCT
{
public IntPtr hwnd;
[MarshalAs(UnmanagedType.U4)]
public int wFunc;
public string pFrom;
public string pTo;
public short fFlags;
[MarshalAs(UnmanagedType.Bool)]
public bool fAnyOperationsAborted;
public IntPtr hNameMappings;
public string lpszProgressTitle;
}
[DllImport("shell32.dll", CharSet = CharSet.Auto)]
static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
public static bool DoCopy(string strSource, string strTarget)
{
SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
fileop.wFunc = FO_COPY;
fileop.pFrom = strSource;
fileop.lpszProgressTitle = "复制大文件";
fileop.pTo = strTarget;
//fileop.fFlags = FOF_ALLOWUNDO;
fileop.fFlags = FOF_SILENT; return SHFileOperation(ref fileop) == ;
}
}
}

FileStrem大文件分割复制的更多相关文章

  1. c# 大文件分割 复制 Filestream 进度条

    大文件分割复制,每次复制100M 也可以复制别的较大数值. 小于1G的小文件就直接复制得了.代码里没写 ,但是很简单 直接写进去就好了,难得是分割复制 所以没写. 好吧 我还是改了 改成小文件也可以复 ...

  2. c#大文件分割过程

    需求: 在项目开发中,我们会遇到单个文件大小超过1TB的文件,这样的文件只能进行单文件读取,往往会造成读取完成耗时过长,导致客户在使用体验过程中不满意. 为了解决提升大文件的解析速度,我想到了先分割大 ...

  3. android下大文件分割上传

    由于android自身的原因,对大文件(如影视频文件)的操作很容易造成OOM,即:Dalvik堆内存溢出,利用文件分割将大文件分割为小文件可以解决问题. 文件分割后分多次请求服务. //文件分割上传 ...

  4. PHP + JS 实现大文件分割上传

    服务器上传文件会有一定的限制.避免内存消耗过大影响性能,在 php.ini 配置文件中,有几个影响参数: upload_max_filesize = 2M //PHP最大能接受的文件大小 post_m ...

  5. Linux大文件分割splite

    /********************************************************************** * Linux大文件分割splite * 说明: * 编 ...

  6. Html5 突破微信限制实现大文件分割上传

    先来前端代码 <!DOCTYPE html> <html> <head> <meta name="viewport" content=&q ...

  7. 大文件分割、命令脚本 - Python

    日志文件分割.命名 工作中经常会收到测试同学.客户同学提供的日志文件,其中不乏几百M一G的也都有,毕竟压测一晚上产生的日志量还是很可观的,xDxD,因此不可避免的需要对日志进行分割,通常定位问题需要针 ...

  8. Linux中split大文件分割和cat合并文件

    当需要将较大的数据上传到服务器,或从服务器下载较大的日志文件时,往往会因为网络或其它原因而导致传输中断而不得不重新传输.这种情况下,可以先将大文件分割成小文件后分批传输,传完后再合并文件. 1.分割 ...

  9. formdata方式上传文件,支持大文件分割上传

    1.upload.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <h ...

随机推荐

  1. Mysql——常用命令

    查看版本:show variables like '%version%'   或者   select version() 是否开启binlog:show variables like 'log_bin ...

  2. coreDNS域名无法解析问题

    问题: 在pod内无法解析域名 解决: busybox的镜像有bug,导致ping可以解析,但是nslookup无法解析 kubectl run -it --rm --image=infoblox/d ...

  3. jquery中对地址中的中文进行encodeURI编码

    传递参数:<script type="text/javascript">     var id= 'abc';  //字符串英文     var num = 998;  ...

  4. C#中的索引器(Indexers)

    前两天刚刚学习完了属性,这两天又搂完了索引器,发现两者非常的相似,但是相似之外还有一些不同之处.今天就来总结一下索引器--Indexers 索引器的作用及格式 索引器的作用就是能够使类或者结构体的实例 ...

  5. 【CSS】我的样式哪里来的?—— css的继承性

    在之前我们写css的时候,曾经出现过如下这样一种情况: 6继承性.html(head部分) <style> div { background-color: #ccc; font-size: ...

  6. 11个顶级 JavaScript 日历插件

    参考链接:https://mp.weixin.qq.com/s?__biz=MzI3NzIzMDY0NA==&mid=2247487050&idx=1&sn=e1cf66726 ...

  7. 使用TensorFlow训练SSD(二):数据的准备

    在进行模型的训练之前,需要准备好相关的数据,相关的数据还需要进行标注.这篇博客将使用labelImg标注工具来进行数据的处理. 首先可从https://github.com/tzutalin/labe ...

  8. 手把手教你在pycharm上上传项目至GitHub

    如果你还没有下载Git,请移步下载:https://git-scm.com/downloads 下载后解压傻瓜式安装,不过请记住你的安装目录,我们会用到. 以我的安装目录为例:D:\Program F ...

  9. sqarkSQL hiveSql

    查看数据库 show databases; 进入数据库 use 库名 查看表 show tables: select * from 表名 hdfs传输spark sql查询 hive找到指定路径sql ...

  10. python私有化xx、_xx、__xx、__xx__、xx_的区别

    xx:共有变量. _xx:私有化的属性或方法,from xxx import * 时无法导入,子类的对象和子类可以访问. __xx:避免与子类中的属性命名冲突,无法在外部直接访问(名字重整所以访问不到 ...