using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Duplicati.Main
{
///
/// This class wraps the usage of SharpRSync into an easy to use static class
///
public class SharpRSyncInterface
{
///
/// Generates a signature, and writes it to the stream
///
/// The stream to create the signature for
/// The stream to write the signature into
public static void GenerateSignature(Stream input, Stream output)
{
SharpRSync.ChecksumFile cs = new Duplicati.SharpRSync.ChecksumFile();
cs.AddStream(input);
cs.Save(output);
}
///
/// Generates a signature file
///
/// The file to create the signature for
/// The file write the signature to
public static void GenerateSignature(string filename, string outputfile)
{
if (System.IO.File.Exists(outputfile))
System.IO.File.Delete(outputfile);
using (FileStream fs1 = File.OpenRead(filename))
using (FileStream fs2 = File.Create(outputfile))
GenerateSignature(fs1, fs2);
}
///
/// Generates a delta stream
///
/// The signature for the stream
/// The (possibly) altered stream to create the delta for
/// The delta output
public static void GenerateDelta(Stream signature, Stream input, Stream output)
{
SharpRSync.ChecksumFile cs = new Duplicati.SharpRSync.ChecksumFile(signature);
SharpRSync.DeltaFile df = new Duplicati.SharpRSync.DeltaFile(cs);
df.GenerateDeltaFile(input, output);
}
///
/// Generates a delta file
///
/// The signature for the file
/// The file to create the delta for
/// The delta output file
public static void GenerateDelta(string signaturefile, string filename, string deltafile)
{
if (System.IO.File.Exists(deltafile))
System.IO.File.Delete(deltafile);
using (FileStream fs1 = File.OpenRead(signaturefile))
using (FileStream fs2 = File.OpenRead(filename))
using (FileStream fs3 = File.Create(deltafile))
GenerateDelta(fs1, fs2, fs3);
}
///
/// Patches a file
///
/// The most recent full copy of the file
/// The delta file
/// The restored file
public static void PatchFile(string basefile, string deltafile, string outputfile)
{
if (System.IO.File.Exists(outputfile))
System.IO.File.Delete(outputfile);
using (FileStream fs1 = File.OpenRead(basefile))
using (FileStream fs2 = File.OpenRead(deltafile))
using (FileStream fs3 = File.Create(outputfile))
PatchFile(fs1, fs2, fs3);
}
///
/// Constructs a stream from a basestream and a delta stream
///
/// The most recent full copy of the file
/// The delta file
/// The restored file
public static void PatchFile(Stream basestream, Stream delta, Stream output)
{
SharpRSync.DeltaFile df = new Duplicati.SharpRSync.DeltaFile(delta);
df.PatchFile(basestream, output);
}
}
}