Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use System.IO.Compression for decompressing PFS files #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 13 additions & 43 deletions LanternExtractor/EQ/Archive/PfsArchive.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Ionic.Zlib;
using System.IO.Compression;
using LanternExtractor.Infrastructure.Logger;

namespace LanternExtractor.EQ.Archive
Expand Down Expand Up @@ -143,52 +143,22 @@ public override bool Initialize()
private static bool InflateBlock(byte[] deflatedBytes, int inflatedSize, out byte[] inflatedBytes,
ILogger logger)
{
var output = new byte[inflatedSize];
var inputStream = new MemoryStream(deflatedBytes);
var decompressStream = new ZLibStream(inputStream, CompressionMode.Decompress);
var outputStream = new MemoryStream();

using (var memoryStream = new MemoryStream())
try
{
var zlibCodec = new ZlibCodec();
zlibCodec.InitializeInflate(true);

zlibCodec.InputBuffer = deflatedBytes;
zlibCodec.AvailableBytesIn = deflatedBytes.Length;
zlibCodec.NextIn = 0;
zlibCodec.OutputBuffer = output;

foreach (FlushType f in new[] { FlushType.None, FlushType.Finish })
{
int bytesToWrite;

do
{
zlibCodec.AvailableBytesOut = inflatedSize;
zlibCodec.NextOut = 0;
try
{
zlibCodec.Inflate(f);
}
catch (Exception e)
{
inflatedBytes = null;
logger.LogError("PfsArchive: Exception caught while inflating bytes: " + e);
return false;
}

bytesToWrite = inflatedSize - zlibCodec.AvailableBytesOut;
if (bytesToWrite > 0)
memoryStream.Write(output, 0, bytesToWrite);
}
while (f == FlushType.None &&
(zlibCodec.AvailableBytesIn != 0 || zlibCodec.AvailableBytesOut == 0) ||
f == FlushType.Finish && bytesToWrite != 0);
}

zlibCodec.EndInflate();

inflatedBytes = output;
decompressStream.CopyTo(outputStream);
inflatedBytes = outputStream.ToArray();
return true;
}
catch (Exception e)
{
inflatedBytes = null;
logger.LogError("PfsArchive: Exception caught while inflating bytes: " + e);
return false;
}
}

}
}
1 change: 0 additions & 1 deletion LanternExtractor/LanternExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
</PackageReference>
<PackageReference Include="System.ComponentModel.Composition" Version="6.0.0" />
<PackageReference Include="System.Data.DataSetExtensions" Version="4.5.0" />
<PackageReference Include="DotNetZip" Version="1.16.0" />
<PackageReference Include="System.Drawing.Common" Version="6.0.0" />
</ItemGroup>
</Project>