欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# GZip Compress DeCompress

程序员文章站 2022-06-20 08:45:25
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO.Compression; using... ......
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");
            }           
        }
    }
}