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

How to Zip/Unzip files using C#.NET

How to Zip/Unzip files using C#.NET
Following example works only with Visual Studio .NET 2005 Framework 2.0
Namespace
using System.IO.Compression;
using System.IO;
Example data to run ZipFile function:
sourceFile = @"D:\Ritesh\standards.pdf"
destinationFile = @"C:\backup\standards.zip"
Private bool ZipFile(string sourceFile, string destinationFile)
{
using (FileStream oldFile = File.OpenRead(sourceFile)
using (FileStream newFile = File.Create(destinationFile)
using (GZipStream compression = new GZipStream(newFile, CompressionMode.Compress))
{
byte[] buffer = new byte[1024];
int numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
while (numberOfBytesRead > 0)
{
compression.Write(buffer, 0, numberOfBytesRead);
numberOfBytesRead = oldFile.Read(buffer, 0, buffer.Length);
}
compression.Close();
}
}
Example data to run UnZipFile function :
sourceFile = @"C:\backup\standards.zip
destinationFile = @"C:\backup\standards.pdf"
Private bool UnZipFile(string sourceFile, string destinationFile)
{
using(FileStream compressFile = File.Open(sourceFile,FileMode.Open))
using (FileStream uncompressedFile = File.Create(destinationFile)
using (GZipStream compression = new GZipStream(compressFile,
CompressionMode.Decompress))
{
int data = compression.ReadByte();
while(data != -1)
{
uncompressedFile.WriteByte((byte) data);
data = compression.ReadByte();
}
compression.Close();
}
}
Another way to Zip/Unzip files using Shell32.DLL in C#.NET
First you need to download Shell32.dll, you can download this DLL from following location.
Namespace
using Shell32;
using System.IO;
Example data to run ZipFile function:
sourceFolder = @"D:\New Folder\"
destinationFile = @"C:\backup\test.zip"
Private bool ZipFile(string sourceFolder, string destinationFile)
{
byte[] emptyzip = new byte[]{80,75,5,6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
FileStream fs = File.Create(@"C:\backup\test.zip");
fs.Write(emptyzip, 0, emptyzip.Length);
fs.Flush();
fs.Close();
fs = null;
//Copy a folder and its contents into the newly created zip file
Shell32.ShellClass sc = new Shell32.ShellClass();
Shell32.Folder SrcFlder = sc.NameSpace(sourceFolder)
Shell32.Folder DestFlder = sc.NameSpace(destinationFile)
Shell32.FolderItems items = SrcFlder.Items();
DestFlder.CopyHere(items, 20);
}
Example data to run UnZipFile function :
inputFileName = @"C:\backup\standards.zip
destinationPath = @"C:\backup\"
private string UnzipFile(string inputFileName, string destinationPath)
{
Shell shell = new ShellClass();
Folder sourceFolder = shell.NameSpace(inputFileName);
Folder destinationFolder = shell.NameSpace(destinationPath);
string outputFileName = sourceFolder.Items().Item(0).Name;
destinationFolder.CopyHere(sourceFolder.Items(),"");
return outputFileName;
}

Ritesh Kumar Kesharwani