FileStrem大文件分割复制
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大文件分割复制的更多相关文章
- c# 大文件分割 复制 Filestream 进度条
大文件分割复制,每次复制100M 也可以复制别的较大数值. 小于1G的小文件就直接复制得了.代码里没写 ,但是很简单 直接写进去就好了,难得是分割复制 所以没写. 好吧 我还是改了 改成小文件也可以复 ...
- c#大文件分割过程
需求: 在项目开发中,我们会遇到单个文件大小超过1TB的文件,这样的文件只能进行单文件读取,往往会造成读取完成耗时过长,导致客户在使用体验过程中不满意. 为了解决提升大文件的解析速度,我想到了先分割大 ...
- android下大文件分割上传
由于android自身的原因,对大文件(如影视频文件)的操作很容易造成OOM,即:Dalvik堆内存溢出,利用文件分割将大文件分割为小文件可以解决问题. 文件分割后分多次请求服务. //文件分割上传 ...
- PHP + JS 实现大文件分割上传
服务器上传文件会有一定的限制.避免内存消耗过大影响性能,在 php.ini 配置文件中,有几个影响参数: upload_max_filesize = 2M //PHP最大能接受的文件大小 post_m ...
- Linux大文件分割splite
/********************************************************************** * Linux大文件分割splite * 说明: * 编 ...
- Html5 突破微信限制实现大文件分割上传
先来前端代码 <!DOCTYPE html> <html> <head> <meta name="viewport" content=&q ...
- 大文件分割、命令脚本 - Python
日志文件分割.命名 工作中经常会收到测试同学.客户同学提供的日志文件,其中不乏几百M一G的也都有,毕竟压测一晚上产生的日志量还是很可观的,xDxD,因此不可避免的需要对日志进行分割,通常定位问题需要针 ...
- Linux中split大文件分割和cat合并文件
当需要将较大的数据上传到服务器,或从服务器下载较大的日志文件时,往往会因为网络或其它原因而导致传输中断而不得不重新传输.这种情况下,可以先将大文件分割成小文件后分批传输,传完后再合并文件. 1.分割 ...
- formdata方式上传文件,支持大文件分割上传
1.upload.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/html"> <h ...
随机推荐
- 解决DBGridEh遍历记录后不移动当前行位置的方法
解决DBGridEh遍历记录后不移动当前行位置的方法 在用DBGridEh配合ClientDataSet使用时,需要知道用户选择了哪些记录,可用遍历记录的方法查询选择列是否为真,但在这之后,Clien ...
- Python 常用模块(2) 序列化(pickle,shelve,json,configpaser)
主要内容: 一. 序列化概述 二. pickle模块 三. shelve模块 四. json模块(重点!) 五. configpaser模块 一. 序列化概述1. 序列化: 将字典,列表等内容转换成一 ...
- RobotFramework的安装
Robot Framework自动化测试框架+可视化编辑工具RIDE+Selenium2这是规范的webAPI. 一通过下载安装包安装 1)RF 框架是基于 Python 语言的,所以一定要有 Pyt ...
- import、from 模块 import*、reload
import 模块名.from 模块名 import* 均为导入模块,前者调用模块中函数或者变量时需要添加引用,即模块名.调用函数或者变量名 具体用法见下 https://jingyan.baidu. ...
- Int8,Int16,Int32,Int64 有啥不同呢?看了立马就懂!
大家有没有写了很久代码,还不知道这个Int8,Int16, Int32, Int64有什么区别呢?或者是为什么后面的数字不一样呢? 初步了解了一下,才清楚这个东西. 先来扫盲一下计算机存储单元, 在计 ...
- PHP学习(3)——数据的存储与检索
要点目录: I.保存数据 II.打开文件 III.创建并写入文件 IV.关闭文件 V.读文件 VI.给文件加锁 VII.删除文件 VIII.其他有用的文件操作函数 IX.数据库管理系统 1.保存数 ...
- nRF5 SDK Bootloader and DFU moudles(3)
DFU控制点特性用于控制DFU过程的状态. 通过写入该特征来请求所有DFU程序. 标记过程结束的响应将作为通知收到. BLE传输 Transfer of an init packet DFU控制器首先 ...
- 这可能是最简单易懂的 ZooKeeper 笔记
分布式架构 CAP 与 BASE 理论 一致性协议 初识 Zookeeper Zookeeper 介绍 Zookeeper 工作机制 Zookeeper 特点 Zookeeper 数据结构 Zooke ...
- mybatis-plus配置多数据源invalid bound statement (not found)
mybatis-plus配置多数据源invalid bound statement (not found) 错误原因 引入mybatis-plus应该使用的依赖如下,而不是mybatis <de ...
- Clone()方法详解
一.克隆的原理与应用 clone在堆上分配内存,分配的内存和源对象(即调用clone方法的对象)相同,然后再使用原对象中对应的各个域,填充新对象的域, 填充完成之后,clone方法返回,一个新的相同的 ...