2019-09-15 23:13:01 +00:00
|
|
|
|
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 Zlib : ICompressionFormat
|
|
|
|
|
{
|
|
|
|
|
public string[] Description { get; set; } = new string[] { "ZLIB Compressed" };
|
|
|
|
|
public string[] Extension { get; set; } = new string[] { "*.z", "*.zlib", };
|
|
|
|
|
|
|
|
|
|
private long startPosition = 0;
|
|
|
|
|
|
|
|
|
|
public override string ToString() { return "Zlib"; }
|
|
|
|
|
|
|
|
|
|
public bool Identify(Stream stream, string fileName)
|
|
|
|
|
{
|
2019-11-10 16:14:09 +00:00
|
|
|
|
if (stream.Length < 16) return false;
|
|
|
|
|
|
2019-09-15 23:13:01 +00:00
|
|
|
|
using (var reader = new FileReader(stream, true))
|
|
|
|
|
{
|
|
|
|
|
startPosition = stream.Position;
|
|
|
|
|
|
|
|
|
|
reader.SetByteOrder(true);
|
|
|
|
|
|
2019-11-10 16:14:09 +00:00
|
|
|
|
bool IsValid = false;
|
2019-11-10 21:24:54 +00:00
|
|
|
|
for (int i = 0; i < 8; i++)
|
2019-11-10 16:14:09 +00:00
|
|
|
|
{
|
|
|
|
|
ushort magicNumber = reader.ReadUInt16();
|
2019-09-15 23:13:01 +00:00
|
|
|
|
|
2019-11-10 16:14:09 +00:00
|
|
|
|
IsValid = magicNumber == 0x789C || magicNumber == 0x78DA;
|
2019-11-10 17:36:56 +00:00
|
|
|
|
if (IsValid) {
|
2019-11-10 21:24:54 +00:00
|
|
|
|
startPosition = reader.Position - 2;
|
2019-11-10 16:14:09 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-15 23:13:01 +00:00
|
|
|
|
|
2019-11-10 16:14:09 +00:00
|
|
|
|
return IsValid;
|
2019-09-15 23:13:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool CanCompress { get; } = true;
|
|
|
|
|
|
|
|
|
|
public Stream Decompress(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
stream.Position = startPosition;
|
|
|
|
|
|
|
|
|
|
var data = STLibraryCompression.ZLIB.Decompress(stream.ToArray());
|
|
|
|
|
return new MemoryStream(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Stream Compress(Stream stream)
|
|
|
|
|
{
|
|
|
|
|
stream.Position = startPosition;
|
|
|
|
|
|
|
|
|
|
var data = STLibraryCompression.ZLIB.Compress(stream.ToArray());
|
|
|
|
|
return new MemoryStream(data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|