-
Notifications
You must be signed in to change notification settings - Fork 2
/
Class34_Zip.cs
339 lines (292 loc) · 10.8 KB
/
Class34_Zip.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using Data;
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Threading;
using System.ComponentModel;
using System.Windows.Forms;
internal class Class34_Zip
{
string _baseromPath = Environment.GetEnvironmentVariable("TEMP");
string _zipPath = "";
int CurrentFileIndex = 0;
private Class10_settings Class10_0;
bool CanLoadC10 = false;
frmUnZIP frmUnZIP_0;
public Class34_Zip()
{
}
public void AddClass10(ref Class10_settings Class10_1)
{
Class10_0 = Class10_1;
CanLoadC10 = true;
}
public void ExtractZip()
{
/*while (File.Exists(_zipPath))
{
CurrentFileIndex++;
SetZipPath();
}*/
try
{
SetZipPath();
int Chechh = GetPackgeChecksum();
if (!CanLoadC10 || (CanLoadC10 && Chechh != this.Class10_0.LastPackageChecksum))
{
if (!File.Exists(_zipPath)) File.Create(_zipPath).Dispose();
File.WriteAllBytes(_zipPath, Properties.Resources.Packages);
if (CanLoadC10) this.Class10_0.LastPackageChecksum = Chechh;
}
//UnZip(_baseromPath);
}
catch
{
this.Class10_0.frmMain_0.LogThis("Unzipper - Unable to Overwrite Package.zip:");
}
}
private int GetPackgeChecksum()
{
int ReturnSum = -1;
if (File.Exists(_zipPath))
{
byte[] AllBytes = File.ReadAllBytes(_zipPath);
for (int i = 0; i < AllBytes.Length; i++)
{
ReturnSum += AllBytes[i];
if (ReturnSum > 65535) ReturnSum -= 65535;
}
}
return ReturnSum;
}
void SetZipPath()
{
_zipPath = Environment.GetEnvironmentVariable("TEMP") + @"\Packages" + CurrentFileIndex + ".zip";
}
public void RemoveZip()
{
try
{
while (File.Exists(_zipPath))
{
File.Delete(_zipPath);
if (CurrentFileIndex > 0) CurrentFileIndex--;
SetZipPath();
}
}
catch { }
}
public Shell32.Folder GetShell32NameSpaceFolder(Object folder)
{
Type shellAppType = Type.GetTypeFromProgID("Shell.Application");
Object shell = Activator.CreateInstance(shellAppType);
return (Shell32.Folder)shellAppType.InvokeMember("NameSpace",
System.Reflection.BindingFlags.InvokeMethod, null, shell, new object[] { folder });
}
public void UnZipFile(string folderPath)
{
try
{
if (!File.Exists(_zipPath))
{
throw new FileNotFoundException();
}
if (!Directory.Exists(folderPath)) Directory.CreateDirectory(folderPath);
Shell32.Folder destinationFolder = GetShell32NameSpaceFolder(folderPath);
Shell32.Folder sourceFile = GetShell32NameSpaceFolder(_zipPath);
foreach (var file in sourceFile.Items())
{
destinationFolder.CopyHere(file, 4 | 16);
}
}
catch { }
}
public void UnZipFile(string folderPath, string Filename)
{
try
{
if (!File.Exists(_zipPath))
{
throw new FileNotFoundException();
}
if (!Directory.Exists(folderPath + @"\" + Filename)) Directory.CreateDirectory(folderPath + @"\" + Filename);
if (CanLoadC10)
{
frmUnZIP_0 = new frmUnZIP();
frmUnZIP_0.Show();
frmUnZIP_0.TopMost = true;
}
Shell32.Folder destinationFolder = GetShell32NameSpaceFolder(folderPath);
Shell32.Folder sourceFile = GetShell32NameSpaceFolder(_zipPath);
bool FoundFile = false;
int MaxItems = sourceFile.Items().Count;
int CurrentIndex = 0;
foreach (Shell32.FolderItem file in sourceFile.Items())
{
int Percent = (int) (((double) CurrentIndex * 100.0) / (double) MaxItems);
if (CanLoadC10) frmUnZIP_0.SetPercent(Percent);
if (file.Name != null)
{
if (file.Name != string.Empty && Filename != string.Empty)
{
if (file.Name == Filename)
{
destinationFolder.CopyHere(file, 4 | 16);
FoundFile = true;
}
}
}
CurrentIndex++;
}
if (!FoundFile) throw new FileNotFoundException();
}
catch { }
if (CanLoadC10)
{
if (frmUnZIP_0 != null)
{
frmUnZIP_0.Dispose();
frmUnZIP_0 = null;
}
}
}
public byte[] UnZipSilent(string FilenameInZIP, string FnameExtracted)
{
byte[] ReturningByte = new byte[] { };
string BufFilePath = Application.StartupPath + @"\" + FilenameInZIP + @"\" + FnameExtracted;
//Unzip First if file do no exist
if (!File.Exists(BufFilePath)) UnZipFile(Application.StartupPath, FilenameInZIP);
//Get Datas
if (File.Exists(BufFilePath))
{
ReturningByte = File.ReadAllBytes(BufFilePath);
}
else
{
Console.WriteLine("NOT loaded:" + FnameExtracted);
throw new FileNotFoundException();
}
return ReturningByte;
}
public void ZipFile(string ThisFile, string NameInZIP)
{
try
{
if (!File.Exists(ThisFile))
{
throw new FileNotFoundException();
}
//Create Empty folder that only have the specific file in it
string Zippingfolder = Application.StartupPath + @"\ZippingFolder";
if (!Directory.Exists(Zippingfolder)) Directory.CreateDirectory(Zippingfolder);
//Copy File to Empty Zipping Folder
File.Create(Zippingfolder + @"\" + NameInZIP).Dispose();
File.WriteAllBytes(Zippingfolder + @"\" + NameInZIP, File.ReadAllBytes(ThisFile));
// Create empty archive
if (!File.Exists(_zipPath))
{
//File.Create(_zipPath).Dispose();
byte[] emptyArchiveContent = new byte[] { 80, 75, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
FileStream fs = File.Create(_zipPath);
fs.Write(emptyArchiveContent, 0, emptyArchiveContent.Length);
fs.Flush();
fs.Close();
fs = null;
}
Shell32.Folder destinationFolder = GetShell32NameSpaceFolder(Zippingfolder);
Shell32.Folder sourceFile = GetShell32NameSpaceFolder(_zipPath);
sourceFile.CopyHere(destinationFolder.Items(), 4 | 16);
int Tries = 0;
bool FileIsZipped = false;
while (!FileIsZipped && Tries < (10 * 15))
{
foreach (Shell32.FolderItem file in sourceFile.Items())
{
if (file.Name == NameInZIP) FileIsZipped = true;
}
Thread.Sleep(100);
Tries++;
}
if (Directory.Exists(Zippingfolder)) Directory.Delete(Zippingfolder, true);
}
catch (Exception mess)
{
MessageBox.Show("ERROR: Could not ZIP File: " + Environment.NewLine + mess);
}
}
public void UnZipFileFromThisZIP(string ThisFile, string ThisZIP)
{
try
{
if (!File.Exists(ThisZIP))
{
throw new FileNotFoundException();
}
Shell32.Folder destinationFolder = GetShell32NameSpaceFolder(Application.StartupPath);
Shell32.Folder sourceFile = GetShell32NameSpaceFolder(ThisZIP);
foreach (Shell32.FolderItem file in sourceFile.Items())
{
if (file.Name == ThisFile) destinationFolder.CopyHere(file, 4 | 16);
}
}
catch (Exception mess)
{
MessageBox.Show("ERROR: Could not UNZIP File: " + Environment.NewLine + mess);
}
}
public string[] GetXDFInternalList()
{
string[] AllXDFFiles = new string[] { };
try
{
if (!File.Exists(_zipPath)) throw new FileNotFoundException();
Shell32.Folder destinationFolder = GetShell32NameSpaceFolder(Application.StartupPath);
Shell32.Folder sourceFile = GetShell32NameSpaceFolder(_zipPath);
foreach (Shell32.FolderItem file in sourceFile.Items())
{
if (file.Name == "XDF_Files")
{
destinationFolder.CopyHere(file, 4 | 16);
}
}
if (Directory.Exists(Application.StartupPath + @"\XDF_Files"))
{
AllXDFFiles = Directory.GetFiles(Application.StartupPath + @"\XDF_Files", "*.*", SearchOption.AllDirectories);
Directory.Delete(Application.StartupPath + @"\XDF_Files", true);
}
}
catch (Exception mess)
{
MessageBox.Show("ERROR: Could not get XDF files list: " + Environment.NewLine + mess);
}
return AllXDFFiles;
}
public string[] GetBINInternalList()
{
string[] AllXDFFiles = new string[] { };
try
{
if (!File.Exists(_zipPath)) throw new FileNotFoundException();
Shell32.Folder destinationFolder = GetShell32NameSpaceFolder(Application.StartupPath);
Shell32.Folder sourceFile = GetShell32NameSpaceFolder(_zipPath);
foreach (Shell32.FolderItem file in sourceFile.Items())
{
if (file.Name == "VW_BIN_Files")
{
destinationFolder.CopyHere(file, 4 | 16);
}
}
if (Directory.Exists(Application.StartupPath + @"\VW_BIN_Files"))
{
AllXDFFiles = Directory.GetFiles(Application.StartupPath + @"\VW_BIN_Files", "*.*", SearchOption.AllDirectories);
Directory.Delete(Application.StartupPath + @"\VW_BIN_Files", true);
}
}
catch (Exception mess)
{
MessageBox.Show("ERROR: Could not get BIN files list: " + Environment.NewLine + mess);
}
return AllXDFFiles;
}
}