private void gSTART_Click(object sender, EventArgs e) { //Lock GUI gGTFO.Enabled = false; gSTART.Enabled = false; ginf.Text = "Starting"; Application.DoEvents(); //Read options SplitOptions v = new SplitOptions(); v.infile = gin.Text; v.basefn = gbase.Text; v.batch = gbatch.Checked; if (gopt1.Checked) { v.nparts = parsevar(gopt1v.Text); } if (gopt2.Checked) { v.sizelim = parsevar(gopt2v.Text); } //Start in separate thread (LEAVE GUI ALONE ;_;) System.Threading.Thread th = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(split)); th.IsBackground = true; th.Start(v); tGUI.Start(); }
void split(object vo) { //Fetch arguments SplitOptions v = (SplitOptions)vo; //Read info from GUI string path = v.infile.Substring(0, v.infile.LastIndexOf("\\") + 1); //Open file in read-only mode FileStream sin = new FileStream( v.infile, FileMode.Open, FileAccess.Read); //Find segment length total = sin.Length; long size = total; if (v.nparts >= 0) { double count = Convert.ToDouble(v.nparts); size = (long)Math.Ceiling((double)size / count); } else { size = v.sizelim; } //Here goes int seg = 1; long segpos = 0; FileStream fout = null; while (true) { //Determine bytes left in input file remain = sin.Length - sin.Position; if (remain <= 0) { break; //gtfo my loop. } if (fout == null) { //No segment open. Let's fix that. fout = new FileStream(v.basefn + "." + seg.ToString("d3"), FileMode.Create); segpos = 0; seg++; } //Limit chunk size to lowest value //between segment size limit and 8MB long chunk = Math.Min(size - segpos, BF); //Limit chunk size to remaining data chunk = Math.Min(remain, chunk); //Create buffer, copy data byte[] buffer = new byte[chunk]; sin.Read(buffer, 0, buffer.Length); fout.Write(buffer, 0, buffer.Length); //Segment full yet? segpos += buffer.Length; if (segpos >= size) { //Close seg closefs(fout); fout = null; } } closefs(fout); closefs(sin); //Write script? if (v.batch) { //This is where I stopped caring //about how my code looks. F**k yeah. System.IO.File.WriteAllText("merge.bat", "@echo off" + "\r\n" + "echo ." + "\r\n" + "echo . Merging files, please wait." + "\r\n" + "echo ." + "\r\n" + "copy /B " + v.basefn + ".* " + v.basefn + "" + "\r\n" + "echo ." + "\r\n" + "echo . Finished!" + "\r\n" + "echo ." + "\r\n" + "pause"); } }