C#创建超过260字符的文件夹并生成ZIP压缩包

  1. 添加NuGet程序包
    AlphaFS
    ICsharpCode.SharpZipLib.dll
  2. Code
    using System;
    using System.Collections.Generic;
    using Alphaleonis.Win32.Filesystem;
    using ICSharpCode.SharpZipLib.Checksum;
    using ICSharpCode.SharpZipLib.Zip;
    
    namespace ConsoleApp1
    {
         class Program
        {
            
            static void Main(string[] args)
            {
                try
                {
                    string baseName = "测试";
                    string path = @"C:\";
                    int i = 0;
                    while (path.Length < 350)
                    {
                        path += string.Format(@"\{0}", baseName + i.ToString().PadLeft(4, '0'));
                        i++;
                    }
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
    
                    path += "\\testa.txt";
                    if (!File.Exists(path))
                    {
                        File.AppendAllText(path, "this is test file");
                    }
                    ZipDirectory("C:\\测试0000", "C:\\", "123", false);
                }
                catch (Exception exp)
                {
                    Console.WriteLine(exp.ToString());
                    Console.ReadLine();
                }
            }
    
    
    
            /// <summary>
            /// ZIP:压缩文件夹
            /// </summary>
            /// <param name="DirectoryToZip">需要压缩的文件夹(绝对路径)</param>
            /// <param name="ZipedPath">压缩后的文件路径(绝对路径)</param>
            /// <param name="ZipedFileName">压缩后的文件名称(文件名,默认 同源文件夹同名)</param>
            /// <param name="IsEncrypt">是否加密(默认 加密)</param>
            public static void ZipDirectory(string DirectoryToZip, string ZipedPath, string ZipedFileName = "", bool IsEncrypt = true)
            {
                //如果目录不存在,则报错
                if (!System.IO.Directory.Exists(DirectoryToZip))
                {
                    throw new System.IO.FileNotFoundException("指定的目录: " + DirectoryToZip + " 不存在!");
                }
    
                //文件名称(默认同源文件名称相同)
                string ZipFileName = string.IsNullOrEmpty(ZipedFileName) ? ZipedPath + "\\" + new DirectoryInfo(DirectoryToZip).Name + ".zip" : ZipedPath + "\\" + ZipedFileName + ".zip";
    
                using (System.IO.FileStream ZipFile = System.IO.File.Create(ZipFileName))
                {
                    using (ZipOutputStream s = new ZipOutputStream(ZipFile))
                    {
                        if (IsEncrypt)
                        {
                            //压缩文件加密
                            s.Password = "123";
                        }
                        ZipSetp(DirectoryToZip, s, "");
                    }
                }
            }
    
            /// <summary>
            /// 递归遍历目录
            /// </summary>
            private static void ZipSetp(string strDirectory, ZipOutputStream s, string parentPath)
            {
                if (strDirectory[strDirectory.Length - 1] != Path.DirectorySeparatorChar)
                {
                    strDirectory += Path.DirectorySeparatorChar;
                }
                Crc32 crc = new Crc32();
    
                string[] filenames = Directory.GetFileSystemEntries(strDirectory);
    
                foreach (string file in filenames)// 遍历所有的文件和目录
                {
    
                    if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                    {
                        string pPath = parentPath;
                        pPath += file.Substring(file.LastIndexOf("\\") + 1);
                        pPath += "\\";
                        ZipSetp(file, s, pPath);
                    }
    
                    else // 否则直接压缩文件
                    {
                        //打开压缩文件
                        using (System.IO.FileStream fs = File.OpenRead(file))
                        {
    
                            byte[] buffer = new byte[fs.Length];
                            fs.Read(buffer, 0, buffer.Length);
    
                            string fileName = parentPath + file.Substring(file.LastIndexOf("\\") + 1);
                            ZipEntry entry = new ZipEntry(fileName);
    
                            entry.DateTime = DateTime.Now;
                            entry.Size = fs.Length;
    
                            fs.Close();
    
                            crc.Reset();
                            crc.Update(buffer);
    
                            entry.Crc = crc.Value;
                            s.PutNextEntry(entry);
    
                            s.Write(buffer, 0, buffer.Length);
                        }
                    }
                }
            }
    
        }
    }

     

posted @ 2021-03-29 09:10  来瓶冰镇可乐吧  阅读(194)  评论(0编辑  收藏  举报