zoukankan      html  css  js  c++  java
  • 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");
                }           
            }
        }
    }
  • 相关阅读:
    sql 查询服务器硬盘剩余空间
    SQL语句导致cpu占用如此高
    (@WhiteTaken)Unity中Invoke的用法
    (@WhiteTaken)设计模式学习——抽象工厂模式
    (@WhiteTaken)设计模式学习——工厂方法模式
    (@WhiteTaken)设计模式学习——简单工厂模式
    c#中的泛型委托(@WhiteTaken)
    c#中@的作用
    lua读书笔记(@WhiteTaken)
    Unity中的预制的Attributes
  • 原文地址:https://www.cnblogs.com/Fred1987/p/12083760.html
Copyright © 2011-2022 走看看