C# GZip Compress DeCompress

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Compression;
using System.IO;
using System.Diagnostics;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            DecompressDemo();
            Console.ReadLine();
        }                
        
        private static void CompressDemo()
        {
            string source = @"D:\";
            string[] files = Directory.GetFiles(source, "*", SearchOption.AllDirectories);
            if(files!=null && files.Any())
            {
                foreach(var file in files)
                {
                    CompressFile(file);
                }
            }                              
        }
        private static void CompressFile(string file)
        {
            Console.WriteLine($"Start to compress {file}");
            Stopwatch sw = new Stopwatch();
            sw.Start();
            string compressedFullName = Path.Combine(Path.GetDirectoryName(file), Path.GetFileNameWithoutExtension(file)) + ".zip";
            if (File.Exists(compressedFullName))
            {
                File.Delete(compressedFullName);
            }

            Task compressTask = Task.Run(() =>
            {
                using (FileStream fs = File.Open(file, FileMode.Open))
                {
                    using (FileStream compressedFileStream = File.Create(compressedFullName))
                    {
                        using (GZipStream gZipStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                        {
                            fs.CopyTo(gZipStream);
                        }
                    }
                }
            });

            compressTask.Wait();
            sw.Stop();
            if (compressTask.IsCompleted)
            {
                Console.WriteLine($"{file} has been compressed successfully,cost:{sw.ElapsedMilliseconds} ElapsedMilliseconds!");
            }
        }
        private static void DecompressDemo()
        {
            string source = @"D:\";
            string[] compressedFiles = Directory.GetFiles(source, "*.zip");
            if (compressedFiles != null && compressedFiles.Any())
            {
                foreach (var file in compressedFiles)
                {
                    DecompressFile(file);
                }
            }
        }
        private static void DecompressFile(string file)
        {
            Console.WriteLine($"Start to decompress {file}");
            Stopwatch decompressSW = new Stopwatch();
            decompressSW.Start();

            string newFileName = Path.Combine(Path.GetDirectoryName(file)) + Guid.NewGuid().ToString() + Path.GetFileNameWithoutExtension(file);

            Task decompressTask = Task.Run(() =>
            {                
                using (FileStream fs = File.Open(file, FileMode.Open))
                {
                    using (FileStream decompressedStream = File.Create(newFileName))
                    {
                        using (GZipStream gzipdecompressedStream = new GZipStream(fs, CompressionMode.Decompress))
                        {
                            gzipdecompressedStream.CopyTo(decompressedStream);
                        }
                    }
                }
            });

            decompressTask.Wait();
            if(decompressTask.IsCompleted)
            {
                decompressSW.Stop();
                Console.WriteLine($"Decompress as {newFileName} cost {decompressSW.ElapsedMilliseconds} milliseconds");
            }           
        }
    }
}

 

posted @ 2019-12-23 14:54  FredGrit  阅读(765)  评论(0编辑  收藏  举报