好吧,我目前正在尝试在我的项目中实现压缩算法,实际上它必须是 lz77 ......我已经能够解压缩数据,但我无法想象从哪里开始压缩。我认为最好通过一个带有数据的字节数组,但仅此而已......我的问题是算法的所有描述对我来说都非常神秘。我希望能清楚地描述该算法的工作原理以及我必须注意的事项。另外:在 C# 中编码时,我是否必须使用不安全的方法和指针?最好避免这种情况……我想。
感谢您提供给我的信息,这是我目前所获得的信息:
private const int searchWindow = 4095;
private const byte lookaheadWindow = 15;
public static byte[] lzCompressData(byte[] input)
{
int position = 0;
List<byte> tempInput = input.ToList();
List<byte> output = new List<byte>();
MemoryStream init = new MemoryStream();
BinaryWriter inbw = new BinaryWriter(init);
inbw.Write(((input.Length << 8) & 0xFFFFFF00) | 0x10);
output.AddRange(init.ToArray());
while (position < input.Length)
{
byte decoder = 0;
List<byte> tempOutput = new List<byte>();
for (int i = 0; i < 8; ++i)
{
List<byte> eligible;
if(position < 255)
{
eligible = tempInput.GetRange(0, position);
}
else
{
eligible = tempInput.GetRange(position - searchWindow, searchWindow);
}
if (!(position > input.Length - 8))
{
MemoryStream ms = new MemoryStream(eligible.ToArray());
List<byte> currentSequence = new List<byte>();
currentSequence.Add(input[position]);
int offset = 0;
int length = 0;
long tempoffset = StreamHelper.FindPosition(ms, currentSequence.ToArray());
while ((tempoffset != -1) && (length < lookaheadWindow) && position < input.Length - 8)
{
offset = (int)tempoffset;
length = currentSequence.Count;
position++;
currentSequence.Add(input[position]);
}
if (length >= 3)
{
decoder = (byte)(decoder | (byte)(1 << i));
byte b1 = (byte)((length << 4) | (offset >> 8));
byte b2 = (byte)(offset & 0xFF);
tempOutput.Add(b1);
tempOutput.Add(b2);
}
else
{
tempOutput.Add(input[position]);
position++;
}
}
else
{
if (position < input.Length)
{
tempOutput.Add(input[position]);
position++;
}
else
{
tempOutput.Add(0xFF);
}
}
}
output.Add(decoder);
output.AddRange(tempOutput.ToArray());
}
return output.ToArray();
}