VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 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");
            }           
        }
    }
}


相关教程