Last active
January 22, 2022 13:59
-
-
Save th3terrorist/8433058b48a5483128a9acb436b9aeaa to your computer and use it in GitHub Desktop.
File splitting and merging demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Collections.Generic; | |
namespace Example | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
if (args.Length < 2) | |
{ | |
Console.WriteLine("Usage: <filename> <parts>"); | |
return; | |
} | |
var splitter = new FileSplitter.Splitter(args[0]); | |
var merger = new FileSplitter.Merger(splitter.Split(Convert.ToInt32(args[1]))); | |
merger.Merge("_" + args[0]); | |
} | |
} | |
} | |
namespace FileSplitter | |
{ | |
class Splitter | |
{ | |
private FileStream stream; | |
public Splitter(string path) | |
{ | |
this.stream = File.OpenRead(path); | |
} | |
private byte[] MergeParts(byte[] first, byte[] second) | |
{ | |
byte[] merged = new byte[first.Length + second.Length]; | |
Array.Copy(first, merged, first.Length); | |
Array.Copy(second, 0, merged, first.Length, second.Length); | |
return merged; | |
} | |
private byte[] MakeTail(byte[] lastBuffer, int remaining) | |
{ | |
byte[] tail = new byte[remaining]; | |
int readBytes = stream.Read(tail); | |
if (readBytes == 0) | |
{ | |
Array.Copy(lastBuffer, tail, remaining); | |
return tail; | |
} | |
else | |
{ | |
return MergeParts(lastBuffer, tail); | |
} | |
} | |
public IEnumerable<byte[]> Split(int nparts, bool mergeLast = true) | |
{ | |
int step = (int)this.stream.Length / nparts; | |
int remaining = (int)stream.Length % nparts; | |
byte[] outBuffer = new byte[step]; | |
while (stream.Read(outBuffer, 0, step) == step) | |
{ | |
if (stream.Length - stream.Position < step | |
&& stream.Position < stream.Length - 1 | |
&& mergeLast) | |
break; | |
yield return outBuffer; | |
Array.Fill<byte>(outBuffer, 0); | |
} | |
if (remaining > 0) | |
yield return MakeTail(outBuffer, remaining); | |
} | |
} | |
class Merger | |
{ | |
IEnumerable<byte[]> parts; | |
public Merger(IEnumerable<byte[]> parts) | |
{ | |
this.parts = parts; | |
} | |
public void Merge(string path) | |
{ | |
using (var stream = new FileStream(path, FileMode.Append)) | |
{ | |
foreach (var part in parts) | |
stream.Write(part, 0, part.Length); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment