This repository was archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBmxZipUtils.cs
66 lines (53 loc) · 2.48 KB
/
BmxZipUtils.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
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace nmo2escn {
public class BmxZipUtils {
private static readonly string TEMP_GUID = "7608400f30784c61b79f271fef797ad1";
public static string DecompressBmxToTemp(string bmxFilePath) {
// generate guid folder and preparing it in file system
var guidFolder = Path.Combine(System.IO.Path.GetTempPath(), TEMP_GUID);
if (Directory.Exists(guidFolder))
Directory.Delete(guidFolder, true);
Directory.CreateDirectory(guidFolder);
// use UTF-8 filename
ZipStrings.CodePage = 65001;
using (ZipInputStream s = new ZipInputStream(new FileStream(bmxFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))) {
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null) {
Console.WriteLine($"Decompressing {theEntry.Name}...");
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName != string.Empty) {
Directory.CreateDirectory(Path.Combine(guidFolder, directoryName));
}
if (fileName != string.Empty) {
using (FileStream streamWriter = new FileStream(
Path.Combine(guidFolder, theEntry.Name),
FileMode.Create, FileAccess.Write, FileShare.None)) {
int size = 2048;
byte[] data = new byte[2048];
while (true) {
size = s.Read(data, 0, data.Length);
if (size > 0) {
streamWriter.Write(data, 0, size);
} else {
break;
}
}
}
}
}
}
return guidFolder;
}
public static void CleanTempFolder() {
var guidFolder = Path.Join(System.IO.Path.GetTempPath(), TEMP_GUID);
if (Directory.Exists(guidFolder))
Directory.Delete(guidFolder, true);
}
}
}