### Implement Encoding and Decoding Workflow Source: https://context7.com/metric/vcdiff/llms.txt Demonstrates the full lifecycle of creating a delta file from two versions and reconstructing the target file. Includes configuration for block sizes and verification logic. ```csharp using VCDiff.Includes; using VCDiff.Encoders; using VCDiff.Decoders; using System.IO; class VCDiffExample { public static void Main() { string originalFile = "version1.bin"; string newFile = "version2.bin"; string deltaFile = "patch.vcdiff"; string reconstructedFile = "version2_reconstructed.bin"; // Configure for optimal small file compression BlockHash.BlockSize = 16; ChunkEncoder.MinBlockSize = 16; // Step 1: Create delta file Console.WriteLine("Creating delta file..."); if (!CreateDelta(originalFile, newFile, deltaFile)) { Console.WriteLine("Failed to create delta"); return; } // Step 2: Apply delta to reconstruct file Console.WriteLine("Applying delta file..."); if (!ApplyDelta(originalFile, deltaFile, reconstructedFile)) { Console.WriteLine("Failed to apply delta"); return; } // Step 3: Verify reconstruction byte[] original = File.ReadAllBytes(newFile); byte[] reconstructed = File.ReadAllBytes(reconstructedFile); bool identical = original.Length == reconstructed.Length; if (identical) { for (int i = 0; i < original.Length; i++) { if (original[i] != reconstructed[i]) { identical = false; break; } } } Console.WriteLine($"Verification: {(identical ? "PASSED" : "FAILED")}"); Console.WriteLine($"Original size: {new FileInfo(newFile).Length} bytes"); Console.WriteLine($"Delta size: {new FileInfo(deltaFile).Length} bytes"); } static bool CreateDelta(string dictPath, string targetPath, string outputPath) { using (var output = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) using (var dict = new FileStream(dictPath, FileMode.Open, FileAccess.Read)) using (var target = new FileStream(targetPath, FileMode.Open, FileAccess.Read)) { var coder = new VCCoder(dict, target, output); return coder.Encode(interleaved: true, checksum: true) == VCDiffResult.SUCCESS; } } static bool ApplyDelta(string dictPath, string deltaPath, string outputPath) { using (var output = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) using (var dict = new FileStream(dictPath, FileMode.Open, FileAccess.Read)) using (var delta = new FileStream(deltaPath, FileMode.Open, FileAccess.Read)) { var decoder = new VCDecoder(dict, delta, output); if (decoder.Start() != VCDiffResult.SUCCESS) return false; long bytesWritten; return decoder.Decode(out bytesWritten) == VCDiffResult.SUCCESS; } } } ``` -------------------------------- ### Encode Delta Files with VCCoder Source: https://context7.com/metric/vcdiff/llms.txt Demonstrates basic and advanced delta encoding using the VCCoder class, including options for SDHC interleaving and checksums. ```csharp using VCDiff.Includes; using VCDiff.Encoders; using VCDiff.Shared; using System.IO; // Basic encoding example void EncodeFile(string oldFilePath, string newFilePath, string deltaOutputPath) { using (FileStream output = new FileStream(deltaOutputPath, FileMode.Create, FileAccess.Write)) using (FileStream dict = new FileStream(oldFilePath, FileMode.Open, FileAccess.Read)) using (FileStream target = new FileStream(newFilePath, FileMode.Open, FileAccess.Read)) { // Create encoder with default 1MB window chunks VCCoder coder = new VCCoder(dict, target, output); // Encode without checksum and non-interleaved (standard format) VCDiffResult result = coder.Encode(); if (result != VCDiffResult.SUCCESS) { Console.WriteLine("Encoding failed with error: " + result); return; } Console.WriteLine("Delta file created successfully"); } } // Advanced encoding with SDHC interleaved format and checksums void EncodeFileWithOptions(string oldFilePath, string newFilePath, string deltaOutputPath) { using (FileStream output = new FileStream(deltaOutputPath, FileMode.Create, FileAccess.Write)) using (FileStream dict = new FileStream(oldFilePath, FileMode.Open, FileAccess.Read)) using (FileStream target = new FileStream(newFilePath, FileMode.Open, FileAccess.Read)) { // Use 2MB window chunks for larger files int windowSizeMB = 2; VCCoder coder = new VCCoder(dict, target, output, windowSizeMB); bool interleaved = true; // Enable SDHC interleaved format for streaming bool checksum = true; // Enable Adler32 checksums for data integrity VCDiffResult result = coder.Encode(interleaved, checksum); if (result != VCDiffResult.SUCCESS) { Console.WriteLine("Encoding failed"); } } } ``` -------------------------------- ### Configure ChunkEncoder.MinBlockSize Source: https://context7.com/metric/vcdiff/llms.txt Sets the minimum match size for COPY instructions. Must be at least 2 and greater than or equal to BlockHash.BlockSize. ```csharp using VCDiff.Encoders; // Configure minimum block size for copy matches void ConfigureMinBlockSize() { // Default is 32 bytes ChunkEncoder.MinBlockSize = 32; // Lower for small files to improve compression // Must be >= 2 and >= BlockHash.BlockSize ChunkEncoder.MinBlockSize = 16; // Example: Optimize for small file delta encoding BlockHash.BlockSize = 8; ChunkEncoder.MinBlockSize = 8; } ``` -------------------------------- ### Configure minimum copy block size Source: https://github.com/metric/vcdiff/blob/master/README.markdown Set the minimum match size required for copying from the dictionary. Must be a power of 2. ```csharp ChunkEncoder.MinBlockSize = 16; //Default is 32 bytes. Lowering this can improve the delta compression for small files. //Please keep it a power of 2. //Anything lower than 2 or BlockHash.BlockSize is ignored. ``` -------------------------------- ### Set window size for encoding Source: https://github.com/metric/vcdiff/blob/master/README.markdown Adjust the chunk size for windows in megabytes; the default is 1MB. ```csharp int windowSize = 2; //in Megabytes. The default is 1MB window chunks. VCCoder coder = new VCCoder(dict, target, output, windowSize) ``` -------------------------------- ### Configure encoding options Source: https://github.com/metric/vcdiff/blob/master/README.markdown Toggle interleaving and checksum generation during the encoding process. ```csharp bool interleaved = true; bool checksum = false; coder.Encode(interleaved, checksum); checksum = true; coder.Encode(interleaved, checksum); ``` -------------------------------- ### Configure block size for hashing Source: https://github.com/metric/vcdiff/blob/master/README.markdown Adjust the block size used for hashing. Increasing this can improve results for large files with similar data. ```csharp BlockHash.BlockSize = 32; //increasing this for large files with lots of similar data can improve results. //the default is 16. Please keep it a power of two. //Lowering it for small files can also improve results. //Anything lower than 2 will be ignored. ``` -------------------------------- ### Handle Streaming VCDiff Data (Interleaved) Source: https://github.com/metric/vcdiff/blob/master/README.markdown This loop is for handling interleaved delta files. It requires checking buffer size before each decode call and continuing until the expected data size is reached or an error occurs. The decode function is blocking and best run in a separate thread. ```csharp while(bytesWritten < someSizeThatYouAreExpecting) { //make sure we have enough data in buffer to at least try and decode the next window section //otherwise we will probably receive an error. if(myStream.Length < 22) continue; long thisChunk = 0; result = decoder.Decode(out thisChunk); bytesWritten += thisChunk; //yes with three Rs. if(result == VCDiffResult.ERRROR) { //it failed to decode something //could be an issue that the window failed to parse //or actual data failed to decode properly break; } //otherwise continue on if you get SUCCESS or EOD (End of Data); //because only you know when you will have the data finished loading //the decoder doesn't care if nothing is available and it will keep trying until more is //it is best to do this in a separate thread as it is blocking. } ``` -------------------------------- ### Configure BlockHash.BlockSize Source: https://context7.com/metric/vcdiff/llms.txt Adjusts the hashing block size for encoding. Values must be at least 2 and a power of two. ```csharp using VCDiff.Encoders; // Configure block size for optimal hashing void ConfigureBlockHash() { // Default is 16 bytes - good for general use BlockHash.BlockSize = 16; // Increase for large files with lots of similar data // Improves performance but may reduce compression ratio BlockHash.BlockSize = 32; // Decrease for small files to improve compression // Must be at least 2 and a power of two BlockHash.BlockSize = 8; // Values less than 2 are ignored BlockHash.BlockSize = 1; // This will be ignored, stays at previous value } ``` -------------------------------- ### Decode VCDiff Data (Non-Interleaved) Source: https://github.com/metric/vcdiff/blob/master/README.markdown Use this method for non-interleaved delta files which require the entire delta file to be available. Ensure the dictionary, target delta file, and output stream are properly opened before calling decoder.Start(). ```csharp using VCDiff.Include; using VCDiff.Decoders; using VCDiff.Shared; void DoDecode() { using(FileStream output = new FileStream("...some output path", FileMode.Create, FileAccess.Write)) using(FileStream dict = new FileStream("..dictionary / old file path", FileMode.Open, FileAccess.Read)) using(FileStream target = new FileStream("..delta encoded part", FileMode.Open, FileMode.Read)) { VCDecoder decoder = new VCDecoder(dict, target, output); //You must call decoder.Start() first. The header of the delta file must be available before calling decoder.Start() VCDiffResult result = decoder.Start(); if(result != VCDiffResult.SUCCESS) { //error abort } long bytesWritten = 0; result = decoder.Decode(out bytesWritten); if(result != VCDiffResult.SUCCESS) { //error decoding } //if success bytesWritten will contain the number of bytes that were decoded } } ``` -------------------------------- ### Decode Files and Streams with VCDecoder Source: https://context7.com/metric/vcdiff/llms.txt Provides methods for standard file-based decoding and streaming-based decoding for interleaved formats. ```csharp using VCDiff.Includes; using VCDiff.Decoders; using VCDiff.Shared; using System.IO; // Basic decoding example void DecodeFile(string dictionaryPath, string deltaPath, string outputPath) { using (FileStream output = new FileStream(outputPath, FileMode.Create, FileAccess.Write)) using (FileStream dict = new FileStream(dictionaryPath, FileMode.Open, FileAccess.Read)) using (FileStream delta = new FileStream(deltaPath, FileMode.Open, FileAccess.Read)) { VCDecoder decoder = new VCDecoder(dict, delta, output); // Must call Start() first to read and validate header VCDiffResult result = decoder.Start(); if (result != VCDiffResult.SUCCESS) { Console.WriteLine("Failed to start decoder: " + result); return; } // Check if using Google's SDHC format Console.WriteLine("Using SDHC format: " + decoder.IsSDHCFormat); // Decode all windows long bytesWritten = 0; result = decoder.Decode(out bytesWritten); if (result != VCDiffResult.SUCCESS) { Console.WriteLine("Decoding failed: " + result); return; } Console.WriteLine($"Successfully decoded {bytesWritten} bytes"); } } // Streaming decode for interleaved format void DecodeStream(Stream dictStream, Stream deltaStream, Stream outputStream, long expectedSize) { VCDecoder decoder = new VCDecoder(dictStream, deltaStream, outputStream); VCDiffResult result = decoder.Start(); if (result != VCDiffResult.SUCCESS) return; long totalBytesWritten = 0; // Continue decoding while more data is expected while (totalBytesWritten < expectedSize) { // Ensure at least 22 bytes available for window header if (deltaStream.Length - deltaStream.Position < 22) { // Wait for more data in streaming scenario continue; } long chunkBytes = 0; result = decoder.Decode(out chunkBytes); totalBytesWritten += chunkBytes; if (result == VCDiffResult.ERRROR) { Console.WriteLine("Decode error occurred"); break; } // SUCCESS or EOD - continue processing } } ``` -------------------------------- ### Encode data using VCCoder Source: https://github.com/metric/vcdiff/blob/master/README.markdown Perform a blocking encode operation using a dictionary and target file stream. ```csharp using VCDiff.Include; using VCDiff.Encoders; using VCDiff.Shared; void DoEncode() { using(FileStream output = new FileStream("...some output path", FileMode.Create, FileAccess.Write)) using(FileStream dict = new FileStream("..dictionary / old file path", FileMode.Open, FileAccess.Read)) using(FileStream target = new FileStream("..target data / new data path", FileMode.Open, FileMode.Read)) { VCCoder coder = new VCCoder(dict, target, output); VCDiffResult result = coder.Encode(); //encodes with no checksum and not interleaved if(result != VCDiffResult.SUCCESS) { //error was not able to encode properly } } } ``` -------------------------------- ### Handle VCDiffResult Enumeration Source: https://context7.com/metric/vcdiff/llms.txt Use this switch structure to process the outcome of encoding or decoding operations. It handles success, error, and end-of-data states. ```csharp using VCDiff.Includes; void HandleResult(VCDiffResult result) { switch (result) { case VCDiffResult.SUCCESS: // Operation completed successfully Console.WriteLine("Operation successful"); break; case VCDiffResult.ERRROR: // An error occurred during encoding/decoding // Could be invalid file format, corrupted data, or empty streams Console.WriteLine("Error occurred during operation"); break; case VCDiffResult.EOD: // End of data reached (normal for streaming scenarios) // Indicates no more data available to process Console.WriteLine("End of data reached"); break; } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.