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;
}

你可能感兴趣的:(大文件)