Code:
public static void ExportFile(ref BackgroundWorker exportWorker, string dataXXX_path, string outPath, int offset, int length)
{
if (File.Exists(dataXXX_path))
{
using (FileStream dataXXX_stream = new FileStream(dataXXX_path, FileMode.Open, FileAccess.Read))
{
dataXXX_stream.Position = offset;
byte[] fileBytes = new byte[length];
dataXXX_stream.Read(fileBytes, 0, length);
if (XOR.Encrypted(Path.GetExtension(outPath).Remove(0, 1)))
{
byte b = 0;
XOR.Cipher(ref fileBytes, ref b);
}
if (File.Exists(outPath)) { File.Delete(outPath); }
else { MessageBox.Show(string.Format("Cannot locate:\n{0}", outPath), "Data000 ExportFile Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
using (BinaryWriter bw = new BinaryWriter(File.Create(outPath), Encoding.Default))
{
//Set the chunkSize to 2% of the total bytes in the file
int chunkSize = Math.Max(64000, (int)(length * .02));
//Set maximum of 'ProgressBar'
exportWorker.ReportProgress(99, length);
//Foreach byteCount if the count is below the fileLength
for (int byteCount = 0; byteCount < length; byteCount += Math.Min(length - byteCount, chunkSize))
{
bw.Write(fileBytes, byteCount, Math.Min(length - byteCount, chunkSize));
exportWorker.ReportProgress(100, byteCount);
}
}
}
}
else { MessageBox.Show(string.Format("Cannot locate:\n{0}", dataXXX_path), "Data000 ExportFile Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}