Advertisement
Guest User

Untitled

a guest
May 22nd, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. class SlicingFile
  4. {
  5.     static void Main()
  6.     {
  7.         Slice("hubble.jpg", @"\..", 10);
  8.     }
  9.     static void Slice(string sourceFile, string destinationDirectory, int parts)
  10.     {
  11.         byte[] buffer = new byte[4096];
  12.         int indexRead = 0;
  13.         long fileSize = new FileInfo(sourceFile).Length;
  14.         string extension = new FileInfo(sourceFile).Extension;
  15.         int pieceSize = (int)Math.Ceiling((decimal)fileSize / parts);
  16.         using (FileStream reader = new FileStream(sourceFile, FileMode.Open))
  17.         {
  18.             string destinationFileName = "Part-1" + extension;
  19.             using (FileStream writer = new FileStream(destinationFileName, FileMode.Append))
  20.             {
  21.                 for (int part = 0; part < parts; part++)
  22.                 {
  23.                     int indexWrite = 0;
  24.                     long currentPieceSize = 0;
  25.                     while (currentPieceSize < pieceSize)
  26.                     {
  27.                         // read piece
  28.                         int bytesRead = reader.Read(buffer, indexRead, buffer.Length);
  29.                         if (bytesRead == 0) break;
  30.                         // write piece
  31.                         writer.Write(buffer, indexWrite, bytesRead);
  32.                         indexRead += bytesRead;
  33.                         indexWrite += bytesRead;
  34.                         currentPieceSize += bytesRead;
  35.                     }
  36.                     destinationFileName = string.Format("Part-{0}", part + 2) + extension;
  37.                 }
  38.             }
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement