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 ...
随机推荐
- Kafka 和 ZooKeeper 的分布式消息队列分析
1. Kafka 总体架构 基于 Kafka-ZooKeeper 的分布式消息队列系统总体架构如下: 如上图所示,一个典型的 Kafka 体系架构包括若干 Producer(消息生产者),若干 bro ...
- Delphi7-TClientDataSet: 查找
TClientDataSet[12]: 查找 方法有:1.Locate: 根据字段列表和对应的字段值查找并定位, 找到返回 True.2.Lookup: 根据字段列表和对应的字段值查找, 返回需要的字 ...
- Pearson Correlation Score
[http://www.statisticshowto.com/what-is-the-pearson-correlation-coefficient/] Correlation between se ...
- NSubstitute.Analyzers检测NSubstitute用法冲突
NSubstitute是一个.Net环境使用的,简洁,语法友好的Mock库.语法简洁的缺点是有一些失败的用法很难察觉和检测.比如试图mock一个非虚拟成员-NSubstitute不能看到这些成员所以不 ...
- vue路由传参的几种基本方式
原文地址 this.$router.push跳转 现有如下场景,点击父组件的li元素跳转到子组件中,并携带参数,便于子组件获取数据.父组件中: <li v-for="article i ...
- web赛题2
@上海赛wp 微信 和 https://www.ctfwp.com/articals/2019unctf.html 后续公告https://unctf.buuoj.cn/notice.html 必看! ...
- 【C/C++开发】【Java开发】JNI的替代者—使用JNA访问Java外部功能接口
JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Java Native Interface)吧,有过不同语言间通信经历的一般都知道,它允许Java代码和其他语言( ...
- Tensorflow之实现物体检测
目录 项目背景 TensorFlow介绍 环境搭建 模型选用 Api使用说明 运行路由 小结 项目背景 产品看到竞品可以标记物体的功能,秉承一贯的他有我也要有,他没有我更要有的作风,丢过来一网站,说这 ...
- python 指定文件夹下所有文件(包括子目录下的文件)拷贝到目标文件夹下
#!/usr/bin/env python3 # -*- coding:utf8 -*- # @TIME :2018/9/17 9:02 # @Author:dazhan # @File :copyf ...
- Appendix 2- Lebesgue integration and Reimann integration
Lebesgue integration and Reimann integration: Reimann: Split up the axis into equal intervals, then ...