forked from bsonnino/WordDelphi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnit1.pas
95 lines (86 loc) · 2.64 KB
/
Unit1.pas
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
unit Unit1;
interface
uses
System.Zip, Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Xml.xmldom, Xml.XMLIntf,
Vcl.ExtCtrls, Vcl.Grids, Vcl.ValEdit, Xml.XMLDoc;
type
TForm1 = class(TForm)
OpenDialog1: TOpenDialog;
Memo1: TMemo;
Button1: TButton;
XMLDocument1: TXMLDocument;
ValueListEditor1: TValueListEditor;
Panel1: TPanel;
XMLDocument2: TXMLDocument;
Splitter1: TSplitter;
procedure Button1Click(Sender: TObject);
private
FZipFile : TZipFile;
procedure ReadProperties(const FileName: String);
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
ZipStream: TStream;
LocalHeader: TZipHeader;
FileName : string;
begin
if OpenDialog1.Execute then begin
Memo1.Clear;
FZipFile := TZipFile.Create;
try
FZipFile.Open(OpenDialog1.FileName, TZipMode.zmRead);
// for fileName in FZipFile.FileNames do
// Memo1.Lines.Add(FileName);
FZipFile.Read('_rels/.rels', ZipStream, LocalHeader);
ZipStream.Position := 0;
XMLDocument1.LoadFromStream(ZipStream);
ValueListEditor1.Strings.Clear;
for var i := 0 to XMLDocument1.DocumentElement.ChildNodes.Count-1 do begin
var XmlNode := XMLDocument1.DocumentElement.ChildNodes.Nodes[i];
var AttType := ExtractFileName(StringReplace(XmlNode.Attributes['Type'],
'/','\', [rfReplaceAll]));
if AnsiSameText(AttType, 'core-properties') or
AnsiSameText(AttType, 'extended-properties') then
ReadProperties(XmlNode.Attributes['Target']);
end;
Memo1.Lines.Text := FormatXmlData(XmlDocument1.XML.Text);
finally
FZipFile.Free;
end;
end;
end;
procedure TForm1.ReadProperties(const FileName : String);
var
ZipStream : TStream;
LocalHeader: TZipHeader;
i : Integer;
XmlNode : IXMLNode;
begin
FZipFile.Read(FileName, ZipStream, LocalHeader);
ZipStream.Position := 0;
XMLDocument2.LoadFromStream(ZipStream);
// Read the properties
for i := 0 to XMLDocument2.DocumentElement.ChildNodes.Count-1 do begin
XmlNode := XMLDocument2.DocumentElement.ChildNodes.Nodes[i];
try
// Found new property
ValueListEditor1.InsertRow(XmlNode.NodeName,XmlNode.NodeValue, True);
except
// Property is not simple - discard it.
On EXMLDocError do
;
// Property is null - add empty string
On EVariantTypeCastError do
ValueListEditor1.InsertRow(XmlNode.NodeName,'', True);
end;
end;
end;
end.