1
0
mirror of synced 2024-12-04 20:08:00 +01:00
Switch-Toolbox/Switch_Toolbox_Library/Compression/Formats/Zstb.cs

62 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using Toolbox.Library.IO;
namespace Toolbox.Library
{
public class Zstb : ICompressionFormat
{
public string[] Description { get; set; } = new string[] { "ZSTD" };
public string[] Extension { get; set; } = new string[] { "*.zstd", };
public override string ToString() { return "ZSTD"; }
public bool Identify(Stream stream, string fileName)
{
using (var reader = new FileReader(stream, true))
{
uint magic = reader.ReadUInt32();
return magic == 0x28B52FFD || magic == 0xFD2FB528;
}
}
public bool CanCompress { get; } = true;
public Stream Decompress(Stream stream)
{
return new MemoryStream(SDecompress(stream.ToArray()));
}
public Stream Compress(Stream stream)
{
return new MemoryStream(SCompress(stream.ToArray()));
}
public static byte[] SDecompress(byte[] b)
{
using (var decompressor = new ZstdNet.Decompressor())
{
return decompressor.Unwrap(b);
}
}
public static byte[] SDecompress(byte[] b, int MaxDecompressedSize)
{
using (var decompressor = new ZstdNet.Decompressor())
{
return decompressor.Unwrap(b, MaxDecompressedSize);
}
}
public static byte[] SCompress(byte[] b)
{
using (var compressor = new ZstdNet.Compressor())
{
return compressor.Wrap(b);
}
}
}
}