• 代码演示用 .NET 4.5 (C# 5.0)自带的压缩类 ZipArchive 创建一个压缩文件


    代码如下:

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.IO.Compression;
    using System.Linq;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace SolutionZip
    {
        class SolutionZip
        {
            static void Main(string[] args)
            {
                string rootFolder = ".\";
                string archiveName = "Archive.zip";
    
                if (args.Length > 0)
                {
                    rootFolder = args[0];
                }
                if (args.Length > 1)
                {
                    archiveName = args[1];
                }
                List<string> exceptions = new List<string>();
                exceptions.Add(@".user");
                exceptions.Add(@".suo");
                exceptions.Add(@"in");
                exceptions.Add(@"obj");
                exceptions.Add(@"packages");
    
                int filesAdded = CreateArchive(rootFolder, 
                    exceptions, archiveName);
                Console.WriteLine(String.Format(" {0} file(s) added ", 
                    filesAdded));
                Console.ReadLine();
            }
    
            public static int CreateArchive(string folder, 
                    IList<string> exceptions, string archiveName)
            {
                int filesCount = 0;
                string folderFullPath = Path.GetFullPath(folder);
                string archivePath = Path.Combine(folderFullPath, archiveName);
                if (File.Exists(archivePath))
                {
                    Console.WriteLine(
                        string.Format(@"File '{0}' already exists. Overwrite (y/n): ", 
                            archiveName));
                    string read = Console.ReadLine();
                    if (read.ToLower() == "y")
                    {
                        File.Delete(archivePath);
                    }
                    else
                    {
                        Console.WriteLine(string.Format(@"Archive {0} already exists. 
                            Aborting!", archivePath));
                        return 0;
                    }
                }
                IEnumerable<string> files = Directory.EnumerateFiles(folder,
                        "*.*", SearchOption.AllDirectories);
                using (ZipArchive archive = ZipFile.Open(archivePath, ZipArchiveMode.Create))
                {                
                    foreach (string file in files)
                    {
                        if (!Excluded(file, exceptions))
                        {
                            try
                            {
                                var addFile = Path.GetFullPath(file);
                                if (addFile != archivePath)
                                {
                                    addFile = addFile.Substring(folderFullPath.Length);
                                    Console.WriteLine("Adding " + addFile);
                                    archive.CreateEntryFromFile(file, addFile);
                                    filesCount++;
                                }
                            }
                            catch (IOException ex)
                            {
                                Console.WriteLine(@"Failed to add {0} due to error : 
                                {1} 
     Ignoring it!", file, ex.Message);
                            }
                        }
                    }
                }
                return filesCount;
            }
    
            private static bool Excluded(string file, IList<string> exceptions)
            {
                List<String> folderNames = (from folder in exceptions
                                            where folder.StartsWith(@"") 
                                                || folder.StartsWith(@"/")
                                            select folder).ToList<string>();
                if (!exceptions.Contains(Path.GetExtension(file)))
                {
                    foreach (string folderException in folderNames)
                    {
                        if(Path.GetDirectoryName(file).Contains(folderException))
                        {
                            return true;
                        }
                    }
                    return false;
                }
                return true;
            }
        }
    }

    图文解释地址:http://www.dotnetcurry.com/showarticle.aspx?ID=974

    谢谢浏览!

  • 相关阅读:
    react项目如何调试?
    react使用引入svg的icon;svg图形制作
    react使用echarts
    SQL SEVER数据库重建索引的方法
    ThreadLocal的内存泄露
    ThreadLocal是否会引发内存泄露的分析 good
    深入分析 ThreadLocal 内存泄漏问题
    清理ThreadLocal
    线程池 Threadlocal 使用注意
    ThreadLocal对象使用过程中容易陷入的坑
  • 原文地址:https://www.cnblogs.com/Music/p/zip-archive-in-dotnet_45.html
Copyright © 2020-2023  润新知