private EncodingStatistics Encode(string inputPath, string outputFile, CancellationToken cancellationToken, IProgress <CodingProgressInfo> progress) { HuffmanEncoder encoder = new HuffmanEncoder(); DirectoryEncodingInputStream directoryInputStream = new DirectoryEncodingInputStream(inputPath, fileSystemService, platform); FileEncodingOutputStream outputStream = new FileEncodingOutputStream(outputFile, platform); ITaskProgressController tpc = CreateTaskProgressController(progress, directoryInputStream); try { outputStream.BeginWrite(); tpc.StartIndeterminate(); EncodingToken encodingToken = encoder.CreateEncodingToken(directoryInputStream, cancellationToken); tpc.EndIndeterminate(); streamBuilder.Initialize(platform, encodingToken, outputStream); streamBuilder.AddWeightsTable(new BootstrapSegment(encodingToken.WeightsTable)); tpc.Start(); foreach (FileSystemEntry entry in fileSystemService.EnumFileSystemEntries(inputPath)) { switch (entry.Type) { case FileSystemEntryType.Directory: streamBuilder.AddDirectory(new DirectorySegment(entry.Name, entry.Cardinality)); break; case FileSystemEntryType.File: streamBuilder.AddFile(new FileSegment(entry.Name, entry.Path), cancellationToken, tpc); break; default: throw new InvalidOperationException(); } } outputStream.EndWrite(); tpc.Finish(); return(new EncodingStatistics(directoryInputStream.SizeInBytes, outputStream.SizeInBytes)); } catch { tpc.Error(); throw; } finally { directoryInputStream.Dispose(); outputStream.Dispose(); } }
private byte[] Encode(byte[] data, out HuffmanTreeBase tree, out long streamSize) { MemoryStream outputMemoryStream = new MemoryStream(); FileEncodingInputStream inputStream = CreateFileEncodingInputStream(data); FileEncodingOutputStream outputStream = CreateFileEncodingOutputStream(outputMemoryStream); HuffmanEncoder encoder = new HuffmanEncoder(); try { outputStream.BeginWrite(); EncodingToken token = encoder.CreateEncodingToken(inputStream, CancellationToken.None); streamSize = encoder.Encode(inputStream, outputStream, token, CancellationToken.None, null); outputStream.EndWrite(); tree = token.HuffmanTree; return(outputMemoryStream.ToArray()); } finally { inputStream.Dispose(); outputStream.Dispose(); } }