namespace Mlaumcherb.Client.Avalonia.холопы;

/// <summary>
/// https://gist.github.com/ststeiger/cb9750664952f775a341
/// </summary>
public static class LZMAHelper
{
    public static void Decompress(Stream inputStream, Stream outputStream)
    {
        var decoder = new SevenZip.Compression.LZMA.Decoder();
        var properties = new byte[5];
        // Read decoder properties
        if (inputStream.Read(properties, 0, 5) != 5)
            throw new Exception("lzma stream is too short");
        decoder.SetDecoderProperties(properties);
        
        // Read decompressed data size
        var fileLengthBytes = new byte[8];
        if(inputStream.Read(fileLengthBytes, 0, 8) != 8)
            throw new Exception("lzma stream is too short");
        long fileLength = BitConverter.ToInt64(fileLengthBytes, 0);
        
        // Decode
        decoder.Code(inputStream, outputStream, -1, fileLength, null);
    }
}