public void CompressAndDecompressTest() { String reference = "Hello World 0123456789"; byte[] dataToCompress = Encoding.Unicode.GetBytes(reference); ZLibCompression zLibCompression = new ZLibCompression(); var compressed = zLibCompression.Compress(dataToCompress); var decompressed = zLibCompression.Decompress(compressed); Assert.Equal(reference, Encoding.Unicode.GetString(decompressed)); }
private Stream GetCompressedStream(Stream baseStream, MpqFileFlags targetFlags, MpqCompressionType compressionType, int targetBlockSize) { var resultStream = new MemoryStream(); var singleUnit = targetFlags.HasFlag(MpqFileFlags.SingleUnit); void TryCompress(uint bytes) { var offset = baseStream.Position; var compressedStream = compressionType switch { MpqCompressionType.ZLib => ZLibCompression.Compress(baseStream, (int)bytes, true), _ => throw new NotSupportedException(), }; // Add one because CompressionType byte not written yet. var length = compressedStream.Length + 1; if (!singleUnit && length >= bytes) { baseStream.CopyTo(resultStream, offset, (int)bytes, StreamExtensions.DefaultBufferSize); } else { resultStream.WriteByte((byte)compressionType); compressedStream.Position = 0; compressedStream.CopyTo(resultStream); } compressedStream.Dispose(); if (singleUnit) { baseStream.Dispose(); } } var length = (uint)baseStream.Length; if ((targetFlags & MpqFileFlags.Compressed) == 0) { baseStream.CopyTo(resultStream); } else if (singleUnit) { TryCompress(length); } else { var blockCount = (uint)((length + targetBlockSize - 1) / targetBlockSize) + 1; var blockOffsets = new uint[blockCount]; blockOffsets[0] = 4 * blockCount; resultStream.Position = blockOffsets[0]; for (var blockIndex = 1; blockIndex < blockCount; blockIndex++) { var bytesToCompress = blockIndex + 1 == blockCount ? (uint)(baseStream.Length - baseStream.Position) : (uint)targetBlockSize; TryCompress(bytesToCompress); blockOffsets[blockIndex] = (uint)resultStream.Position; } resultStream.Position = 0; using (var writer = new BinaryWriter(resultStream, new System.Text.UTF8Encoding(false, true), true)) { for (var blockIndex = 0; blockIndex < blockCount; blockIndex++) { writer.Write(blockOffsets[blockIndex]); } } } resultStream.Position = 0; return(resultStream); }