zoukankan      html  css  js  c++  java
  • FileStrem大文件分割复制

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.IO;
      7 using System.Linq;
      8 using System.Runtime.InteropServices;
      9 using System.Text;
     10 using System.Threading.Tasks;
     11 using System.Windows.Forms;
     12 
     13 namespace FileStrem大文件分割复制
     14 {
     15     public partial class Form1 : Form
     16     {
     17         private int WriterByetNub = 104857600;//100M复制速度
     18         //源目标
     19         private FileStream FileToRead;
     20         //复制到文件
     21         private FileStream FileToWrite;
     22         //保存文件的地址
     23         private string SaveFile_Add;
     24         //源文件的名字
     25         private string File_Add;
     26         //设置正常写入字节
     27         private Byte[] byteToWrite;
     28         //设置剩余写入字节
     29         private Byte[] byteToLastWrite;
     30         //循环次数
     31         private long WriteTimes;
     32         //循环后的剩余字节
     33         private int L_Size;
     34 
     35         public Form1()
     36         {
     37             InitializeComponent();
     38         }
     39         //设置委托
     40         private delegate void OpenFile();
     41 
     42         private void Cpy()
     43         {
     44             try
     45             {
     46                 label_Add.Text = "源地址";
     47 
     48                 label_Cpy_Add.Text = "复制到";
     49 
     50                 label_Cpy_Lc.Text = "复制进程:";
     51 
     52                 label_Write.Text = "已经写入";
     53 
     54                 label_FileSize.Text = "源文件总大小";
     55                 //文件选取
     56                 OpenFileDialog openfileDialog = new OpenFileDialog();
     57                 //show文件选取器
     58                 openfileDialog.ShowDialog();
     59 
     60                 File_Add = openfileDialog.FileName;
     61 
     62                 label_Add.Text += "" + File_Add;
     63                 
     64                 //保存地址选取
     65                 FolderBrowserDialog savefileDialog = new FolderBrowserDialog();
     66 
     67                 savefileDialog.ShowDialog();
     68 
     69                 SaveFile_Add = savefileDialog.SelectedPath;
     70 
     71                 label_Cpy_Add.Text += "" + SaveFile_Add + File_Add;
     72 
     73                 FileToRead = new FileStream(File_Add, FileMode.Open, FileAccess.Read);
     74 
     75                 FileToWrite = new FileStream(@SaveFile_Add + "\" + openfileDialog.SafeFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
     76 
     77                 label_FileSize.Text = "源文件总大小"+(FileToRead.Length/1024).ToString()+"KB";
     78                 if (FileToRead.Length > WriterByetNub)
     79                 //设置写入字节数组
     80                 {
     81                     byteToWrite = new byte[WriterByetNub];
     82                     //循环次数
     83                     WriteTimes = FileToRead.Length / WriterByetNub;
     84                     //多次循环后剩余字节
     85                     L_Size = Convert.ToInt32(FileToRead.Length % WriterByetNub);
     86                     //多次循环后字节数组
     87                     byteToLastWrite = new byte[L_Size];
     88 
     89                     for (long i = 0; i <= WriteTimes; i++)
     90                     {
     91                         //读源文件
     92                         FileToRead.Read(byteToWrite, 0, WriterByetNub);
     93 
     94                         //写数据到目标文件
     95                         FileToWrite.Write(byteToWrite, 0, WriterByetNub);
     96 
     97                         //设置进度条的值
     98                         progressBar.Value = Convert.ToInt32(i * 100 / WriteTimes);
     99 
    100                         Application.DoEvents();
    101 
    102                         //设置Lable上的进度值
    103                         label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32((i * 100) / WriteTimes).ToString() + "%";
    104 
    105                         //设置写入值
    106                         label_Write.Text = "已写入" + (FileToRead.Position / 1024).ToString() + "KB";
    107                     }
    108 
    109                     //剩余字节的读和写
    110                     if (L_Size != 0)
    111                     {
    112                         FileToRead.Read(byteToLastWrite, 0, L_Size);
    113 
    114                         FileToWrite.Write(byteToLastWrite, 0, L_Size);
    115                     }
    116                 }
    117                 else
    118                 {
    119                     progressBar.Maximum =(int) FileToRead.Length;
    120                     byteToWrite = new byte[FileToRead.Length];
    121                     FileToRead.Read(byteToWrite, 0, (int)FileToRead.Length);
    122                     label_Cpy_Lc.Text = "复制进程:" + Convert.ToInt32(FileToRead.Position/FileToRead.Length*100).ToString() + "%";
    123 
    124                     //设置写入值
    125                     label_Write.Text = "已写入" + (FileToRead.Position / 1024).ToString() + "KB";
    126                     progressBar.Value =(int )FileToRead.Position;
    127                     FileToWrite.Write(byteToWrite, 0, (int)FileToRead.Length);                   
    128                 }
    129                 FileToRead.Flush();
    130 
    131                 FileToWrite.Flush();
    132 
    133                 FileToRead.Close();
    134 
    135                 FileToWrite.Close();
    136 
    137                 MessageBox.Show("复制完成");
    138             }
    139             catch(Exception ex)
    140 
    141             {
    142                 FileToRead.Flush();
    143 
    144                 FileToWrite.Flush();
    145 
    146                 FileToRead.Close();
    147 
    148                 FileToWrite.Close();
    149 
    150                 MessageBox.Show(ex.ToString());
    151                
    152             }
    153         }
    154 
    155         private void openFileBtn_Click(object sender, EventArgs e)
    156         {
    157             OpenFile getFile = new OpenFile(Cpy);
    158             this.Invoke(getFile);
    159         }
    160 
    161 
    162 
    163 
    164 
    165 
    166 
    167 
    168 
    169 
    170         private void button2_Click(object sender, EventArgs e)
    171         {
    172             OpenFileDialog openFileDialog1 = new OpenFileDialog();
    173             if (openFileDialog1.ShowDialog() == DialogResult.OK)
    174             {
    175                 textBox1.Text = openFileDialog1.FileName;
    176             }
    177         }
    178         private void button3_Click(object sender, EventArgs e)
    179         {
    180             FolderBrowserDialog f = new FolderBrowserDialog();
    181             if (f.ShowDialog() == DialogResult.OK)
    182             {
    183                 textBox2.Text = f.SelectedPath +@""+Path.GetFileName(textBox1.Text.Trim());
    184             }
    185         }
    186 
    187         private void button1_Click(object sender, EventArgs e)
    188         {
    189             ApiCopyFile.DoCopy(textBox1.Text.Trim(), textBox2.Text.Trim());
    190         }
    191 
    192        
    193     }
    194     public class ApiCopyFile
    195     {
    196         private const int FO_COPY = 0x0002;
    197         private const int FOF_ALLOWUNDO = 0x00044;
    198         //显示进度条  0x00044 // 不显示一个进度对话框 0x0100 显示进度对话框单不显示进度条  0x0002显示进度条和对话框  
    199         private const int FOF_SILENT = 0x0002;//0x0100;  
    200         //  
    201         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 0)]
    202         public struct SHFILEOPSTRUCT
    203         {
    204             public IntPtr hwnd;
    205             [MarshalAs(UnmanagedType.U4)]
    206             public int wFunc;
    207             public string pFrom;
    208             public string pTo;
    209             public short fFlags;
    210             [MarshalAs(UnmanagedType.Bool)]
    211             public bool fAnyOperationsAborted;
    212             public IntPtr hNameMappings;
    213             public string lpszProgressTitle;
    214         }
    215         [DllImport("shell32.dll", CharSet = CharSet.Auto)]
    216         static extern int SHFileOperation(ref SHFILEOPSTRUCT FileOp);
    217         public static bool DoCopy(string strSource, string strTarget)
    218         {
    219             SHFILEOPSTRUCT fileop = new SHFILEOPSTRUCT();
    220             fileop.wFunc = FO_COPY;
    221             fileop.pFrom = strSource;
    222             fileop.lpszProgressTitle = "复制大文件";
    223             fileop.pTo = strTarget;
    224             //fileop.fFlags = FOF_ALLOWUNDO;  
    225             fileop.fFlags = FOF_SILENT;
    226 
    227             return SHFileOperation(ref fileop) == 0;
    228         }
    229     }
    230 }
  • 相关阅读:
    修改mysql数据库字段内容默认值为当前时间
    报错 raise MigrationSchemaMissing("Unable to create the django_migrations table (%s)" % exc)
    python标准库之ConfigParser模块读写配置文件
    使用xshell连接liunx服务器遇到的问题( Connection failed)
    linux学习笔记(四)
    本地项目上传到github
    Java 获取昨天、前天时间
    Kibana 启动报错 Error: Unable to write Kibana UUID file, please check the uuid.server configuration value in kibana.yml
    Mybatis foreach 标签的使用
    RabbitMQ 延时队列
  • 原文地址:https://www.cnblogs.com/ccxz/p/9413936.html
Copyright © 2011-2022 走看看