By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,056 Members | 1,296 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,056 IT Pros & Developers. It's quick & easy.

Concatenate numerous large binary or text files into one

P: n/a
Hi,
I would like to find out the fastest way to concatenate large (200 or
300MB) and numerous (500 - 700) files (Postscript files) into a single
one (which can ends up being several GigaB) using csharp within a
windows form application.
One of the requirement also is to make sure the output file is closed
only when the concatenation is done so that the next application using
the file (printer spool) does not assume it is finished unledd it is
actually finished.

Thanks in advance for your help.

Sep 30 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
ff*****@gmail.com wrote:
I would like to find out the fastest way to concatenate large (200 or
300MB) and numerous (500 - 700) files (Postscript files) into a single
one (which can ends up being several GigaB) using csharp within a
windows form application.
One of the requirement also is to make sure the output file is closed
only when the concatenation is done so that the next application using
the file (printer spool) does not assume it is finished unledd it is
actually finished.
Efficient reading and writing of files should just be a
matter of using huge buffers.

I would be more worried about whether PDF files
can just be concatenated.

Arne
Sep 30 '06 #2

P: n/a


<ff*****@gmail.comwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi,
I would like to find out the fastest way to concatenate large (200 or
300MB) and numerous (500 - 700) files (Postscript files) into a single
one (which can ends up being several GigaB) using csharp within a
windows form application.
One of the requirement also is to make sure the output file is closed
only when the concatenation is done so that the next application using
the file (printer spool) does not assume it is finished unledd it is
actually finished.
Well, you don't really need the fastest way. You need a reasonably fast
way. Just use System.IO.FileStream, open one for your output file, and
iterate over your input files. For each input file, read a buffer from the
input file, write the buffer to the output file, repeat.

You don't need to use huge buffers, and it's probably not worth while to use
async IO or double buffering or any of that.

Here's a simple file concatenation program:

David

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace FileConcat
{
class Program
{
static int Main(string[] args)
{
if (args.Length != 2)
{
Console.WriteLine("usage FileConcat [SourceDir] [DestinationFile]");
}
try
{
Run(args[0],args[1]);
return 0;
}
catch (Exception ex)
{
Console.WriteLine(ex);
return 1;
}
}
static void Run(string SourceDir, string OutputFileName)
{
string[] inputFiles = Directory.GetFiles(SourceDir);

int bufSize = 1024 * 64;

byte[] buf = new byte[bufSize];

using (FileStream outFile =
new FileStream(OutputFileName, FileMode.OpenOrCreate,
FileAccess.Write, FileShare.None, bufSize))
{
foreach (string inputFile in inputFiles)
{
using (FileStream inFile =
new FileStream(inputFile, FileMode.Open, FileAccess.Read,
FileShare.Read, bufSize))
{
int br = 0;
while ((br = inFile.Read(buf,0,buf.Length))0)
{
outFile.Write(buf,0,br);
}
}
}
}

}
}
}
Sep 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.