Skip to content

Commit 72a8d0c

Browse files
committed
Reimplemented Directory
DirectoryTree renamed to DirectoryNode. New code using SortedDictionary as tree node.
1 parent 0f3369f commit 72a8d0c

File tree

5 files changed

+77
-108
lines changed

5 files changed

+77
-108
lines changed

HPIZ/DirectoryNode.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace HPIZ
6+
{
7+
public class DirectoryNode
8+
{
9+
public SortedDictionary<string, DirectoryNode> Children { get; set; }
10+
public DirectoryNode()
11+
{
12+
Children = new SortedDictionary<string, DirectoryNode>(StringComparer.OrdinalIgnoreCase);
13+
}
14+
15+
public void AddEntry(string entry)
16+
{
17+
var sections = entry.Split('\\');
18+
AddEntryRecursively(sections, 0);
19+
}
20+
21+
private void AddEntryRecursively(string[] entry, int level)
22+
{
23+
if (entry.Length > level)
24+
{
25+
if (Children.ContainsKey(entry[level]))
26+
Children[entry[level]].AddEntryRecursively(entry, level + 1);
27+
else
28+
OnlyAddRecursively(entry, level);
29+
}
30+
}
31+
private void OnlyAddRecursively(string[] entry, int level)
32+
{
33+
if (entry.Length > level)
34+
{
35+
Children.Add(entry[level], new DirectoryNode());
36+
Children[entry[level]].OnlyAddRecursively(entry, level + 1);
37+
}
38+
}
39+
40+
public int CalculateSize()
41+
{
42+
int totalSize = 8;
43+
foreach (var item in Children)
44+
{
45+
totalSize += 9;
46+
totalSize += item.Key.Length + 1;
47+
if (item.Value.Children.Count != 0)
48+
totalSize += item.Value.CalculateSize();
49+
else
50+
totalSize += 9;
51+
}
52+
return totalSize;
53+
}
54+
}
55+
}

HPIZ/DirectoryTree.cs

Lines changed: 0 additions & 82 deletions
This file was deleted.

HPIZ/HPIZ.projitems

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<Compile Include="$(MSBuildThisFileDirectory)Compression\Strategy.cs" />
1616
<Compile Include="$(MSBuildThisFileDirectory)Compression\ZLibDeflater.cs" />
1717
<Compile Include="$(MSBuildThisFileDirectory)Compression\LZ77.cs" />
18-
<Compile Include="$(MSBuildThisFileDirectory)DirectoryTree.cs" />
18+
<Compile Include="$(MSBuildThisFileDirectory)DirectoryNode.cs" />
1919
<Compile Include="$(MSBuildThisFileDirectory)FileEntry.cs" />
2020
<Compile Include="$(MSBuildThisFileDirectory)HpiArchive.cs" />
2121
<Compile Include="$(MSBuildThisFileDirectory)HpiFile.cs" />

HPIZ/HpiArchive.cs

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ public HpiArchive(Stream stream)
6767

6868
archiveReader = new BinaryReader(archiveStream);
6969

70-
7170
foreach (var entry in entriesDictionary.Keys)
7271
{
7372
if (entriesDictionary[entry].FlagCompression != CompressionMethod.None)
@@ -84,8 +83,6 @@ public HpiArchive(Stream stream)
8483

8584
entriesDictionary[entry].compressedChunkSizes = size;
8685
}
87-
else
88-
entriesDictionary[entry].compressedChunkSizes = new int[] { entriesDictionary[entry].UncompressedSize}; //To optimize
8986
}
9087
}
9188

@@ -96,52 +93,51 @@ private void GetEntries(int NumberOfEntries, int EntryListOffset, BinaryReader r
9693
reader.BaseStream.Position = EntryListOffset + (i * 9);
9794

9895
int nameOffset = reader.ReadInt32();
99-
int dataOffset = reader.ReadInt32(); //Unused?
96+
int dataOffset = reader.ReadInt32();
10097
bool IsDirectory = reader.ReadBoolean();
10198
reader.BaseStream.Position = nameOffset;
10299
var fullPath = Path.Combine(parentPath, ReadStringCP437NullTerminated(reader));
100+
reader.BaseStream.Position = dataOffset;
103101

104102
if (IsDirectory)
105103
GetEntries(reader.ReadInt32(), reader.ReadInt32(), reader, fullPath);
106104
else
107-
{
108-
FileEntry fd = new FileEntry(reader, this);
109-
entriesDictionary.Add(fullPath, fd);
110-
}
105+
entriesDictionary.Add(fullPath, new FileEntry(reader, this));
111106
}
112107
}
113108

114-
internal static void SetEntries(DirectoryTree tree, BinaryWriter bw, IEnumerator<FileEntry> sequence)
109+
internal static void SetEntries(DirectoryNode node, BinaryWriter bw, IEnumerator<FileEntry> sequence)
115110
{
116-
bw.Write(tree.Count); //Root Entries number in directory
117-
111+
bw.Write(node.Children.Count); //Root Entries number in directory
118112
bw.Write((int)bw.BaseStream.Position + 4); //Entries Offset point to next
119113

120-
for (int i = 0; i < tree.Count; ++i)
114+
bool first = true;
115+
foreach (var item in node.Children)
121116
{
122-
int posString;
123-
if (i == 0)
124-
posString = (int)bw.BaseStream.Position + (tree.Count - i) * 9;
125-
else
126-
posString = (int)bw.BaseStream.Length;
117+
int posString = (int)bw.BaseStream.Length;
118+
if (first)
119+
{
120+
first = false;
121+
posString = (int)bw.BaseStream.Position + node.Children.Count * 9;
122+
}
127123
bw.Write(posString); //NameOffset; /* points to the file name */
128-
int posNext = posString + tree[i].Key.Length + 1;
129-
bw.Write(posNext); //DirDataOffset; /* points to directory data */
130-
bool isDir = tree[i].Children.Count != 0;
131-
bw.Write(isDir);
124+
int posNext = posString + item.Key.Length + 1;
125+
bw.Write(posNext); //DataOffset; /* points to directory data */
126+
bool isDir = item.Value.Children.Count != 0;
127+
bw.Write(isDir);
132128

133129
int previousPos = (int)bw.BaseStream.Position;
134130
bw.BaseStream.Position = posString;
135-
WriteStringCP437NullTerminated(bw, tree[i].Key);
131+
WriteStringCP437NullTerminated(bw, item.Key);
136132
if (isDir)
137-
SetEntries(tree[i].Children, bw, sequence);
133+
SetEntries(item.Value, bw, sequence);
138134
else
139135
{
140136
sequence.MoveNext();
141137
bw.Write(sequence.Current.OffsetOfCompressedData); //OffsetOfData
142138
bw.Write(sequence.Current.UncompressedSize); //UncompressedSize
143139
bw.Write((byte)sequence.Current.FlagCompression); //FlagCompression
144-
140+
145141
}
146142
bw.BaseStream.Position = previousPos;
147143
}

HPIZ/HpiFile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static void Merge(PathCollection archivesFiles, string destinationArchive
101101

102102
private static void WriteToFile(string destinationArchiveFileName, SortedDictionary<string, FileEntry> entries)
103103
{
104-
var tree = new DirectoryTree();
104+
var tree = new DirectoryNode();
105105
foreach (var fileName in entries.Keys)
106106
tree.AddEntry(fileName);
107107
int chunkStartPosition = tree.CalculateSize() + HpiArchive.HeaderSize;

0 commit comments

Comments
 (0)