C# ZipHelper C#公共類 -- ZipArchive實現壓縮和解壓

從網上找來個ZipArchive來壓縮和解壓縮的類,供參考吧html

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Shared;

namespace Helpers
{
    public static class ZipFileHelper

    {
        #region  Methods

        /// <summary>
        ///     建立 zip 存檔,該存檔包含指定目錄的文件和目錄。
        /// </summary>
        /// <param name="sourceDirectoryName">要存檔的目錄的路徑,指定爲相對路徑或絕對路徑。 相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="destinationArchiveFileName">要生成的存檔路徑,指定爲相對路徑或絕對路徑。 相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="compressionLevel"></param>
        /// <param name="includeBaseDirectory">壓縮包中是否包含父目錄</param>
        public static bool CreatZipFileFromDirectory(string sourceDirectoryName, string destinationArchiveFileName,
            CompressionLevel compressionLevel = CompressionLevel.NoCompression,
            bool includeBaseDirectory = true)
        {
            try
            {
                if (Directory.Exists(sourceDirectoryName)) //目錄
                    if (!File.Exists(destinationArchiveFileName))
                    {
                        ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName,
                            compressionLevel, includeBaseDirectory);
                    }
                    else
                    {
                        var toZipFileDictionaryList = GetAllDirList(sourceDirectoryName, includeBaseDirectory);

                        using (
                            var archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Update)
                        )
                        {
                            foreach (var toZipFileKey in toZipFileDictionaryList.Keys)
                                if (toZipFileKey != destinationArchiveFileName)
                                {
                                    var toZipedFileName = Path.GetFileName(toZipFileKey);
                                    var toDelArchives = new List<ZipArchiveEntry>();
                                    foreach (var zipArchiveEntry in archive.Entries)
                                        if (toZipedFileName != null &&
                                            (zipArchiveEntry.FullName.StartsWith(toZipedFileName) ||
                                             toZipedFileName.StartsWith(zipArchiveEntry.FullName)))
                                            toDelArchives.Add(zipArchiveEntry);
                                    foreach (var zipArchiveEntry in toDelArchives)
                                        zipArchiveEntry.Delete();
                                    archive.CreateEntryFromFile(toZipFileKey, toZipFileDictionaryList[toZipFileKey],
                                        compressionLevel);
                                }
                        }
                    }
                else if (File.Exists(sourceDirectoryName))
                    if (!File.Exists(destinationArchiveFileName))
                        ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName,
                            compressionLevel, false);
                    else
                        using (
                            var archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Update)
                        )
                        {
                            if (sourceDirectoryName != destinationArchiveFileName)
                            {
                                var toZipedFileName = Path.GetFileName(sourceDirectoryName);
                                var toDelArchives = new List<ZipArchiveEntry>();
                                foreach (var zipArchiveEntry in archive.Entries)
                                    if (toZipedFileName != null &&
                                        (zipArchiveEntry.FullName.StartsWith(toZipedFileName) ||
                                         toZipedFileName.StartsWith(zipArchiveEntry.FullName)))
                                        toDelArchives.Add(zipArchiveEntry);
                                foreach (var zipArchiveEntry in toDelArchives)
                                    zipArchiveEntry.Delete();
                                archive.CreateEntryFromFile(sourceDirectoryName, toZipedFileName, compressionLevel);
                            }
                        }
                else
                    return false;
                return true;
            }
            catch (Exception exception)
            {
                LogHelper.LogError("Error! ", exception);
                MessageBox.Show(exception.StackTrace, exception.Source);
                return false;
            }
        }

        /// <summary>
        ///     建立 zip 存檔,該存檔包含指定目錄的文件和目錄。
        /// </summary>
        /// <param name="sourceDirectoryName">要存檔的目錄的路徑,指定爲相對路徑或絕對路徑。 相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="destinationArchiveFileName">要生成的存檔路徑,指定爲相對路徑或絕對路徑。 相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="compressionLevel"></param>
        public static bool CreatZipFileFromDictionary(Dictionary<string, string> sourceDirectoryName,
            string destinationArchiveFileName,
            CompressionLevel compressionLevel = CompressionLevel.NoCompression)
        {
            try
            {
                using (FileStream zipToOpen = new FileStream(destinationArchiveFileName, FileMode.OpenOrCreate))
                {
                    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                    {
                        foreach (var toZipFileKey in sourceDirectoryName.Keys)
                            if (toZipFileKey != destinationArchiveFileName)
                            {
                                var toZipedFileName = Path.GetFileName(toZipFileKey);
                                var toDelArchives = new List<ZipArchiveEntry>();
                                foreach (var zipArchiveEntry in archive.Entries)
                                    if (toZipedFileName != null &&
                                        (zipArchiveEntry.FullName.StartsWith(toZipedFileName) ||
                                         toZipedFileName.StartsWith(zipArchiveEntry.FullName)))
                                        toDelArchives.Add(zipArchiveEntry);
                                foreach (var zipArchiveEntry in toDelArchives)
                                    zipArchiveEntry.Delete();
                                archive.CreateEntryFromFile(toZipFileKey, sourceDirectoryName[toZipFileKey],
                                    compressionLevel);
                            }
                    }
                }
                return true;
            }
            catch (Exception exception)
            {
                LogHelper.LogError("Error! ", exception);
                MessageBox.Show(exception.StackTrace, exception.Source);
                return false;
            }
        }

        /// <summary>
        ///     遞歸刪除文件夾目錄及文件
        /// </summary>
        /// <param name="baseDirectory"></param>
        /// <returns></returns>
        public static bool DeleteFolder(string baseDirectory)
        {
            var successed = true;
            try
            {
                if (Directory.Exists(baseDirectory)) //若是存在這個文件夾刪除之 
                {
                    foreach (var directory in Directory.GetFileSystemEntries(baseDirectory))
                        if (File.Exists(directory))
                            File.Delete(directory); //直接刪除其中的文件  
                        else
                            successed = DeleteFolder(directory); //遞歸刪除子文件夾 
                    Directory.Delete(baseDirectory); //刪除已空文件夾     
                }
            }
            catch (Exception exception)
            {
                LogHelper.LogError("Error! ", exception);
                successed = false;
            }
            return successed;
        }

        /// <summary>
        /// 調用bat刪除目錄,以防止系統底層的異步刪除機制
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        public static bool DeleteDirectoryWithCmd(string dirPath)
        {
            var process = new Process(); //string path = ...;//bat路徑  
            var processStartInfo = new ProcessStartInfo("CMD.EXE", "/C rd /S /Q \"" + dirPath + "\"")
            {
                UseShellExecute = false,
                RedirectStandardOutput = true
            }; //第二個參數爲傳入的參數,string類型以空格分隔各個參數  
            process.StartInfo = processStartInfo;
            process.Start();
            process.WaitForExit();
            var output = process.StandardOutput.ReadToEnd();
            if (string.IsNullOrWhiteSpace(output))
                return true;
            return false;
        }

        /// <summary>
        /// 調用bat刪除文件,以防止系統底層的異步刪除機制
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool DelFileWithCmd(string filePath)
        {
            var process = new Process(); //string path = ...;//bat路徑  
            var processStartInfo = new ProcessStartInfo("CMD.EXE", "/C del /F /S /Q \"" + filePath + "\"")
            {
                UseShellExecute = false,
                RedirectStandardOutput = true
            }; //第二個參數爲傳入的參數,string類型以空格分隔各個參數  
            process.StartInfo = processStartInfo;
            process.Start();
            process.WaitForExit();
            var output = process.StandardOutput.ReadToEnd();
            if (output.Contains(filePath))
                return true;
            return false;
        }

        /// <summary>
        /// 獲取目錄下全部[文件名,要壓縮的相對文件名]字典
        /// </summary>
        /// <param name="strBaseDir"></param>
        /// <param name="includeBaseDirectory"></param>
        /// <param name="namePrefix"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetAllDirList(string strBaseDir,
            bool includeBaseDirectory = false, string namePrefix = "")
        {
            var resultDictionary = new Dictionary<string, string>();
            var directoryInfo = new DirectoryInfo(strBaseDir);
            var directories = directoryInfo.GetDirectories();
            var fileInfos = directoryInfo.GetFiles();
            if (includeBaseDirectory)
                namePrefix += directoryInfo.Name + "\\";
            foreach (var directory in directories)
                resultDictionary =
                    resultDictionary.Concat(GetAllDirList(directory.FullName, true, namePrefix))
                        .ToDictionary(k => k.Key, k => k.Value); //.FullName是某個子目錄的絕對地址,
            foreach (var fileInfo in fileInfos)
                if (!resultDictionary.ContainsKey(fileInfo.FullName))
                    resultDictionary.Add(fileInfo.FullName, namePrefix + fileInfo.Name);
            return resultDictionary;
        }

        /// <summary>
        ///     Zip解壓並更新目標文件
        /// </summary>
        /// <param name="zipFilePath">Zip壓縮包路徑</param>
        /// <param name="unZipDir">解壓目標路徑</param>
        /// <returns></returns>
        public static bool UnZip(string zipFilePath, string unZipDir)
        {
            bool resualt;
            try
            {
                unZipDir = unZipDir.EndsWith(@"\") ? unZipDir : unZipDir + @"\";
                var directoryInfo = new DirectoryInfo(unZipDir);
                if (!directoryInfo.Exists)
                    directoryInfo.Create();
                var fileInfo = new FileInfo(zipFilePath);
                if (!fileInfo.Exists)
                    return false;
                using (
                    var zipToOpen = new FileStream(zipFilePath, FileMode.Open, FileAccess.ReadWrite,
                        FileShare.Read))
                {
                    using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
                    {
                        foreach (var zipArchiveEntry in archive.Entries)
                            if (!zipArchiveEntry.FullName.EndsWith("/"))
                            {
                                var entryFilePath = Regex.Replace(zipArchiveEntry.FullName.Replace("/", @"\"),
                                    @"^\\*", "");
                                var filePath = directoryInfo + entryFilePath; //設置解壓路徑
                                var content = new byte[zipArchiveEntry.Length];
                                zipArchiveEntry.Open().Read(content, 0, content.Length);

                                if (File.Exists(filePath) && content.Length == new FileInfo(filePath).Length)
                                    continue; //跳過相同的文件,不然覆蓋更新

                                var sameDirectoryNameFilePath = new DirectoryInfo(filePath);
                                if (sameDirectoryNameFilePath.Exists)
                                {
                                    sameDirectoryNameFilePath.Delete(true);
                                    DeleteDirectoryWithCmd(filePath);
                                    /*if (!DeleteDirectoryWithCmd(filePath))
                                    {
                                        Console.WriteLine(filePath + "刪除失敗");
                                        resualt = false;
                                        break;
                                    }*/
                                }
                                var sameFileNameFilePath = new FileInfo(filePath);
                                if (sameFileNameFilePath.Exists)
                                {
                                    sameFileNameFilePath.Delete();
                                    DelFileWithCmd(filePath);
                                    /*if (!DelFileWithCmd(filePath))
                                    {
                                        Console.WriteLine(filePath + "刪除失敗");
                                        resualt = false;
                                        break;
                                    }*/
                                }
                                var greatFolder = Directory.GetParent(filePath);
                                if (!greatFolder.Exists) greatFolder.Create();
                                File.WriteAllBytes(filePath, content);
                            }
                    }
                }
                resualt = true;
            }
            catch
                (Exception exception)
            {
                LogHelper.LogError("Error! ", exception);
                resualt = false;
            }
                return resualt;
        }

        #endregion
    }
}

 

