Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.IO.Compression;
- using System.Threading;
- using System.Threading.Tasks;
- namespace GZipComressionTestApp
- {
- class Program
- {
- static void Main(string[] args)
- {
- var task = Task.Run(() => CompressFile("SkypeSetup.exe", "SkypeSetup.exe.gz"));
- task.Wait();
- Console.ReadKey();
- }
- public static async Task CompressFile(string inputFile, string outPutFile)
- {
- using (var fileStream = new FileStream(inputFile, FileMode.Open, FileAccess.Read))
- {
- var chunk = new byte[200];
- var bytesToRead = (int) fileStream.Length;
- var bytesRead = 0;
- while (bytesToRead > 0)
- {
- var numberOfReadBytes = await fileStream.ReadAsync(chunk, bytesRead, bytesToRead);
- // не понимаю как запустить задачу CompressChunk
- if(numberOfReadBytes == 0)
- break;
- bytesRead += numberOfReadBytes;
- bytesToRead -= numberOfReadBytes;
- }
- }
- }
- private static async Task CompressChunk(byte[] chunk, string outPutFile)
- {
- using (var memoryStream = new MemoryStream(chunk))
- {
- await CompressChunk(memoryStream, outPutFile);
- }
- }
- private static async Task CompressChunk(Stream memoryStream, string outPutFile)
- {
- using (var fileStream = File.Create(outPutFile))
- using (var gzipStream = new GZipStream(fileStream, CompressionMode.Compress))
- {
- await memoryStream.CopyToAsync(gzipStream);
- gzipStream.Close();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement