跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

Unzip.cs

88 行 · 3.1 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.IO;
5using System.IO.Compression;
6using System.Linq;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace 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 // Gets the full path to ensure that relative segments are removed.
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 // Ordinal match is safest, case-sensitive volumes can be mounted within volumes that
48 // are case-insensitive.
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}