private void btnSplit_Click(object sender, EventArgs e) { string inputFile = txtInputFile.Text; // Substitute this with your Input File FileStream fs = new FileStream(inputFile, FileMode.Open, FileAccess.Read); int numberOfFiles = Convert.ToInt32(txtChunks.Text); int sizeOfEachFile = (int)Math.Ceiling((double)fs.Length / numberOfFiles); for (int i = 1; i <= numberOfFiles; i++) { string baseFileName = Path.GetFileNameWithoutExtension(inputFile); string extension = Path.GetExtension(inputFile); FileStream outputFile = new FileStream(Path.GetDirectoryName(inputFile) + "\\" + 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(); }
private void btnMerge_Click(object sender, EventArgs e) { string outPath = txtInputFolder.Text; // Substitute this with your Input Folder string[] tmpFiles = Directory.GetFiles(outPath, "*.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(outPath + 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(); }
codemaverick
0 nhận xét:
Đăng nhận xét