Add decompressing zlib method (Works with ZCMP files too)

This commit is contained in:
KillzXGaming 2019-06-30 20:03:04 -04:00
parent a4fb55b5e7
commit cb0d680b9d
5 changed files with 27 additions and 2 deletions

Binary file not shown.

View file

@ -24,6 +24,7 @@ namespace Switch_Toolbox.Library.IO
items.Add(new ToolStripMenuItem("lZ4")); items.Add(new ToolStripMenuItem("lZ4"));
items.Add(new ToolStripMenuItem("lZ4F")); items.Add(new ToolStripMenuItem("lZ4F"));
items.Add(new ToolStripMenuItem("ZSTD")); items.Add(new ToolStripMenuItem("ZSTD"));
items.Add(new ToolStripMenuItem("ZLIB"));
SetFunctions(items); SetFunctions(items);
return items; return items;
@ -71,6 +72,8 @@ namespace Switch_Toolbox.Library.IO
OpenFileForCompression(CompressionType.Lz4f, Compress); OpenFileForCompression(CompressionType.Lz4f, Compress);
else if (Name == "ZSTD") else if (Name == "ZSTD")
OpenFileForCompression(CompressionType.Zstb, Compress); OpenFileForCompression(CompressionType.Zstb, Compress);
else if (Name == "ZLIB")
OpenFileForCompression(CompressionType.Zlib, Compress);
else throw new Exception("Unimplimented Type! " + Name); else throw new Exception("Unimplimented Type! " + Name);
} }
@ -107,6 +110,7 @@ namespace Switch_Toolbox.Library.IO
SaveFileForCompression(EveryFileExplorer.YAZ0.Decompress(data)); SaveFileForCompression(EveryFileExplorer.YAZ0.Decompress(data));
break; break;
case CompressionType.Zlib: case CompressionType.Zlib:
SaveFileForCompression(STLibraryCompression.ZLIB.Decompress(data));
break; break;
case CompressionType.Gzip: case CompressionType.Gzip:
SaveFileForCompression(STLibraryCompression.GZIP.Decompress(data)); SaveFileForCompression(STLibraryCompression.GZIP.Decompress(data));

View file

@ -60,11 +60,32 @@ namespace Switch_Toolbox.Library.IO
{ {
public static byte[] Decompress(byte[] b) public static byte[] Decompress(byte[] b)
{ {
using (var br = new FileReader(new MemoryStream(b), true))
{
if (br.ReadString(4) == "ZCMP")
{
return DecompressZCMP(b);
}
else
{
var ms = new System.IO.MemoryStream();
br.BaseStream.Position = 2;
using (var ds = new DeflateStream(new MemoryStream(br.ReadBytes((int)br.BaseStream.Length - 6)), CompressionMode.Decompress))
ds.CopyTo(ms);
return ms.ToArray();
}
}
}
public static Byte[] DecompressZCMP(byte[] b)
{
Console.WriteLine("DecompressZCMP");
using (var br = new FileReader(new MemoryStream(b), true)) using (var br = new FileReader(new MemoryStream(b), true))
{ {
var ms = new System.IO.MemoryStream(); var ms = new System.IO.MemoryStream();
br.BaseStream.Position = 2; br.BaseStream.Position = 130;
using (var ds = new DeflateStream(new MemoryStream(br.ReadBytes((int)br.BaseStream.Length - 6)), CompressionMode.Decompress)) using (var ds = new DeflateStream(new MemoryStream(br.ReadBytes((int)br.BaseStream.Length - 80)), CompressionMode.Decompress))
ds.CopyTo(ms); ds.CopyTo(ms);
return ms.ToArray(); return ms.ToArray();
} }