File splitting and merging

Source: Internet
Author: User
File splitting and merging
Public partial class form1: FORM {public form1 () {initializecomponent ();} // select the file event private void btn_selfile_click (Object sender, eventargs E) {string filter = "select file (*. *) | *. * "; openfileselection OFS = new openfileselection (filter, false); If (OFS. isfileselected () = true) {This. textbox1.text = ofs. getfilename ;}// select the directory event private void btn_seldir_click (Object sender, eventargs e) {directoryselecti On dirsel = new directoryselection (environment. specialfolder. desktop, "", true); If (dirsel. isdirectoryselected () = true) {This. textbox2.text = dirsel. getselectedpath ;}}// start to split private void btn_split_click (Object sender, eventargs e) {try {string fpath = This. textbox1.text; string splitdir = This. textbox2.text; splitfile (fpath, splitdir, 10); MessageBox. show ("split! "," Prompt ");} catch (exception ee) {MessageBox. show (EE. message, "prompt") ;}} public bool splitfile (string sourefilepath, string splitedstoredir, int filenums) {bool RBC = false; filestream FS = new filestream (sourefilepath, filemode. open); long filelength = FS. length; long fnum = (long) filenums; long filesize = filelength/fnum; For (INT I = 0; I <fnum; I ++) {int tmpfsize = (INT) filesize; string TMP Fname = (I + 1 ). tostring (); string tmpfpath = splitedstoredir + "\" + tmpfname; filestream WFS = new filestream (tmpfpath, filemode. openorcreate); byte [] array = NULL; if (I = fnum-1) {long len_pos = FS. length-fs.Position; array = new byte [len_pos]; FS. read (array, 0, (INT) len_pos); WFS. write (array, 0, (INT) len_pos);} else {array = new byte [tmpfsize]; FS. read (array, 0, tmpfsize); WFS. write (array, 0, tmpfsize);} WFS. flush (); WFS. close (); WFS. dispose (); WFS = NULL;} FS. close (); FS. dispose (); FS = NULL; return enumeration;} public bool unionfile (string splitedstoredir, string unionfilepath) {bool enumeration = false; string [] filepatharray = commonclass. getfiles (splitedstoredir, false); List <int> List = new list <int> (); foreach (string t in filepatharray) list. add (commonclass. tint (T. replace ("\", ""); L Ist. sort (); filestream WFS = new filestream (unionfilepath, filemode. append); For (INT I = 0; I <list. count; I ++) {string fp = splitedstoredir + "\" + list [I]. tostring (); filestream FS = new filestream (FP, filemode. open); byte [] dataarray = new byte [FS. length]; FS. read (dataarray, 0, (INT) FS. length); WFS. write (dataarray, 0, (INT) FS. length); // FS. close (); FS. dispose (); FS = NULL;} WFS. flush (); WFS. close (); WFS. dispose (); WFS = NULL; return rb;}// start merging private void btn_union_click (Object sender, eventargs e) {try {string splitdir = This. textbox2.text; unionfile (splitdir, splitdir + "\ union. ISO "); MessageBox. show ("merged! "," Prompt ");} catch (exception ee) {MessageBox. Show (EE. message," prompt ");}}}

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.