public byte[] AddFile(ulong streamId, Stream file, HashType type = HashType.None) { EnsureMaximumState(ArchiveState.PushingFiles); if (_state == ArchiveState.Initial) { _outputFilter = DoOutputFiltering(_hashedStream); _state = ArchiveState.PushingFiles; } byte[] ret = null; _streamIds.Add(streamId); _streamSizes.Add(file.Length); Stream stream = file; HashAlgorithm hash = null; if (type != HashType.None) { hash = Hash.New(type); stream = new HashCalculatorInputFilter(stream, hash); } stream.CopyTo(_outputFilter); if (type != HashType.None) { stream.Dispose(); hash.FinishHashing(); ret = hash.Hash; } AnyFile = true; return ret; }
static void Main() { const string inputPath = "test.bin"; const string outputPath1 = "test.bin.xz"; const string outputPath2 = "test2.bin"; Console.WriteLine("Compressing..."); { var inputFile = new FileStream(inputPath, FileMode.Open, FileAccess.Read, FileShare.Read); var report = new InputProgressReport(100); var sha2 = SHA256.Create(); var calculator = new HashCalculatorInputFilter(inputFile, sha2, false); using (var filter = new ProgressInputFilter(calculator, report.ProgressCallback, inputFile.Length, false)) { var outputFile = new FileStream(outputPath1, FileMode.Create, FileAccess.Write, FileShare.None); using (var lzma = new LzmaOutputFilter(outputFile, false)) filter.CopyTo(lzma); } sha2.FinishHashing(); Console.WriteLine("\n" + sha2.Hash.ToHexString()); } Console.WriteLine("Decompressing..."); { var inputFile = new FileStream(outputPath1, FileMode.Open, FileAccess.Read, FileShare.Read); var report = new OutputProgressReport(100); var sha2 = SHA256.Create(); var progressInputFilter = new ProgressInputFilter(inputFile, report.InputProgressCallback, inputFile.Length, false); using (var lzma = new LzmaInputFilter(progressInputFilter, false)) { var outputFile = new FileStream(outputPath2, FileMode.Create, FileAccess.Write, FileShare.None); var calculator = new HashCalculatorOutputFilter(outputFile, sha2, false); using (var progressOutputFilter = new ProgressOutputFilter(calculator, report.OutputProgressCallback, 0, false)) lzma.CopyTo(progressOutputFilter); } sha2.FinishHashing(); Console.WriteLine("\n" + sha2.Hash.ToHexString()); } }