出處:https://www.cnblogs.com/Chary/p/No0000DF.html異步

==============================================ide

本身在項目中的使用:spa

using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows.Forms;



namespace Car.AutoUpdate.Comm
{
    public class ZipArchiveHelper
    {

        #region  Methods

        /// <summary>
        /// 建立 zip 存檔,該文檔包含指定目錄的文件和子目錄。
        /// </summary>
        /// <param name="sourceDirectoryName">將要壓縮存檔的文件目錄的路徑,能夠爲相對路徑或絕對路徑。 相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="destinationArchiveFileName">將要生成的壓縮包的存檔路徑,能夠爲相對路徑或絕對路徑。相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="compressionLevel">指示壓縮操做是強調速度仍是強調壓縮大小的枚舉值</param>
        /// <param name="includeBaseDirectory">壓縮包中是否包含父目錄</param>
        public static bool CreatZip(string sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel = CompressionLevel.NoCompression, bool includeBaseDirectory = true)
        {
            try
            {
                if (Directory.Exists(sourceDirectoryName))
                    if (!File.Exists(destinationArchiveFileName))
                    {
                        ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName, compressionLevel, includeBaseDirectory);
                    }
                    else
                    {
                        var toZipFileDictionaryList = GetAllDirList(sourceDirectoryName, includeBaseDirectory);
                        using (var archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Update))
                        {
                            foreach (var toZipFileKey in toZipFileDictionaryList.Keys)
                                if (toZipFileKey != destinationArchiveFileName)
                                {
                                    var toZipedFileName = Path.GetFileName(toZipFileKey);
                                    var toDelArchives = new List<ZipArchiveEntry>();
                                    foreach (var zipArchiveEntry in archive.Entries)
                                        if (toZipedFileName != null && (zipArchiveEntry.FullName.StartsWith(toZipedFileName) || toZipedFileName.StartsWith(zipArchiveEntry.FullName)))
                                            toDelArchives.Add(zipArchiveEntry);
                                    foreach (var zipArchiveEntry in toDelArchives)
                                        zipArchiveEntry.Delete();
                                    archive.CreateEntryFromFile(toZipFileKey, toZipFileDictionaryList[toZipFileKey], compressionLevel);
                                }
                        }
                    }
                else if (File.Exists(sourceDirectoryName))
                    if (!File.Exists(destinationArchiveFileName))
                        ZipFile.CreateFromDirectory(sourceDirectoryName, destinationArchiveFileName, compressionLevel, false);
                    else
                        using (var archive = ZipFile.Open(destinationArchiveFileName, ZipArchiveMode.Update))
                        {
                            if (sourceDirectoryName != destinationArchiveFileName)
                            {
                                var toZipedFileName = Path.GetFileName(sourceDirectoryName);
                                var toDelArchives = new List<ZipArchiveEntry>();
                                foreach (var zipArchiveEntry in archive.Entries)
                                    if (toZipedFileName != null && (zipArchiveEntry.FullName.StartsWith(toZipedFileName) || toZipedFileName.StartsWith(zipArchiveEntry.FullName)))
                                        toDelArchives.Add(zipArchiveEntry);
                                foreach (var zipArchiveEntry in toDelArchives)
                                    zipArchiveEntry.Delete();
                                archive.CreateEntryFromFile(sourceDirectoryName, toZipedFileName, compressionLevel);
                            }
                        }
                else
                    return false;
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error("Error! ", ex);
                //MessageBox.Show(ex.StackTrace, ex.Source);
                UtilityHelper.ShowMessageDialog(ex.StackTrace);
                return false;
            }
        }

        /// <summary>
        /// 建立 zip 存檔,該存檔包含指定目錄的文件和目錄。
        /// </summary>
        /// <param name="sourceDirectoryName">將要壓縮存檔的文件目錄的路徑,能夠爲相對路徑或絕對路徑。 相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="destinationArchiveFileName">將要生成的壓縮包的存檔路徑,能夠爲相對路徑或絕對路徑。 相對路徑是指相對於當前工做目錄的路徑。</param>
        /// <param name="compressionLevel">指示壓縮操做是強調速度仍是強調壓縮大小的枚舉值</param>
        public static bool CreatZip(Dictionary<string, string> sourceDirectoryName, string destinationArchiveFileName, CompressionLevel compressionLevel = CompressionLevel.NoCompression)
        {
            try
            {
                using (FileStream zipToOpen = new FileStream(destinationArchiveFileName, FileMode.OpenOrCreate))
                {
                    using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
                    {
                        foreach (var toZipFileKey in sourceDirectoryName.Keys)
                            if (toZipFileKey != destinationArchiveFileName)
                            {
                                var toZipedFileName = Path.GetFileName(toZipFileKey);
                                var toDelArchives = new List<ZipArchiveEntry>();
                                foreach (var zipArchiveEntry in archive.Entries)
                                    if (toZipedFileName != null && (zipArchiveEntry.FullName.StartsWith(toZipedFileName) || toZipedFileName.StartsWith(zipArchiveEntry.FullName)))
                                        toDelArchives.Add(zipArchiveEntry);
                                foreach (var zipArchiveEntry in toDelArchives)
                                    zipArchiveEntry.Delete();
                                archive.CreateEntryFromFile(toZipFileKey, sourceDirectoryName[toZipFileKey], compressionLevel);
                            }
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.Error("Error! ", ex);
                UtilityHelper.ShowMessageDialog(ex.StackTrace);
                return false;
            }
        }

        /// <summary>
        ///     遞歸刪除磁盤上的指定文件夾目錄及文件
        /// </summary>
        /// <param name="baseDirectory"></param>
        /// <returns></returns>
        private static bool DeleteFolder(string baseDirectory)
        {
            var successed = true;
            try
            {
                if (Directory.Exists(baseDirectory)) //若是存在這個文件夾刪除之 
                {
                    foreach (var directory in Directory.GetFileSystemEntries(baseDirectory))
                        if (File.Exists(directory))
                            File.Delete(directory); //直接刪除其中的文件  
                        else
                            successed = DeleteFolder(directory); //遞歸刪除子文件夾 
                    Directory.Delete(baseDirectory); //刪除已空文件夾     
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("Error! ", ex);
                successed = false;
            }
            return successed;
        }

        /// <summary>
        /// 調用CMD,命令行刪除磁盤上的指定目錄,以防止系統底層的異步刪除機制
        /// </summary>
        /// <param name="dirPath"></param>
        /// <returns></returns>
        private static bool DeleteDirectoryWithCmd(string dirPath)
        {
            var process = new Process();
            //ProcessStartInfo構造方法的第二個參數是執行第一個命令的所須要傳入的參數,以空格分隔各個參數
            var processStartInfo = new ProcessStartInfo("CMD.EXE", "/C rd /S /Q \"" + dirPath + "\"") { UseShellExecute = false, RedirectStandardOutput = true };
            process.StartInfo = processStartInfo;
            process.Start();
            process.WaitForExit();
            var output = process.StandardOutput.ReadToEnd();
            if (string.IsNullOrWhiteSpace(output))
                return true;
            return false;
        }

        /// <summary>
        /// 調用CMD,命令行刪除磁盤上的指定目錄,以防止系統底層的異步刪除機制
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        private static bool DelFileWithCmd(string filePath)
        {
            var process = new Process();
            //ProcessStartInfo構造方法的第二個參數是執行第一個命令的所須要傳入的參數,以空格分隔各個參數
            var processStartInfo = new ProcessStartInfo("CMD.EXE", "/C del /F /S /Q \"" + filePath + "\"") { UseShellExecute = false, RedirectStandardOutput = true };
            process.StartInfo = processStartInfo;
            process.Start();
            process.WaitForExit();
            var output = process.StandardOutput.ReadToEnd();
            if (output.Contains(filePath))
                return true;
            return false;
        }

        /// <summary>
        /// 遞歸獲取磁盤上的指定目錄下全部文件的集合,返回類型是:字典[文件名,要壓縮的相對文件名]
        /// </summary>
        /// <param name="strBaseDir"></param>
        /// <param name="includeBaseDirectory"></param>
        /// <param name="namePrefix"></param>
        /// <returns></returns>
        private static Dictionary<string, string> GetAllDirList(string strBaseDir, bool includeBaseDirectory = false, string namePrefix = "")
        {
            var resultDictionary = new Dictionary<string, string>();
            var directoryInfo = new DirectoryInfo(strBaseDir);
            var directories = directoryInfo.GetDirectories();
            var fileInfos = directoryInfo.GetFiles();
            if (includeBaseDirectory)
                namePrefix += directoryInfo.Name + "\\";
            foreach (var directory in directories)
                resultDictionary = resultDictionary.Concat(GetAllDirList(directory.FullName, true, namePrefix)).ToDictionary(k => k.Key, k => k.Value); //FullName是某個子目錄的絕對地址,
            foreach (var fileInfo in fileInfos)
                if (!resultDictionary.ContainsKey(fileInfo.FullName))
                    resultDictionary.Add(fileInfo.FullName, namePrefix + fileInfo.Name);
            return resultDictionary;
        }

        /// <summary>
        /// 解壓Zip文件,並覆蓋保存到指定的目標路徑文件夾下
        /// </summary>
        /// <param name="zipFilePath">將要解壓縮的zip文件的路徑</param>
        /// <param name="unZipDir">解壓後將zip中的文件存儲到磁盤的目標路徑</param>
        /// <returns></returns>
        public static bool UnZip(string zipFilePath, string unZipDir)
        {
            bool resualt;
            try
            {
                unZipDir = unZipDir.EndsWith(@"\") ? unZipDir : unZipDir + @"\";
                var directoryInfo = new DirectoryInfo(unZipDir);
                if (!directoryInfo.Exists)
                    directoryInfo.Create();
                var fileInfo = new FileInfo(zipFilePath);
                if (!fileInfo.Exists)
                    return false;
                using (var zipToOpen = new FileStream(zipFilePath, FileMode.Open, FileAccess.ReadWrite, FileShare.Read))
                {
                    using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
                    {
                        foreach (var zipArchiveEntry in archive.Entries)
                            if (!zipArchiveEntry.FullName.EndsWith("/"))
                            {
                                var entryFilePath = Regex.Replace(zipArchiveEntry.FullName.Replace("/", @"\"), @"^\\*", "");
                                var filePath = directoryInfo + entryFilePath; //設置解壓路徑
                                var content = new byte[zipArchiveEntry.Length];
                                zipArchiveEntry.Open().Read(content, 0, content.Length);

                                // mq 2019-06-14: 註銷下面的代碼,所有覆蓋現有文件
                                //if (File.Exists(filePath) && content.Length == new FileInfo(filePath).Length)
                                //{
                                //    continue; //跳過相同的文件,不然覆蓋更新
                                //}
                                //var sameDirectoryNameFilePath = new DirectoryInfo(filePath);
                                //if (sameDirectoryNameFilePath.Exists)
                                //{
                                //    sameDirectoryNameFilePath.Delete(true);
                                //    DeleteDirectoryWithCmd(filePath);
                                //}
                                //var sameFileNameFilePath = new FileInfo(filePath);
                                //if (sameFileNameFilePath.Exists)
                                //{
                                //    sameFileNameFilePath.Delete();
                                //    DelFileWithCmd(filePath);
                                //    /*if (!DelFileWithCmd(filePath))
                                //    {
                                //        Console.WriteLine(filePath + "刪除失敗");
                                //        resualt = false;
                                //        break;
                                //    }*/
                                //}

                                var greatFolder = Directory.GetParent(filePath);
                                if (!greatFolder.Exists)
                                    greatFolder.Create();
                                File.WriteAllBytes(filePath, content);
                            }
                    }
                }
                resualt = true;
            }
            catch (Exception ex)
            {
                LogHelper.Error("Error! ", ex);
                resualt = false;
            }
            return resualt;
        }

        /// <summary>
        /// 獲取Zip壓縮包中的文件列表
        /// </summary>
        /// <param name="zipFilePath">Zip壓縮包文件的物理路徑</param>
        /// <returns></returns>
        public static List<string> GetZipFileList(string zipFilePath)
        {
            List<string> fList = new List<string>();
            if (!File.Exists(zipFilePath))
                return fList;
            try
            {
                using (var zipToOpen = new FileStream(zipFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    using (var archive = new ZipArchive(zipToOpen, ZipArchiveMode.Read))
                    {
                        foreach (var zipArchiveEntry in archive.Entries)
                            if (!zipArchiveEntry.FullName.EndsWith("/"))
                                fList.Add(Regex.Replace(zipArchiveEntry.FullName.Replace("/", @"\"), @"^\\*", ""));
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("Error! ", ex);
            }
            return fList;
        }


        #endregion
    }
}
View Code
相關文章
相關標籤/搜索