Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public void CreateFile(string path)
  2. {
  3. Task<byte[]> headerTask = CreateHeaderAsync();
  4. Task<byte[]> filesTask = CombineFilesAsync();
  5. byte[] header = headerTask.Result;
  6. byte[] files = filesTask.Result;
  7.  
  8. byte[] combined = new byte[header.Length + files.Length];
  9. Buffer.BlockCopy(header, 0, combined, 0, header.Length);
  10. Buffer.BlockCopy(files, 0, combined, header.Length, files.Length);
  11.  
  12. Task.Factory.StartNew(() => File.WriteAllBytes(path, combined));
  13. }
  14.  
  15. private Task<byte[]> CreateHeaderAsync()
  16. {
  17. return Task.Factory.StartNew(() =>
  18. {
  19. StringBuilder sb = new StringBuilder();
  20. int position = 0;
  21. foreach (ByteFile file in _files)
  22. {
  23. sb.Append(file + "?" + position + Environment.NewLine);
  24. position += file.Length;
  25. }
  26. return Encoding.UTF8.GetBytes(sb + "--header_end--" + Environment.NewLine);
  27. });
  28. }
  29.  
  30. private Task<byte[]> CombineFilesAsync()
  31. {
  32. return Task.Factory.StartNew(() =>
  33. {
  34. ByteFile[] arrays = _files.ToArray();
  35.  
  36. byte[] rv = new byte[arrays.Sum(a => a.Length)];
  37. int offset = 0;
  38. foreach (ByteFile t in arrays)
  39. {
  40. var array = Encryption.EncryptBytes(t.Content, "password");
  41. var array = t.Content;
  42.  
  43. Buffer.BlockCopy(array, 0, rv, offset, array.Length);
  44. offset += array.Length;
  45. }
  46. return rv;
  47. });
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement