private void DeflateEntries(out Dictionary<Entry, byte[]> entryDeflatedData, out List<uint> zLengths)
{
// TODO: This produces perfect results for song archives (original vs repacked)
// there are slight differences in the binary of large archives (original vs repacked). WHY?
//
entryDeflatedData = new Dictionary<Entry, byte[]>();
uint blockSize = _header.BlockSizeAlloc;
zLengths = new List<uint>();
var ndx = 0; // for debugging
// var step = Math.Round(1.0 / (_toc.Count + 2) * 100, 3);
// double progress = 0;
// GlobalExtension.ShowProgress("Deflating Entries ...");
foreach (Entry entry in _toc)
{
var zList = new List<Tuple<byte[], int>>();
entry.zIndexBegin = (uint)zLengths.Count;
entry.Data.Seek(0, SeekOrigin.Begin);
while (entry.Data.Position < entry.Data.Length)
{
var array_i = new byte[blockSize];
var array_o = new byte[blockSize * 2];
var memoryStream = new MemoryStream(array_o);
int plain_len = entry.Data.Read(array_i, 0, array_i.Length);
int packed_len = (int)RijndaelEncryptor.Zip(array_i, memoryStream, plain_len, false);
if (packed_len >= plain_len)
{
// If packed data "worse" than plain (i.e. already packed) z = 0
zList.Add(new Tuple<byte[], int>(array_i, plain_len));
}
else
{
// If packed data is good
if (packed_len < (blockSize - 1))
{
// If packed data fits maximum packed block size z = packed_len
zList.Add(new Tuple<byte[], int>(array_o, packed_len));
}
else
{
// Write plain. z = 0
zList.Add(new Tuple<byte[], int>(array_i, plain_len));
}
}
}
int zSisesSum = 0;
foreach (var zSize in zList)
{
zSisesSum += zSize.Item2;
zLengths.Add((uint)zSize.Item2);
}
var array3 = new byte[zSisesSum];
var memoryStream2 = new MemoryStream(array3);
foreach (var entryblock in zList)
{
memoryStream2.Write(entryblock.Item1, 0, entryblock.Item2);
}
entryDeflatedData.Add(entry, array3);
// progress += step;
// GlobalExtension.UpdateProgress.Value = (int)progress;
Debug.WriteLine("Deflating: " + ndx++);
}
}