c# - How to split file into parts and download -


i'm working on split downloader c#. downloading fine (so logic working) problem whatever file downloads corrupts. have no idea on how fix it. here's code:

private void mergeclean()     {         const int chunksize = 1 * 1024; // 2kb         using (var output = file.create("output.jpg"))         {             foreach (var file in files)             {                 using (var input = file.openread(file))                 {                     var buffer = new byte[chunksize];                     int bytesread;                     while ((bytesread = input.read(buffer, 0, buffer.length)) > 0)                     {                         output.write(buffer, 0, bytesread);                     }                 }             }         }          foreach (var file in files)         {             file.delete(file);         }     }      private void savefilestream(string path, stream stream)     {         var filestream = new filestream(path, filemode.create, fileaccess.write);         stream.copyto(filestream);         filestream.dispose();     }      public void splitdownload(string url)     {         system.net.webrequest req = system.net.httpwebrequest.create(url);         req.method = "head";         system.net.webresponse resp = req.getresponse();         var responselength = double.parse(resp.headers.get("content-length"));         var partsize = math.ceiling(responselength / 10);         var previous = 0;          (int = (int)partsize; <= responselength; = + (int)partsize)         {             thread t = new thread(() => download(url, previous, i));             t.start();             t.join();             previous = i;         }          mergeclean();     }      private void download(string url, int start, int end)     {         console.writeline(string.format("{0},{1}", start, end));          httpwebrequest myhttpwebrequest = (httpwebrequest)webrequest.create(url);         myhttpwebrequest.addrange(start, end);         httpwebresponse myhttpwebresponse = (httpwebresponse)myhttpwebrequest.getresponse();         stream streamresponse = myhttpwebresponse.getresponsestream();         string name = generatetempname();         savefilestream(name, streamresponse);         files.add(name);     } 

here example of does:

original downloaded

updated code:

static string generatetempname(int start)     {         string name = string.format("{0:d6}.tmp", start);         return name;     }      static public list<string> files = new list<string>();      static private void mergeclean()     {         files.sort();         const int chunksize = 1 * 1024; // 2kb         using (var output = file.create("output.jpg"))         {             foreach (var file in files)             {                 using (var input = file.openread(file))                 {                     var buffer = new byte[chunksize];                     int bytesread;                     while ((bytesread = input.read(buffer, 0, buffer.length)) > 0)                     {                         output.write(buffer, 0, bytesread);                     }                 }             }         }          foreach (var file in files)         {             file.delete(file);         }     } 

you need recombine file pieces in correct order - current code create random file names , if items added list of files added in random order due unpredictable time when segment download finishes.

possible fix: use block start offset part of file name string name = string.format("file{0:d6}.tmp", start) , sort files name before combining them back.

note {0:d6} formatting used pad index 0 allow sorting name easier , avoid need natural sort code.


Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -