zoukankan      html  css  js  c++  java
  • c#大文件分割合并

    public static 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);

    int bytesRead = 0;
    byte[] buffer = new byte[SizeofEachFile];

    if ((bytesRead = fs.Read(buffer, 0, SizeofEachFile)) > 0)
    {
    outputFile.Write(buffer, 0, bytesRead);
    }

    outputFile.Close();

    }
    fs.Close();
    }
    catch (Exception Ex)
    {
    throw Ex;
    }

    return Split;
    }






    public static bool MergeFile(string InputFileFolder)
    {
    bool Output = false;

    try
    {
    string[] tmpfiles = Directory.GetFiles(InputFileFolder, "*.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(InputFileFolder + "\\" + 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();
    }
    catch (Exception Ex)
    {
    throw Ex;
    }

    return Output;
    }
  • 相关阅读:
    关于width属性max/min-content和fit-content
    director.js实现前端路由
    第一篇博文
    AtCoder Beginner Contest 120 题解
    AtCoder Beginner Contest 121 题解
    AtCoder Beginner Contest 115 题解
    Python中list的复制及深拷贝与浅拷贝探究
    Python中print()函数不换行的方法
    高斯消元法的C++简单实现
    Python中list作为默认参数的陷阱
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175756.html
Copyright © 2011-2022 走看看