-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainInterface.cs
85 lines (76 loc) · 2.27 KB
/
MainInterface.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
using System;
using System.IO;
using System.Windows.Forms;
namespace XmfExtractor {
public partial class MainInterface : Form {
public MainInterface() {
InitializeComponent();
}
private Stream openedXmf = null;
private void openToolStripMenuItem_Click(object sender, EventArgs e) {
if (this.openXmfFileDialog.ShowDialog(this) == DialogResult.OK) {
this.OpenFile(openXmfFileDialog.FileName);
}
}
private void OpenFile(string filename) {
try {
if (this.openedXmf != null) {
this.openedXmf.Dispose();
this.openedXmf = null;
}
this.openedXmf = File.OpenRead(filename);
var xmf = Xmf.FromStream(this.openedXmf);
this.listView.Items.Clear();
ExtractFiles(this.openedXmf, xmf.RootNode);
} catch (Exception ex) {
if (this.openedXmf != null) {
this.openedXmf.Dispose();
this.openedXmf = null;
}
MessageBox.Show(this, "Error loading file: " + ex.Message, "Open", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void ExtractFiles(Stream stream, Node node) {
if (node.Children != null) {
foreach (var child in node.Children) {
ExtractFiles(stream, child);
}
} else {
string filename = null;
foreach (var meta in node.MetaData) {
switch (meta.FieldSpecifier) {
case FieldSpecifier.FilenameOnDisk:
filename = meta.GetStringValue();
break;
}
}
if (!string.IsNullOrEmpty(filename)) {
listView.Items.Add(new ListViewItem {
Text = filename,
Tag = node,
Selected = true,
});
}
}
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e) {
foreach (ListViewItem lvi in listView.SelectedItems) {
saveFileDialog.FileName = Path.GetFileName(lvi.Text);
if (saveFileDialog.ShowDialog(this) == DialogResult.OK) {
try {
Node n = (Node)lvi.Tag;
File.WriteAllBytes(saveFileDialog.FileName, n.GetFileData(this.openedXmf));
} catch (Exception ex) {
MessageBox.Show(this, "Error saving file '" + lvi.Text + "': " + ex.Message, "Open", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void MainInterface_FormClosed(object sender, FormClosedEventArgs e) {
if (this.openedXmf != null) {
this.openedXmf.Dispose();
this.openedXmf = null;
}
}
}
}