| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Diagnostics; |
| 4 | using System.IO; |
| 5 | using System.IO.Compression; |
| 6 | using System.Linq; |
| 7 | using System.Text; |
| 8 | using System.Threading.Tasks; |
| 9 | |
| 10 | namespace Updater |
| 11 | { |
| 12 | public class Unzip |
| 13 | { |
| 14 | ///<summary> |
| 15 | /// |
| 16 | ///</summary> |
| 17 | static readonly string[] IgnoreFiles = { }; |
| 18 | public static bool ExtractZipFile(string zipPath, string extractPath) |
| 19 | { |
| 20 | try |
| 21 | { |
| 22 | if (!extractPath.EndsWith(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)) |
| 23 | extractPath += Path.DirectorySeparatorChar; |
| 24 | |
| 25 | using (ZipArchive archive = ZipFile.OpenRead(zipPath)) |
| 26 | { |
| 27 | foreach (ZipArchiveEntry entry in archive.Entries) |
| 28 | { |
| 29 | |
| 30 | if (!IsIgnoreFile(entry.FullName)) |
| 31 | { |
| 32 | |
| 33 | string destinationPath = Path.GetFullPath(Path.Combine(extractPath, entry.FullName)); |
| 34 | if (!IsDir(destinationPath)) |
| 35 | { |
| 36 | |
| 37 | string dir = Path.GetDirectoryName(destinationPath); |
| 38 | if (!Directory.Exists(dir)) |
| 39 | { |
| 40 | Directory.CreateDirectory(dir); |
| 41 | } |
| 42 | |
| 43 | if (File.Exists(destinationPath)) |
| 44 | { |
| 45 | File.Delete(destinationPath); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | Debug.WriteLine($"抽取:{destinationPath}"); |
| 50 | if (destinationPath.StartsWith(extractPath, StringComparison.Ordinal)) |
| 51 | entry.ExtractToFile(destinationPath); |
| 52 | } |
| 53 | else |
| 54 | { |
| 55 | |
| 56 | Directory.CreateDirectory(destinationPath); |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | } |
| 63 | catch |
| 64 | { |
| 65 | return false; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | ///<summary> |
| 70 | /// |
| 71 | ///</summary> |
| 72 | ///<param name="fileName"></param> |
| 73 | ///<returns></returns> |
| 74 | static bool IsIgnoreFile(string fileName) |
| 75 | { |
| 76 | return (Array.IndexOf(IgnoreFiles, fileName) != -1); |
| 77 | } |
| 78 | ///<summary> |
| 79 | /// |
| 80 | ///</summary> |
| 81 | ///<param name="path"></param> |
| 82 | ///<returns></returns> |
| 83 | static bool IsDir(string path) |
| 84 | { |
| 85 | return (path.Last() == '\\'); |
| 86 | } |
| 87 | } |
| 88 | } |