Click here to Skip to main content
15,885,933 members
Articles / Programming Languages / C#

File Splitter Utility in C# - WinForms

Rate me:
Please Sign up or sign in to vote.
4.53/5 (19 votes)
25 Sep 20033 min read 109.2K   6.2K   47  
Simple File Splitter / Joiner utility that demonstrates FCL and Winforms UI processing
using System;
using System.IO;

namespace CedarLogic
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class FileSplitter2
	{
		private static long m_segmentSize = 600 * 1024 * 1024;
		private static int blockSize = 1024;
		
		public int SplitFile(string fileName)
		{
			FileStream fsIn = new FileStream(fileName, FileMode.Open, FileAccess.Read);

			string basePath = System.IO.Path.GetDirectoryName(fileName);

			int fileSize = (int) fsIn.Length;
			int segments = (int) (fileSize / m_segmentSize);
			int remainder = (int) (fileSize % m_segmentSize);

			if (remainder > 0) segments++;

			byte[] buffer = new byte[blockSize];

			for (int i = 0; i < segments; i++)
			{
				FileStream fsOut = new FileStream(fileName + "." + i, FileMode.Create,FileAccess.Write);
				int blocks = (int) (m_segmentSize / blockSize);

				if ( remainder > 0 && i < segments - 1)
				{
					for ( int j = 0; j < blocks ; j++)
					{
						//probably should just check the int ret value then write those blocks
						// then if the ret value < 1024, just write the remainder & exit
						fsIn.Read(buffer, 0, blockSize);
						fsOut.Write(buffer, 0, blockSize);
					}
				}
				else
				{
					int finalBlocks = (int) (remainder / blockSize);
					int lastBlock = (int) (remainder % blockSize);
					for (int k = 0; k < finalBlocks; k++)
					{
						fsIn.Read(buffer, 0, blockSize);
						fsOut.Write(buffer,0,blockSize);
					}
					fsIn.Read(buffer, 0, lastBlock);
					fsOut.Write(buffer, 0, lastBlock);
				}
				fsOut.Flush();
				fsOut.Close();
			}
			fsIn.Close();

			return (int) segments;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I have over 15 years of experience in the Application Development. Unfortunately, I still remember punch cards from College and VisiCalc on the Apple II.

My recent experience (about 6 years) covers the 2 main camps in distributed computing: J2EE based and COM[+] / .NET.

Lately, it's been deep .NET, C#, ASP.NET and the rest of the .NET Framework.

I've been working on Internet related technologies since 1993, initially writing Perl scripts under the original NCSA Http server.

Comments and Discussions