• C#文件的拆分与合并操作示例


    C#文件的拆分与合并操作示例代码。


    全局变量定义

        int size=100;//文件大小
            //拆分、合并的文件数
            int count;
            FileInfo splitFile;
            string splitFliePath;
            FileStream splitFileStream;
    
            FileInfo joinFile;
            string joinFliePath;
            FileStream joinFileStream;

    拆分文件操作

    private void CheckSplit_Click(object sender, EventArgs e)
            {            
                OpenFileDialog fd = new OpenFileDialog();
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    txtFilePath.Text = fd.FileName;
                    splitFile = new FileInfo(fd.FileName);
                    splitFliePath = splitFile.FullName.Substring(0, splitFile.FullName.Length - fd.SafeFileName.Length);
                    splitFileStream = new FileStream(splitFile.FullName, FileMode.Open, FileAccess.Read);
                }
            }
            private void btnSplit_Click(object sender, EventArgs e)
            {
                int length = (int)splitFileStream.Length;
                txtSplit.AppendText(Environment.NewLine + "拆分文件的大小:" + length + "
    ");
                count = length / (size * 1024);
                count = length % (size * 1024) == 0 ? count : count + 1;
                for (int i = 1; i <= count; i++)
                {
                    using (FileStream fgstream = new FileStream(splitFliePath + (i - 1) + splitFile.Extension, FileMode.OpenOrCreate, FileAccess.Write))
                    {
                        if (i == count)
                        {
                            size = length - (count - 1) * 102400;
                        }
                        else
                        {
                            size = 102400;
                        }
                        byte[] buffer = new byte[size];
                        int data = 0;
                        if ((data = splitFileStream.Read(buffer, 0, buffer.Length)) > 0)
                        {
                            BinaryWriter bWriter = new BinaryWriter(fgstream, Encoding.Default);
                            bWriter.Write(buffer, 0, data);  
                        }
                    }
                    txtSplit.AppendText(Environment.NewLine + "拆分出文件:" + splitFliePath + (i - 1) + splitFile.Extension + "
    ");
                }
                txtSplit.AppendText(Environment.NewLine + "拆分完成!
    ");
            }

    合并文件操作

    private void CheckJoin_Click(object sender, EventArgs e)
            {
                OpenFileDialog fd = new OpenFileDialog();
                if (fd.ShowDialog() == DialogResult.OK)
                {
                    txtFlie.Text = fd.FileName;
                    joinFile = new FileInfo(fd.FileName);
                    joinFliePath = "C:\Users\XXXX\Desktop\合并\";
                }
            }
    private void btnJoin_Click(object sender, EventArgs e)
            {
                using (joinFileStream = new FileStream(joinFliePath + "join" + joinFile.Extension, FileMode.OpenOrCreate, FileAccess.Write))
                {
                      for (int i = 0; i < count; i++)
                    {
                        using (FileStream readStream = new FileStream(joinFile.FullName.Substring(0, joinFile.FullName.Length - joinFile.Extension.Length - 1) + i + joinFile.Extension, FileMode.Open, FileAccess.Read))
                        {
                            byte[] buffer = new byte[readStream.Length];
                            int data = 0;
                            if ((data = readStream.Read(buffer, 0, buffer.Length)) > 0)
                            {
                                BinaryWriter binary = new BinaryWriter(joinFileStream, Encoding.Default);
                                binary.Write(buffer, 0, data);
                            }
                        }
                        txtJoin.AppendText(Environment.NewLine + "合并文件:" + splitFliePath + (i) + splitFile.Extension + "
    ");
                    }
                    txtJoin.AppendText(Environment.NewLine + "合并完成!
    ");
                    txtJoin.AppendText(Environment.NewLine + "合并后文件的大小:" + joinFileStream.Length + "
    ");
                }
            }
  • 相关阅读:
    数据结构学习记录_2019.02.26
    数据结构学习记录_2019.02.23
    数据结构学习记录_2019.02.27
    C语言学习记录_2019.02.23
    本博客所有博文密码均为“000000”
    Oracle触发器
    Oracle重载
    Oracle程序包
    Oracle函数
    Oracle存储过程
  • 原文地址:https://www.cnblogs.com/huhangfei/p/4989210.html
Copyright © 2020-2023  润新知