.NET的ZIP压缩包文件操作帮助类

使用System.IO.Compression.ZipArchive进行操作。

一、解压

public class ZipFileHelper
    {
        public static void ExtractToDirectory(string zipFile, string outPath)
        {
            if (!Regex.IsMatch(outPath, @".*[/\\]$"))
            {
                outPath += "\\";
            }
            using (FileStream fs = new FileStream(zipFile, FileMode.Open))
            {
                using (ZipArchive zr = new ZipArchive(fs))
                {
                    foreach (var en in zr.Entries)
                    {
                        FileInfo path = new FileInfo(outPath + en.FullName);
                        if (!path.Directory.Exists)
                        {
                            path.Directory.Create();
                        }
                        if (en.Name == "")
                        {
                            //是文件夹
                            Directory.CreateDirectory(outPath + en.FullName);
                        }
                        else
                        {
                            en.ExtractToFile(outPath + en.FullName, true);
                        }
                    }
                }
            }
        }
    }

你可能感兴趣的:(.net)