Wednesday, April 18, 2007

How to Split and Merge files using C#.NET

How to Split and Merge files using C#.NET
Namespace
using System.IO;
Variable Declaration
private const long FILE_SIZE_MB = 1024 * 1024;
private const long FILE_SIZE_KB = 1024;
private FileStream FSIn, FSout;
private int PreDefinedCacheSize;
Private Methods
// Gives the Filename from the long Path
private string FileName(string strString)
{
return strString.Substring(strString.LastIndexOf("\\"));
}
// This will set the Cache size
private int DefineCache(int useCache)
{
if (useCache == 8) return 8192;
else return 8192 * 2;
}
//This will give the file size
private long GetSizes(string[] strFileZ)
{
long intSizeToReturn = 0;
foreach (string a in strFileZ)
{
FileStream tmpFS = new FileStream(a, FileMode.Open);
intSizeToReturn += tmpFS.Length;
tmpFS.Close();
}
return intSizeToReturn;
}
Example to Run SplitFile function
strFileName = @"C:\backup\standards.zip;
strPathName = @"C:\backup\
lgSize = 10 * FILE_SIZE_MB
private bool SplitFile(string strFileName, string strPathName,long lgSize)
{
string strDirectory = "", strNewFileNames = "";
long FileSize = 0;
int intCounter = 0;
// Code to Check whether it is logical or not to Continue...
FSIn = new FileStream(strFileName, FileMode.Open);
BinaryReader rFSIn = new BinaryReader(FSIn);
FileSize = FSIn.Length;
if (FileSize < lgSize)
{
//MessageBox.Show("Check Sizes!!");
return false;
}
strDirectory = strPathName + FileName(strFileName);
//split it to parts in a folder Called "FileName"
if (!System.IO.Directory.Exists(strDirectory))
{
System.IO.Directory.CreateDirectory(strDirectory);
}
//begin writing
while (FSIn.Position != FSIn.Length)
{
PreDefinedCacheSize = DefineCache(8);
byte[] buffer = new byte[PreDefinedCacheSize];
strNewFileNames = strDirectory + "\\" + intCounter.ToString() + ".part";
FSout = new FileStream(strNewFileNames, FileMode.Create);
BinaryWriter wFSOut = new BinaryWriter(FSout);
while ((FSout.Position < lgSize) && (FSIn.Position != FSIn.Length))
{
if (((FSIn.Length - FSIn.Position) < Math.Min(PreDefinedCacheSize, (int)lgSize)) && (PreDefinedCacheSize > lgSize))
{
PreDefinedCacheSize = (int)FSIn.Length - (int)FSIn.Position;
rFSIn.Read(buffer, 0, PreDefinedCacheSize);
wFSOut.Write(buffer);
}
else
{
if (PreDefinedCacheSize > lgSize) PreDefinedCacheSize = (int)lgSize;
rFSIn.Read(buffer, 0, PreDefinedCacheSize);
wFSOut.Write(buffer);
}
}
wFSOut.Close();
FSout.Close();
intCounter++;
}
//finish
rFSIn.Close();
return true;
}
Example to Run MergeFile function
strDirectory = "D:\standards";
private bool MergerFile(string strDirectory)
{
string[] strFiles = Directory.GetFiles(strDirectory, "*.part");
FSout = new FileStream(strDirectory + "\\" + FileName(strDirectory), FileMode.Create);
BinaryWriter wFSOut = new BinaryWriter(FSout);
long FileSizes = 0;
FileSizes = GetSizes(strFiles);
foreach (string a in strFiles)
{
PreDefinedCacheSize = DefineCache(8);
FSIn = new FileStream(strDirectory + "\\" + FileName(a), FileMode.Open);
BinaryReader rFSIn = new BinaryReader(FSIn);
if (PreDefinedCacheSize > FSIn.Length - FSIn.Position)
PreDefinedCacheSize = (int)FSIn.Length - (int)FSIn.Position;
byte[] buffer = new byte[PreDefinedCacheSize];
while (FSIn.Position != FSIn.Length)
{
rFSIn.Read(buffer, 0, PreDefinedCacheSize);
wFSOut.Write(buffer);
}
rFSIn.Close();
FSIn.Close();
}
wFSOut.Close();
FSout.Close();
return true;
}
Ritesh Kesharwani

1 comment:

Anonymous said...

Good work .. thanks bhiya