Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Improve speed of splitting file

I am using this code to extract a chunk from file

// info is FileInfo object pointing to file
var percentSplit = info.Length * 50 / 100; // extract 50% of file
var bytes = new byte[percentSplit];
var fileStream = File.OpenRead(fileName);
fileStream.Read(bytes, 0, bytes.Length);
fileStream.Dispose();
File.WriteAllBytes(splitName, bytes);

Is there any way to speed up this process?

Currently for a 530 MB file it takes around 4 - 5 seconds. Can this time be improved?

like image 979
Haris Hasan Avatar asked Feb 10 '13 04:02

Haris Hasan


1 Answers

There are several cases of you question, but none of them is language relevant.

Following are something to concern

  • What is the file system of source/destination file?
  • Do you want to keep original source file?
  • Are they lie on the same drive?

In c#, you almost do not have a method could be faster than File.Copy which invokes CopyFile of WINAPI internally. Because of the percentage is fifty, however, following code might not be faster. It copies whole file and then set the length of the destination file

var info=new FileInfo(fileName);
var percentSplit=info.Length*50/100; // extract 50% of file

File.Copy(info.FullName, splitName);
using(var outStream=File.OpenWrite(splitName))
    outStream.SetLength(percentSplit);

Further, if

  1. you don't keep original source after file splitted
  2. destination drive is the same as source
  3. your are not using a crypto/compression enabled file system

then, the best thing you can do, is don't copy files at all. For example, if your source file lies on FAT or FAT32 file system, what you can do is

  1. create new dir entry(entries) for newly splitted parts of file
  2. let the entry(entries) point(s) to the cluster of target part(s)
  3. set correct file size for each entry
  4. check for cross-link and avoid that

If your file system was NTFS, you might need to spend a long time to study the spec.

Good luck!

like image 75
Ken Kin Avatar answered Sep 20 '22 13:09

Ken Kin