C # merge large files

Source: Internet
Author: User
Public static bool splitfile (string sourcefile, int nnooffiles)
{
Bool split = false;

Try
{

Filestream FS = new filestream (sourcefile, filemode. Open, fileaccess. Read );
Int sizeofeachfile = (INT) math. Ceiling (double) fs. Length/nnooffiles );

For (INT I = 0; I <= nnooffiles; I ++)
{
String basefilename = path. getfilenamewithoutextension (sourcefile );
String extension = path. getextension (sourcefile );

Filestream outputfile = new filestream (path. getdirectoryname (sourcefile) + "\" + basefilename + "." +
I. tostring (). padleft (5, convert. tochar ("0") + extension + ". tmp", filemode. Create, fileaccess. Write );

Int bytesread = 0;
Byte [] buffer = new byte [sizeofeachfile];

If (bytesread = FS. Read (buffer, 0, sizeofeachfile)> 0)
{
Outputfile. Write (buffer, 0, bytesread );
}

Outputfile. Close ();

}
FS. Close ();
}
Catch (exception ex)
{
Throw ex;
}

Return split;
}

Public static bool mergefile (string inputfilefolder)
{
Bool output = false;

Try
{
String [] tmpfiles = directory. getfiles (inputfilefolder, "*. tmp ");

Filestream outputfile = NULL;
String prevfilename = "";

Foreach (string tempfile in tmpfiles)
{
String filename = path. getfilenamewithoutextension (tempfile );
String basefilename = filename. substring (0, filename. indexof (convert. tochar (".")));
String extension = path. getextension (filename );

If (! Prevfilename. Equals (basefilename ))
{
If (outputfile! = NULL)
{
Outputfile. Flush ();
Outputfile. Close ();
}
Outputfile = new filestream (inputfilefolder + "\" + basefilename + extension, filemode. openorcreate, fileaccess. Write );

}

Int bytesread = 0;
Byte [] buffer = new byte [1024];
Filestream inputtempfile = new filestream (tempfile, filemode. openorcreate, fileaccess. Read );

While (bytesread = inputtempfile. Read (buffer, 0, 1024)> 0)
Outputfile. Write (buffer, 0, bytesread );

Inputtempfile. Close ();
File. Delete (tempfile );
Prevfilename = basefilename;

}

Outputfile. Close ();
}
Catch (exception ex)
{
Throw ex;
}

Return output;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.