Ejemplo n.º 1
0
 public void WhenCompressThreadsDone(string filename, List <Task <byte[]> > threads, long ticks)
 {
     Task.WhenAll(threads).ContinueWith(param =>
     {
         try
         {
             var list   = new byte[threads.Sum(x => x.Result.Length)];
             var offset = 0;
             threads.ForEach(x => { Buffer.BlockCopy(x.Result, 0, list, offset, x.Result.Length); offset += x.Result.Length; });
             File.WriteAllBytes(filename, list);
             GC.Collect();
             CompressionDoneEventHandler?.Invoke(true, $"Success!\nElapsed time - {ElapsedTimeInSeconds(ticks, DateTime.Now.Ticks)} seconds.");
         }
         catch (Exception ex)
         {
             CompressionDoneEventHandler?.Invoke(true, ex.Message);
         }
     });
 }
Ejemplo n.º 2
0
        public void Decompress(string fileName)
        {
            var start = DateTime.Now.Ticks;
            var file  = File.ReadAllBytes(fileName);

            fileName = fileName.Remove(fileName.Length - 3, 3);
            var chunks = file.SearchBytePattern(gzipHeader);

            ThreadsChanged?.Invoke(chunks.Count);
            if (chunks.Count == 0 || chunks[0] != 0)
            {
                CompressionDoneEventHandler?.Invoke(true, "File is not an archive, or damaged!");
                return;
            }
            var threads = new List <Task <byte[]> >();
            var offset  = 0;

            try
            {
                if (chunks.Count == 1)
                {
                    DecompressThreadsAdd(threads, 0, file.Length, file);
                }
                else
                {
                    for (int i = 1; i < chunks.Count; i++)
                    {
                        var iter = i;
                        offset = chunks[iter - 1];
                        DecompressThreadsAdd(threads, offset, chunks[iter] - offset, file);
                    }
                    DecompressThreadsAdd(threads, chunks.LastOrDefault(), file.Length - chunks.LastOrDefault() - 1, file);
                }
                threads.ForEach(x => x.Start());
                WhenCompressThreadsDone(fileName, threads, start);
            }
            catch (Exception ex)
            {
                CompressionDoneEventHandler?.Invoke(true, ex.Message);
            }
        }