private void WriteStringValue(BionToken markerType, String8 value) { if (_compressor == null) { // Write marker and length WriteStringLength(markerType, value.Length); // Write value _writer.EnsureSpace(value.Length); value.CopyTo(_writer.Buffer, _writer.Index); _writer.Index += value.Length; } else { // Write marker for compressed, terminated value Write((byte)(markerType == BionToken.String ? BionMarker.StringCompressedTerminated : BionMarker.PropertyNameCompressedTerminated)); // Compress and write value using (BufferedReader reader = BufferedReader.FromString(value)) { _compressor.Compress(reader, _writer); } // Write end token Write(BionMarker.EndValue); } }
private static void Compress(string fromPath, string toPath, string toDictionaryPath) { VerifyFileExists(fromPath); using (new ConsoleWatch($"Compressing {fromPath}...", () => $"Done. {FileLength.MB(fromPath)} to {FileLength.MB(toPath)} + {FileLength.MB(toDictionaryPath)} dictionary ({FileLength.Percentage(fromPath, toPath, toDictionaryPath)})")) { WordCompressor.Compress(fromPath, toPath, toDictionaryPath); } }
public void WordCompressor_RoundTrip() { string originalPath = @"Content\Medium.json"; string compressedPath = "Medium.compressed.bin"; string dictionaryPath = "Medium.compressed.dict"; string comparePath = "Medium.roundtrip.json"; // Roundtrip without optimization; verify files equal WordCompressor.Compress(originalPath, compressedPath, dictionaryPath, false); WordCompressor.Expand(compressedPath, comparePath, dictionaryPath); Verify.FilesEqual(originalPath, comparePath); Verify.SizeRatioUnder(originalPath, compressedPath, 0.5f); File.Delete(compressedPath); File.Delete(dictionaryPath); File.Delete(comparePath); // Roundtrip *with* optimization; verify files equal WordCompressor.Compress(originalPath, compressedPath, dictionaryPath, true); WordCompressor.Expand(compressedPath, comparePath, dictionaryPath); Verify.FilesEqual(originalPath, comparePath); Verify.SizeRatioUnder(originalPath, compressedPath, 0.5f); }