-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUnXAPK.cs
106 lines (93 loc) · 3.43 KB
/
UnXAPK.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System.IO.Compression;
class UnXAPK
{
public static async Task UnXAPKMain(string[] args)
{
string rootDirectory = Directory.GetCurrentDirectory();
string downloadPath = Path.Combine(rootDirectory, "Downloads", "XAPK");
if (!Directory.Exists(downloadPath))
{
Console.WriteLine("XAPK 下載目錄不存在,請先下載 XAPK 檔案。");
return;
}
try
{
// 解壓縮目的資料夾 (Unzip)
// 建議只用 downloadPath 再加 "Unzip",不需重複合併 rootDirectory
string extractionPath = Path.Combine(downloadPath, "Unzip");
if (!Directory.Exists(extractionPath))
{
Directory.CreateDirectory(extractionPath);
}
// 尋找第一個 .xapk 檔案
string xapkFile = Directory
.GetFiles(downloadPath, "*.xapk", SearchOption.TopDirectoryOnly)
.FirstOrDefault();
if (xapkFile == null)
{
Console.WriteLine("找不到任何 .xapk 檔案。");
return;
}
// 解壓縮 XAPK
if (await UnpackZip(xapkFile, extractionPath))
{
// 若 XAPK 解壓後出現 .apk,繼續解壓
foreach (var apkFile in Directory.GetFiles(extractionPath, "*.apk", SearchOption.TopDirectoryOnly))
{
if (!await UnpackZip(apkFile, extractionPath))
{
Console.WriteLine($"Error unpacking apk file: {apkFile}");
return;
}
}
}
else
{
Console.WriteLine("Error unpacking xapk file");
return;
}
Console.WriteLine("APK extracted successfully!");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
await Hex.HexMain(args);
}
private static async Task<bool> UnpackZip(string zipFile, string extractionPath)
{
try
{
Directory.CreateDirectory(extractionPath);
using (ZipArchive archive = ZipFile.OpenRead(zipFile))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
string entryFullName = Path.Combine(extractionPath, entry.FullName);
string entryDirectory = Path.GetDirectoryName(entryFullName);
if (string.IsNullOrEmpty(entryDirectory))
continue; // 若 entry 是純資料夾 (空字串) 就略過
if (!Directory.Exists(entryDirectory))
Directory.CreateDirectory(entryDirectory);
if (File.Exists(entryFullName))
{
Console.WriteLine($"Skipped: {entryFullName} already exists.");
continue;
}
entry.ExtractToFile(entryFullName);
}
}
return true;
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
return false;
}
finally
{
// 即使這裡用不上 async-await 實際非同步IO,但保留方法簽名以方便流程統一
await Task.CompletedTask;
}
}
}