Skip to content

Commit 0725f4b

Browse files
committed
Use Serialization implementation of BFPK extraction
1 parent 6e0063c commit 0725f4b

File tree

2 files changed

+1
-113
lines changed

2 files changed

+1
-113
lines changed

BinaryObjectScanner.Test/FileType/BFPKTests.cs

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,5 @@ public void ExtractStream_Empty_False()
4040
bool actual = extractable.Extract(stream, file, outDir, includeDebug: false);
4141
Assert.False(actual);
4242
}
43-
44-
[Fact]
45-
public void ExtractAll_EmptyModel_False()
46-
{
47-
var model = new SabreTools.Models.BFPK.Archive();
48-
var data = new MemoryStream();
49-
var item = new SabreTools.Serialization.Wrappers.BFPK(model, data);
50-
string outputDirectory = string.Empty;
51-
52-
bool actual = BFPK.ExtractAll(item, outputDirectory);
53-
Assert.False(actual);
54-
}
55-
56-
[Fact]
57-
public void ExtractFile_EmptyModel_False()
58-
{
59-
var model = new SabreTools.Models.BFPK.Archive();
60-
var data = new MemoryStream();
61-
var item = new SabreTools.Serialization.Wrappers.BFPK(model, data);
62-
string outputDirectory = string.Empty;
63-
64-
bool actual = BFPK.ExtractFile(item, 0, outputDirectory);
65-
Assert.False(actual);
66-
}
6743
}
6844
}
Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.IO;
22
using BinaryObjectScanner.Interfaces;
3-
using SabreTools.Compression.Deflate;
43

54
namespace BinaryObjectScanner.FileType
65
{
@@ -29,96 +28,9 @@ public bool Extract(Stream? stream, string file, string outDir, bool includeDebu
2928

3029
// Extract all files
3130
Directory.CreateDirectory(outDir);
32-
ExtractAll(bfpk, outDir);
31+
bfpk.ExtractAll(outDir);
3332

3433
return true;
3534
}
36-
37-
/// <summary>
38-
/// Extract all files from the BFPK to an output directory
39-
/// </summary>
40-
/// <param name="outputDirectory">Output directory to write to</param>
41-
/// <returns>True if all files extracted, false otherwise</returns>
42-
public static bool ExtractAll(SabreTools.Serialization.Wrappers.BFPK item, string outputDirectory)
43-
{
44-
// If we have no files
45-
if (item.Model.Files == null || item.Model.Files.Length == 0)
46-
return false;
47-
48-
// Loop through and extract all files to the output
49-
bool allExtracted = true;
50-
for (int i = 0; i < item.Model.Files.Length; i++)
51-
{
52-
allExtracted &= ExtractFile(item, i, outputDirectory);
53-
}
54-
55-
return allExtracted;
56-
}
57-
58-
/// <summary>
59-
/// Extract a file from the BFPK to an output directory by index
60-
/// </summary>
61-
/// <param name="index">File index to extract</param>
62-
/// <param name="outputDirectory">Output directory to write to</param>
63-
/// <returns>True if the file extracted, false otherwise</returns>
64-
public static bool ExtractFile(SabreTools.Serialization.Wrappers.BFPK item, int index, string outputDirectory)
65-
{
66-
// If we have no files
67-
if (item.Model.Files == null || item.Model.Files.Length == 0)
68-
return false;
69-
70-
// If we have an invalid index
71-
if (index < 0 || index >= item.Model.Files.Length)
72-
return false;
73-
74-
// Get the file information
75-
var file = item.Model.Files[index];
76-
if (file == null)
77-
return false;
78-
79-
// Get the read index and length
80-
int offset = file.Offset + 4;
81-
int compressedSize = file.CompressedSize;
82-
83-
// Some files can lack the length prefix
84-
if (compressedSize > item.GetEndOfFile())
85-
{
86-
offset -= 4;
87-
compressedSize = file.UncompressedSize;
88-
}
89-
90-
try
91-
{
92-
// Ensure the output directory exists
93-
Directory.CreateDirectory(outputDirectory);
94-
95-
// Create the output path
96-
string filePath = Path.Combine(outputDirectory, file.Name ?? $"file{index}");
97-
using FileStream fs = File.OpenWrite(filePath);
98-
99-
// Read the data block
100-
var data = item.ReadFromDataSource(offset, compressedSize);
101-
if (data == null)
102-
return false;
103-
104-
// If we have uncompressed data
105-
if (compressedSize == file.UncompressedSize)
106-
{
107-
fs.Write(data, 0, compressedSize);
108-
}
109-
else
110-
{
111-
MemoryStream ms = new MemoryStream(data);
112-
ZlibStream zs = new ZlibStream(ms, CompressionMode.Decompress);
113-
zs.CopyTo(fs);
114-
}
115-
116-
return true;
117-
}
118-
catch
119-
{
120-
return false;
121-
}
122-
}
12335
}
12436
}

0 commit comments

Comments
 (0)