public class FileSplitter
{
private string BuildPartFilename(string Filename, long Part)
{
string s;
s = Filename+"."+Part.ToString();
return s;
}
private int MinToRead(int a, int b)
{
return Math.Min(a, b);
}
public void Split(string Filename, string DestFolder, long cntParts, int PartSize)
{
FileStream fts; // FileToSplit
FileStream pf; // PartFile
string PartFilename;
int nBytes = 4096;
byte[] ByteArray = new byte[nBytes];
int BytesToRead, BytesRead;
long Done, OldDone;
try
{
fts = new FileStream(Filename, FileMode.Open, FileAccess.Read);
fts.Seek(0, SeekOrigin.Begin);
for (int i = 1; i <= cntParts; i++)
{
OldDone = 0;
BytesToRead = PartSize * 1024; // Size of the file parts
try
{
PartFilename = DestFolder+"\\"+BuildPartFilename(Path.GetFileName(Filename), i);
if (File.Exists(PartFilename))
File.Delete(PartFilename);
pf = new FileStream(PartFilename, FileMode.CreateNew, FileAccess.Write);
do
{
BytesRead = fts.Read(ByteArray, 0, Math.Min(ByteArray.Length, BytesToRead));
pf.Write(ByteArray, 0, BytesRead);
BytesToRead = BytesToRead - ByteArray.Length;
Done = fts.Position * 100 / fts.Length;
if (Done != OldDone)
{
OldDone = Done;
}
}
while (BytesToRead > 0);
pf.Close();
}
catch (
Exception e)
{
fts.Close();
throw new FSException(e.Message);
}
}
fts.Close();
}
catch (
Exception e)
{
throw new FSException(e.Message);
}
}
}