csharp C#文件分割器代码。第二个文件是拆分器合并应用程序。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp C#文件分割器代码。第二个文件是拆分器合并应用程序。相关的知识,希望对你有一定的参考价值。

private static void SplitFile(string inputFile, int chunkSize, string path)
    {
        byte[] buffer = new byte[chunkSize];
        List<byte> extraBuffer = new List<byte>();

        using (Stream input = File.OpenRead(inputFile))
        {
            int index = 0;
            while (input.Position < input.Length)
            {
                using (Stream output = File.Create(path + "\\" + index + ".csv"))
                {
                    int chunkBytesRead = 0;
                    while (chunkBytesRead < chunkSize)
                    {
                        int bytesRead = input.Read(buffer,
                                                   chunkBytesRead,
                                                   chunkSize - chunkBytesRead);

                        if (bytesRead == 0)
                        {
                            break;
                        }

                        chunkBytesRead += bytesRead;
                    }

                    byte extraByte = buffer[chunkSize - 1];
                    while (extraByte != '\n')
                    {
                        int flag = input.ReadByte();
                        if (flag == -1)
                            break;
                        extraByte = (byte)flag;
                        extraBuffer.Add(extraByte);
                    }

                    output.Write(buffer, 0, chunkBytesRead);
                    if (extraBuffer.Count > 0)
                        output.Write(extraBuffer.ToArray(), 0, extraBuffer.Count);

                    extraBuffer.Clear();
                }
                index++;
            }
        }
    }
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Splitfiles
{
    public partial class Form1 : Form
    {
        public FileStream fs;
        string mergeFolder;
        public Form1()
        {
            InitializeComponent();
        }
        List<string> Packets = new List<string>();
        //Merge file is stored in drive
        string SaveFileFolder = @"c:\";
//Code Under Brows button:                  
        private void brows_Click(object sender, EventArgs e)
        {
            try
            {
                openFileDialog1.ShowDialog();
                txtBrowsFile.Text = openFileDialog1.FileName;              
                fs = new FileStream(txtBrowsFile.Text, FileMode.Open, FileAccess.Read);
                int FileLength = (int)fs.Length / 1024;
                string name = Path.GetFileName(txtBrowsFile.Text);
            }
            catch (Exception ex)
            {
                lblSendingResult.Text = "EXCEPTION:" + ex;
            }
        }
//Code Under Split button:
        private void btnSplit_Click(object sender, EventArgs e)
        {
            SplitFile(txtBrowsFile.Text, Convert.ToInt32(5));
            listBox1.Items.Add(Packets[0].ToString());
            listBox1.Items.Add(Packets[1].ToString());
            listBox1.Items.Add(Packets[2].ToString());
            listBox1.Items.Add(Packets[3].ToString());
            listBox1.Items.Add(Packets[4].ToString());           
        }
        public bool SplitFile(string SourceFile, int nNoofFiles)
        {
            bool Split = false;
            try
            {
                FileStream fs = new FileStream(SourceFile, FileMode.Open, FileAccess.Read);
                int SizeofEachFile = (int)Math.Ceiling((double)fs.Length / nNoofFiles);
                for (int i = 0; i < nNoofFiles; i++)
                {
                    string baseFileName = Path.GetFileNameWithoutExtension(SourceFile);
                    string Extension = Path.GetExtension(SourceFile);
                    FileStream outputFile = new FileStream(Path.GetDirectoryName(SourceFile) + "\\" + baseFileName + "." +
                        i.ToString().PadLeft(5, Convert.ToChar("0")) + Extension + ".tmp", FileMode.Create, FileAccess.Write);
                    mergeFolder = Path.GetDirectoryName(SourceFile);
                    int bytesRead = 0;
                    byte[] buffer = new byte[SizeofEachFile];
                    if ((bytesRead = fs.Read(buffer, 0, SizeofEachFile)) > 0)
                    {
                        outputFile.Write(buffer, 0, bytesRead);
                        //outp.Write(buffer, 0, BytesRead);
                        string packet = baseFileName + "." + i.ToString().PadLeft(3, Convert.ToChar("0")) + Extension.ToString();
                        Packets.Add(packet);
                    }
                    outputFile.Close();
                }
                fs.Close();
            }
            catch (Exception Ex)
            {
                throw new ArgumentException(Ex.Message);
            }

            return Split;
        }
//Code Under Merge button:
//Files have been merged and saved at location C:\\"
        private void btnMergeFile_Click(object sender, EventArgs e)
        {
            MergeFile(mergeFolder);
        }
        public bool MergeFile(string inputfoldername1)
        {
            bool Output = false;
            try
            {
                string[] tmpfiles = Directory.GetFiles(inputfoldername1, "*.tmp");
                FileStream outPutFile = null;
                string PrevFileName = "";
                foreach (string tempFile in tmpfiles)
                {
                    string fileName = Path.GetFileNameWithoutExtension(tempFile);
                    string baseFileName = fileName.Substring(0, fileName.IndexOf(Convert.ToChar(".")));
                    string extension = Path.GetExtension(fileName);
                    if (!PrevFileName.Equals(baseFileName))
                    {
                        if (outPutFile != null)
                        {
                            outPutFile.Flush();
                            outPutFile.Close();
                        }
                        outPutFile = new FileStream(SaveFileFolder + "\\" + baseFileName + extension, FileMode.OpenOrCreate, FileAccess.Write);
                    }
                    int bytesRead = 0;
                    byte[] buffer = new byte[1024];
                    FileStream inputTempFile = new FileStream(tempFile, FileMode.OpenOrCreate, FileAccess.Read);
                    while ((bytesRead = inputTempFile.Read(buffer, 0, 1024)) > 0)
                        outPutFile.Write(buffer, 0, bytesRead);
                    inputTempFile.Close();
                    File.Delete(tempFile);
                    PrevFileName = baseFileName;
                }
                outPutFile.Close();
                lblSendingResult.Text = "Files have been merged and saved at location C:\\";
            }
            catch
            {
            }
            return Output;
        }
    }
} 

以上是关于csharp C#文件分割器代码。第二个文件是拆分器合并应用程序。的主要内容,如果未能解决你的问题,请参考以下文章

管理在另一个kendo分割器中调整kendo分割器的大小

如何在 tkinter 中为每个分割屏幕添加计时器

linux文件拆分命令

csharp Foreach循环示例。第二个例子来自计算机程序基础与C#http://www.introprogramming.info/wp-content/uploa

csharp Foreach循环示例。第二个例子来自计算机程序基础与C#http://www.introprogramming.info/wp-content/uploa

SQL拆分逗号分隔的字符串