Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
598
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.88 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.IO.Compression;
  4. using System.Threading;
  5. using System.Threading.Tasks;
  6.  
  7. namespace GZipComressionTestApp
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var task = Task.Run(() => CompressFile("SkypeSetup.exe", "SkypeSetup.exe.gz"));
  14.             task.Wait();
  15.  
  16.             Console.ReadKey();
  17.         }
  18.  
  19.         public static async Task CompressFile(string inputFile, string outPutFile)
  20.         {
  21.             using (var fileStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
  22.             {
  23.                 var chunk = new byte[200];
  24.  
  25.                 var bytesToRead = (int) fileStream.Length;
  26.                 var bytesRead = 0;
  27.  
  28.                 while (bytesToRead > 0)
  29.                 {
  30.                     var numberOfReadBytes = await fileStream.ReadAsync(chunk, bytesRead, bytesToRead);
  31.                     // не понимаю как запустить задачу CompressChunk
  32.  
  33.                     if(numberOfReadBytes == 0)
  34.                         break;
  35.  
  36.                     bytesRead += numberOfReadBytes;
  37.                     bytesToRead -= numberOfReadBytes;
  38.  
  39.                 }
  40.             }
  41.         }
  42.  
  43.         private static async Task CompressChunk(byte[] chunk, string outPutFile)
  44.         {
  45.             using (var memoryStream = new MemoryStream(chunk))
  46.             {
  47.                 await CompressChunk(memoryStream, outPutFile);
  48.             }
  49.         }
  50.  
  51.         private static async Task CompressChunk(Stream memoryStream, string outPutFile)
  52.         {
  53.             using (var fileStream = File.Create(outPutFile))
  54.             using (var gzipStream = new GZipStream(fileStream, CompressionMode.Compress))
  55.             {
  56.                 await memoryStream.CopyToAsync(gzipStream);
  57.                 gzipStream.Close();
  58.             }
  59.         }
  60.  
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement