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
|
|
|
|
{
|
2019-09-16 01:13:01 +02:00
|
|
|
|
public class lz4 : ICompressionFormat
|
2019-07-01 21:44:19 +02:00
|
|
|
|
{
|
2019-09-16 01:13:01 +02:00
|
|
|
|
public string[] Description { get; set; } = new string[] { "LZ4 Compression" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.lz4" };
|
2019-07-01 21:44:19 +02:00
|
|
|
|
|
2019-09-16 01:13:01 +02:00
|
|
|
|
public override string ToString() { return "lz4"; }
|
|
|
|
|
|
|
|
|
|
public bool Identify(Stream stream, string fileName)
|
2019-07-01 21:44:19 +02:00
|
|
|
|
{
|
2019-09-16 01:13:01 +02:00
|
|
|
|
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)
|
|
|
|
|
{
|
|
|
|
|
using (MemoryStream mem = new MemoryStream())
|
|
|
|
|
{
|
|
|
|
|
using (var source = LZ4Stream.Decode(stream))
|
|
|
|
|
{
|
|
|
|
|
source.CopyTo(mem);
|
|
|
|
|
}
|
|
|
|
|
return mem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Stream Compress(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
var mem = new MemoryStream();
|
2019-09-16 01:13:01 +02:00
|
|
|
|
using (var source = LZ4Stream.Encode(stream, null, true))
|
2019-07-01 21:44:19 +02:00
|
|
|
|
{
|
2019-09-16 01:13:01 +02:00
|
|
|
|
source.CopyTo(mem);
|
2019-07-01 21:44:19 +02:00
|
|
|
|
}
|
2019-09-16 01:13:01 +02:00
|
|
|
|
return mem;
|
2019-07-01 21:44:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|