Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.64 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. class SlicingFile
  6. {
  7.     static string ext;
  8.     static void Main()
  9.     {
  10.         Slice("hubble.jpg", @"\..", 10);
  11.  
  12.         List<string> files = new List<string>();
  13.         string destinationDirectory = Directory.GetCurrentDirectory();
  14.         string[] allFiles = Directory.GetFiles(destinationDirectory);
  15.         for (int i = 0; i < allFiles.Length; i++)
  16.         {
  17.             if (allFiles[i].Contains("Part-") && allFiles[i].Contains(ext))
  18.             {
  19.                 files.Add(allFiles[i]);
  20.             }    
  21.         }
  22.         Assemble(files, destinationDirectory);
  23.     }
  24.     static void Slice(string sourceFile, string destinationDirectory, int parts)
  25.     {
  26.         byte[] buffer = new byte[4096];
  27.         long fileSize = new FileInfo(sourceFile).Length;
  28.         string extension = new FileInfo(sourceFile).Extension;
  29.         ext = extension;
  30.         int pieceSize = (int)Math.Ceiling((decimal)fileSize / parts);
  31.         using (FileStream reader = new FileStream(sourceFile, FileMode.Open))
  32.         {
  33.             for (int part = 0; part < parts; part++)
  34.             {
  35.                 string destinationFileName = string.Format("Part-{0}", part + 1) + extension;
  36.                 using (FileStream writer = new FileStream(destinationFileName, FileMode.Append))
  37.                 {
  38.                     long currentPieceSize = 0;
  39.                     while (currentPieceSize < pieceSize)
  40.                     {
  41.                         // read piece
  42.                         int bytesRead = reader.Read(buffer, 0, buffer.Length);
  43.                         if (bytesRead == 0) break;
  44.                         // write piece
  45.                         writer.Write(buffer, 0, bytesRead);
  46.                         currentPieceSize += bytesRead;
  47.                     }
  48.                 }
  49.             }
  50.         }
  51.     }
  52.     static void Assemble(List<string> files, string destinationDirectory)
  53.     {
  54.         byte[] buffer = new byte[4096];
  55.         using (FileStream writer = new FileStream(("Assembled" + ext), FileMode.Append))
  56.         {
  57.             foreach (var sourceFile in files)
  58.             {
  59.                 using (FileStream reader = new FileStream(sourceFile, FileMode.Open))
  60.                 {
  61.                     while (true)
  62.                     {
  63.                         // read piece
  64.                         int bytesRead = reader.Read(buffer, 0, buffer.Length);
  65.                         if (bytesRead == 0) break;
  66.                         // write piece
  67.                         writer.Write(buffer, 0, bytesRead);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement