Advertisement
Guest User

05. Slicing File

a guest
May 22nd, 2015
523
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5.  
  6. internal class SlicingFile
  7. {
  8.     private static List<string> files = new List<string>();
  9.     private static MatchCollection matches;
  10.     private static void Main()
  11.     {
  12.         string inputFile = @"../../Rex.avi";
  13.         string folderPath = @"../../";
  14.         int NumberOfParts = 4;
  15.  
  16.         Slice(inputFile, folderPath, NumberOfParts);
  17.  
  18.         Assemble(files, folderPath);
  19.     }
  20.  
  21.     private static void Assemble(List<string> files, string destinationDirectory)
  22.     {
  23.         // creating the file path for the reconstructed file
  24.         string fileOutputPath = destinationDirectory + "assembled" + "." + matches[0].Groups[2];
  25.         var fsSource = new FileStream(fileOutputPath, FileMode.Create);
  26.  
  27.         fsSource.Close();
  28.         using (fsSource = new FileStream(fileOutputPath, FileMode.Append))
  29.         {
  30.             // reading the file paths of the parts from the files list
  31.             foreach (var filePart in files)
  32.             {
  33.                 using (var partSource = new FileStream(filePart, FileMode.Open))
  34.                 {
  35.                     // Create a byte array of the content of the current file
  36.                     Byte[] bytePart = new byte[partSource.Length];
  37.                     int readBytes = partSource.Read(bytePart, 0, bytePart.Length);
  38.                     // Write the bytes to the reconstructed file
  39.                     fsSource.Write(bytePart, 0, readBytes);
  40.                 }
  41.             }
  42.         }
  43.     }
  44.  
  45.     private static void Slice(string sourceFile, string destinationDirectory, int parts)
  46.     {
  47.         using (var source = new FileStream(sourceFile, FileMode.Open))
  48.         {
  49.             long partSize = (long) Math.Ceiling((double) source.Length/parts);
  50.  
  51.             // The offset at which to start reading from the source file
  52.             long fileOffset = 0;
  53. ;
  54.             string currPartPath;
  55.             FileStream fsPart;
  56.             long sizeRemaining = source.Length;
  57.  
  58.             // extracting name and extension of the input file
  59.             string pattern = @"(\w+)(?=\.)\.(?<=\.)(\w+)";
  60.             Regex pairs = new Regex(pattern);
  61.             matches = pairs.Matches(sourceFile);
  62.             for (int i = 0; i < parts; i++)
  63.             {
  64.                 currPartPath = destinationDirectory + matches[0].Groups[1] + String.Format(@"-{0}", i) + "." + matches[0].Groups[2];
  65.                 files.Add(currPartPath);
  66.  
  67.                 // reading one part size
  68.                 using (fsPart = new FileStream(currPartPath, FileMode.Create))
  69.                 {
  70.                     byte[] buffer = new byte[partSize];
  71.  
  72.                     int readBytes = source.Read(buffer, 0, buffer.Length);
  73.  
  74.                     // creating one part size file
  75.                     fsPart.Write(buffer, 0, readBytes);
  76.                 }
  77.  
  78.                 // calculating the remaining file size which iis still too be read
  79.                 sizeRemaining = (int) source.Length - (i*partSize);
  80.                 if (sizeRemaining < partSize)
  81.                 {
  82.                     partSize = sizeRemaining;
  83.                 }
  84.                 fileOffset += partSize;
  85.             }
  86.         }
  87.     }
  88.  
  89.     public static Microsoft.Win32.SafeHandles.SafeFileHandle ImagePath { get; set; }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement