zoukankan      html  css  js  c++  java
  • 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 + "
    ");
                }
            }
  • 相关阅读:
    BZOJ1406: [AHOI2007]密码箱 数论
    BZOJ5188: [Usaco2018 Jan]MooTube 并查集+离线处理
    BZOJ2662: [BeiJing wc2012]冻结 spfa+分层图
    BZOJ1297: [SCOI2009]迷路 矩阵快速幂
    BZOJ4887: [Tjoi2017]可乐 矩阵快速幂
    BZOJ5168: [HAOI2014]贴海报 线段树
    开发富文本编辑器的一些经验教训
    数据可视化的发展前景、商业/职业前景?
    市场调研中如何做数据分析?
    当前火热的短视频,背后有着哪些黑科技技术?
  • 原文地址:https://www.cnblogs.com/huhangfei/p/4989210.html
Copyright © 2011-2022 走看看