2019-07-01 21:44:19 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.IO.Compression;
|
|
|
|
|
using System.Threading.Tasks;
|
2019-07-16 23:35:21 +02:00
|
|
|
|
using Toolbox.Library.IO;
|
2019-07-01 21:44:19 +02:00
|
|
|
|
using K4os.Compression.LZ4.Streams;
|
|
|
|
|
|
2019-07-16 23:35:21 +02:00
|
|
|
|
namespace Toolbox.Library
|
2019-07-01 21:44:19 +02:00
|
|
|
|
{
|
|
|
|
|
public class LZ4F : ICompressionFormat
|
|
|
|
|
{
|
|
|
|
|
public string[] Description { get; set; } = new string[] { "LZ4F Compression" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.cmp", "*.lz4f" };
|
|
|
|
|
|
2019-09-16 01:13:01 +02:00
|
|
|
|
public override string ToString() { return "LZ4F"; }
|
|
|
|
|
|
|
|
|
|
public bool Identify(Stream stream, string fileName)
|
2019-07-01 21:44:19 +02:00
|
|
|
|
{
|
2019-09-18 23:02:25 +02:00
|
|
|
|
if (stream.Length < 12) return false;
|
|
|
|
|
|
2019-07-01 21:44:19 +02:00
|
|
|
|
using (var reader = new FileReader(stream, true))
|
|
|
|
|
{
|
|
|
|
|
uint DecompressedSize = reader.ReadUInt32();
|
2019-09-16 01:13:01 +02:00
|
|
|
|
uint magicCheck = reader.ReadUInt32();
|
2019-07-01 21:44:19 +02:00
|
|
|
|
|
|
|
|
|
bool LZ4FDefault = magicCheck == 0x184D2204;
|
|
|
|
|
|
|
|
|
|
return LZ4FDefault;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanCompress { get; } = true;
|
|
|
|
|
|
|
|
|
|
public Stream Decompress(Stream stream)
|
|
|
|
|
{
|
2019-09-16 01:13:01 +02:00
|
|
|
|
using (var reader = new FileReader(stream, true))
|
2019-07-01 21:44:19 +02:00
|
|
|
|
{
|
2019-09-16 01:13:01 +02:00
|
|
|
|
reader.Position = 0;
|
|
|
|
|
int OuSize = reader.ReadInt32();
|
|
|
|
|
int InSize = (int)stream.Length - 4;
|
|
|
|
|
var dec = STLibraryCompression.Type_LZ4F.Decompress(reader.getSection(4, InSize));
|
|
|
|
|
return new MemoryStream(dec);
|
2019-07-01 21:44:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Stream Compress(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
var mem = new MemoryStream();
|
2019-09-16 01:13:01 +02:00
|
|
|
|
using (var writer = new FileWriter(mem, true))
|
2019-07-01 21:44:19 +02:00
|
|
|
|
{
|
2019-09-16 01:13:01 +02:00
|
|
|
|
writer.Write((uint)stream.Length);
|
|
|
|
|
byte[] buffer = LZ4.Frame.LZ4Frame.Compress(stream,
|
2019-07-01 21:44:19 +02:00
|
|
|
|
LZ4.Frame.LZ4MaxBlockSize.MB1, true, true, false, true, false);
|
|
|
|
|
|
|
|
|
|
writer.Write(buffer, 0, buffer.Length);
|
|
|
|
|
}
|
2019-09-16 01:13:01 +02:00
|
|
|
|
return mem;
|
2019-07-01 21:44:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|