alexmitev

CompressDecompressSplitAndAssemble

Dec 21st, 2015
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Collections.Generic;
  8. using System.IO.Compression;
  9.  
  10.  
  11.  
  12. namespace SplitFileGzip
  13. {
  14.  
  15.  
  16.     class SplitWithGzip
  17.     {
  18.         static void AssembleAndDecompress(List<string> files, string destinationDir, string extension)
  19.         {
  20.             int bufferSize = 4096 * 2;
  21.  
  22.  
  23.             FileStream outputStream = new FileStream(string.Format("{0}assembled{1}", destinationDir, extension), FileMode.Append);
  24.             using (outputStream)
  25.             {
  26.                 int index = 0;
  27.                 while (index < files.Count)
  28.                 {
  29.                     using (var inputStreams = new FileStream(files[index], FileMode.Open))
  30.                     {
  31.                         using (var decompressionStream = new GZipStream(inputStreams, CompressionMode.Decompress, false))
  32.                         {
  33.                             byte[] buffer = new byte[bufferSize];
  34.                             while (true)
  35.                             {
  36.                                 int bytes = decompressionStream.Read(buffer, 0, buffer.Length);
  37.                                 if (bytes == 0)
  38.                                 {
  39.                                     break;
  40.                                 }
  41.                                 outputStream.Write(buffer, 0, bytes);
  42.                             }
  43.                         }
  44.                     }
  45.                     index++;
  46.                 }
  47.             }
  48.         }
  49.         static void SliceAndCompress(string sourceFile, string destinationDir, int parts)
  50.         {
  51.             FileStream inputStream = new FileStream(sourceFile, FileMode.Open);
  52.             using (inputStream)
  53.             {
  54.                int bufferSize = 4096 * 2;
  55.                 string extension = ".gz";
  56.                 int index = 1;
  57.                 long partSize = (long)Math.Ceiling((double)inputStream.Length / parts);
  58.                 while (inputStream.Position < inputStream.Length)
  59.                 {
  60.                     using (var outputStream = new FileStream(string.Format("{0}part{1}{2}", destinationDir, index, extension), FileMode.Create))
  61.                     {
  62.                         using (var compressionStream = new GZipStream(outputStream, CompressionMode.Compress, false))
  63.                         {
  64.                             byte[] buffer = new byte[bufferSize];
  65.                             long bytesRead = 0;
  66.                             while (bytesRead < partSize)
  67.                             {
  68.                                 int bytes = inputStream.Read(buffer, 0, buffer.Length);
  69.                                 if (bytes == 0)
  70.                                 {
  71.                                     break;
  72.                                 }
  73.                                 compressionStream.Write(buffer, 0, bytes);
  74.                                 bytesRead += bytes;
  75.                             }
  76.                         }
  77.                     }
  78.                     index++;
  79.                 }
  80.             }
  81.         }
  82.  
  83.         static void Main()
  84.         {
  85.             string sourceFile = @"../../Video.mp4";
  86.             string resultDirectory = @"../../Result/";
  87.             string extension = Path.GetExtension(sourceFile);
  88.             //Slice(sourceFile, resultDirectory, 3);
  89.             List<string> compressedFiles = Directory.GetFiles(resultDirectory, "*.gz").ToList();
  90.             AssembleAndDecompress(compressedFiles, resultDirectory, extension);
  91.  
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment