• 0

[C#] Merge and split files in directory


Question

Need to loop through a directory and merge a series of binary files like this > File001, File002, File003 etc.

 

I'm using the following code but need to count how many are in the folder then concat them:



using (var fs = File.OpenWrite(output))
{
    var buffer = File.ReadAllBytes(file1);
    fs.Write(buffer, 0, buffer.Length);
    buffer = File.ReadAllBytes(file2);
    fs.Write(buffer, 0, buffer.Length);
    buffer = File.ReadAllBytes(file3);
    fs.Write(buffer, 0, buffer.Length);
    buffer = File.ReadAllBytes(file4);
    fs.Write(buffer, 0, buffer.Length);
    buffer = File.ReadAllBytes(file5);
    fs.Write(buffer, 0, buffer.Length);
    fs.Flush();
}


Also need to split files in 1Mb or 3Mb chunks like this > File001, File002, File003.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

Directory.GetFiles() gets you a string array containing all the file names in the specified directory. Its length is the number of files.

 

To split in chunks of a specific size, well that's the third parameter to the FileStream.Write method you're using. Keep track of how many bytes you've written and open new files as needed.

Link to comment
Share on other sites

  • 0

Directory.GetFiles() gets you a string array containing all the file names in the specified directory. Its length is the number of files.

 

To split in chunks of a specific size, well that's the third parameter to the FileStream.Write method you're using. Keep track of how many bytes you've written and open new files as needed.

How would the code for that look like? I don't want all files, just ones with filename00*

foreach (var file in Directory.GetFiles(folderBrowserDialog1.SelectedPath, "*.*"))
    {
	var buffer = File.ReadAllBytes(file);
	fs.Write(buffer, 0, buffer.Length);
    }
Link to comment
Share on other sites

  • 0

What does the final code look like? Can't seem to get it working...

 

EDIT:

using (var fs = File.OpenWrite(output))
{
    foreach (var file in Directory.GetFiles(folderBrowserDialog1.SelectedPath).Where(path => Path.GetFileName(path).StartsWith("File00")))
    {
	var buffer = File.ReadAllBytes(file);
	fs.Write(buffer, 0, buffer.Length);
    }
    fs.Flush();
}

What should I be doing at var buffer? Doesn't seem to find/do anything.

 

EDIT2: Never mind it's working, had some old code that was stopping it from working.

Link to comment
Share on other sites

  • 0

splitting is easy.

Here is the pseudo code

set fileName to be test.exe

set newFile to be test.exe.part
set index to be 0
set length to be 1024
set offset to be 0

set byteBuffer to a new Byte array of length size.

open fileName for binary input

while ((readDataLen = fileName.read(byteBuffer,offset,length) > 0) //we got data

set offset to be offset + readDataLen
open (newFile + index+=1) for binary output
write byteBuffer into the newFile starting at 0, going readDataLen long
close the newFile
go back to beginning of while loop (assuming readDataLen > 0, otherwise go to next line)
close the fileName

Done.

Link to comment
Share on other sites

  • 0

I'm using this converted from vb:

ChopItUp(openFileDialog1.FileName, 1048576);

public int ChopItUp(string inputFile, int chunkSize)
{

    FileStream reader = new FileStream(inputFile, FileMode.Open);
    int fileIndex = 0;
    byte[] buffer = new byte[chunkSize + 1];
    FileStream writer = default(FileStream);
    int bytesRead = 0;

    do
    {
	bytesRead = reader.Read(buffer, 0, chunkSize);
	if (bytesRead > 0)
	{
	    writer = new FileStream(Path.GetDirectoryName(openFileDialog1.FileName) + @"\File000" + fileIndex, FileMode.Create);
	    writer.Write(buffer, 0, bytesRead);
	    writer.Close();
	    fileIndex = fileIndex + 1;
	}
    } while (bytesRead > 0);

    reader.Close();

    return fileIndex - 1;
}
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.