Skip to content

Commit ecf6c95

Browse files
committed
Port OpenNext/OpenPrevious from BOS
1 parent 5e0b007 commit ecf6c95

File tree

1 file changed

+59
-27
lines changed

1 file changed

+59
-27
lines changed

SabreTools.Serialization/Wrappers/MicrosoftCabinet.cs

Lines changed: 59 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ public MicrosoftCabinet(Models.MicrosoftCabinet.Cabinet? model, Stream? data)
124124

125125
// Get the full file path and directory
126126
filename = Path.GetFullPath(filename);
127-
string? directory = Path.GetDirectoryName(filename);
128127

129128
// Read in the current file and try to parse
130129
var stream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
@@ -135,21 +134,8 @@ public MicrosoftCabinet(Models.MicrosoftCabinet.Cabinet? model, Stream? data)
135134
// Seek to the first part of the cabinet set
136135
while (current.Header.CabinetPrev != null)
137136
{
138-
// Get the path defined in the current header
139-
string prevPath = current.Header.CabinetPrev;
140-
if (directory != null)
141-
prevPath = Path.Combine(directory, prevPath);
142-
143-
// If the file doesn't exist
144-
if (!File.Exists(prevPath))
145-
break;
146-
147-
// Close the previous cabinet part to avoid locking issues
148-
stream.Close();
149-
150-
// Open the previous cabinet and try to parse
151-
stream = File.Open(prevPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
152-
var prev = Create(stream);
137+
// Attempt to open the previous cabinet
138+
var prev = current.OpenPrevious(filename);
153139
if (prev?.Header == null)
154140
break;
155141

@@ -163,18 +149,8 @@ public MicrosoftCabinet(Models.MicrosoftCabinet.Cabinet? model, Stream? data)
163149
// Read in the cabinet parts sequentially
164150
while (current.Header.CabinetNext != null)
165151
{
166-
// Get the path defined in the current header
167-
string nextPath = current.Header.CabinetNext;
168-
if (directory != null)
169-
nextPath = Path.Combine(directory, nextPath);
170-
171-
// If the file doesn't exist
172-
if (!File.Exists(nextPath))
173-
break;
174-
175152
// Open the next cabinet and try to parse
176-
stream = File.Open(nextPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
177-
var next = Create(stream);
153+
var next = current.OpenNext(filename);
178154
if (next?.Header == null)
179155
break;
180156

@@ -188,6 +164,62 @@ public MicrosoftCabinet(Models.MicrosoftCabinet.Cabinet? model, Stream? data)
188164
return start;
189165
}
190166

167+
/// <summary>
168+
/// Open the next archive, if possible
169+
/// </summary>
170+
/// <param name="filename">Filename for one cabinet in the set</param>
171+
public MicrosoftCabinet? OpenNext(string file)
172+
{
173+
// Ignore invalid archives
174+
if (Header == null)
175+
return null;
176+
177+
// Normalize the filename
178+
file = Path.GetFullPath(file);
179+
180+
// Get if the cabinet has a next part
181+
string? next = Header.CabinetNext;
182+
if (string.IsNullOrEmpty(next))
183+
return null;
184+
185+
// Get the full next path
186+
string? folder = Path.GetDirectoryName(file);
187+
if (folder != null)
188+
next = Path.Combine(folder, next);
189+
190+
// Open and return the next cabinet
191+
var fs = File.Open(next, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
192+
return Create(fs);
193+
}
194+
195+
/// <summary>
196+
/// Open the previous archive, if possible
197+
/// </summary>
198+
/// <param name="filename">Filename for one cabinet in the set</param>
199+
public MicrosoftCabinet? OpenPrevious(string file)
200+
{
201+
// Ignore invalid archives
202+
if (Header == null)
203+
return null;
204+
205+
// Normalize the filename
206+
file = Path.GetFullPath(file);
207+
208+
// Get if the cabinet has a previous part
209+
string? prev = Header.CabinetPrev;
210+
if (string.IsNullOrEmpty(prev))
211+
return null;
212+
213+
// Get the full next path
214+
string? folder = Path.GetDirectoryName(file);
215+
if (folder != null)
216+
prev = Path.Combine(folder, prev);
217+
218+
// Open and return the previous cabinet
219+
var fs = File.Open(prev, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
220+
return Create(fs);
221+
}
222+
191223
#endregion
192224

193225
#region Checksumming

0 commit comments

Comments
 (0)