c# FastRSyncNet - delete from destination source not working - c#

Im trying to use FastRSyncNet library for sync between 2 folders.
It works fine when files exists in source folder and not exists in the destination folder.
when files not exists on the source folder and exists in destination folder , its not deleted.
example :
source folder -> 1.txt , 2.txt
destination folder -> 1.txt , 2.txt , 3.txt
all files transferred from source to destination.
public void sync()
{
// Create signature file
var signatureBaseFilePath = #"C:\tmp\test\r1\r1.tar.gz";
var signatureFilePath = #"C:\tmp\test\output\r1.tar.gz.octosig";
var signatureOutputDirectory = Path.GetDirectoryName(signatureFilePath);
if (!Directory.Exists(signatureOutputDirectory))
Directory.CreateDirectory(signatureOutputDirectory);
var signatureBuilder = new SignatureBuilder();
using (var basisStream = new FileStream(signatureBaseFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var signatureStream = new FileStream(signatureFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
signatureBuilder.Build(basisStream, new SignatureWriter(signatureStream));
}
// Create delta file
var newFilePath = #"C:\tmp\test\r2\r2.tar.gz";
var deltaFilePath = #"C:\tmp\test\output\r2.tar.gz.octodelta";
var deltaOutputDirectory = Path.GetDirectoryName(deltaFilePath);
if (!Directory.Exists(deltaOutputDirectory))
Directory.CreateDirectory(deltaOutputDirectory);
var deltaBuilder = new DeltaBuilder();
using (var newFileStream = new FileStream(newFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var signatureFileStream = new FileStream(signatureFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var deltaStream = new FileStream(deltaFilePath, FileMode.Create, FileAccess.Write, FileShare.Read))
{
deltaBuilder.BuildDelta(newFileStream, new SignatureReader(signatureFileStream, new ConsoleProgressReporter()), new AggregateCopyOperationsDecorator(new BinaryDeltaWriter(deltaStream)));
}
// Apply delta file to create new file
var newFilePath2 = #"C:\tmp\test\output\r1.tar.gz";
var newFileOutputDirectory = Path.GetDirectoryName(newFilePath2);
if (!Directory.Exists(newFileOutputDirectory))
Directory.CreateDirectory(newFileOutputDirectory);
var deltaApplier = new DeltaApplier { SkipHashCheck = false };
using (var basisStream = new FileStream(signatureBaseFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var deltaStream = new FileStream(deltaFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
using (var newFileStream = new FileStream(newFilePath2, FileMode.Create, FileAccess.ReadWrite, FileShare.Read))
{
deltaApplier.Apply(basisStream, new BinaryDeltaReader(deltaStream, new ConsoleProgressReporter()), newFileStream);
}
}
resources : https://github.com/GrzegorzBlok/FastRsyncNet

Related

How to read whole binary file (not only the first variables) including string, long and int

using (var fileStream = new FileStream("data.bin", FileMode.Append, FileAccess.Write))
using (var bw1 = new BinaryWriter(fileStream))
{
bw1.Write(jmeno);
bw1.Write(date);
bw1.Write(rodnecislo);
bw1.Write(sarze);
}
using (var fileStream = new FileStream("data.bin", FileMode.Open, FileAccess.Read))
using (var br1 = new BinaryReader(fileStream))
{
string readname = br1.ReadString();
int readdate = br1.ReadInt32();
long readcislo = br1.ReadInt64();
long readsarze = br1.ReadInt64();
Console.WriteLine(readname);
Console.WriteLine(readdate);
Console.WriteLine(readcislo);
Console.WriteLine(readsarze);
}
This is how the BinaryWriter looks like. I don't know how to make the console write this whole binary file.
OK, then here is how to do it:
using (var fileStream = new FileStream("data.bin", FileMode.Open, FileAccess.Read))
using (var br1 = new BinaryReader(fileStream))
{
while (br1.BaseStream.Position != br1.BaseStream.Length)
{
string readname = br1.ReadString();
int readdate = br1.ReadInt32();
long readcislo = br1.ReadInt64();
long readsarze = br1.ReadInt64();
Console.WriteLine(readname);
Console.WriteLine(readdate);
Console.WriteLine(readcislo);
Console.WriteLine(readsarze);
}
}
For an explanation of the while condition see e.g. here

Read file that's already used by another process

I have a C# app that tries to read a log file which is being written to by another app. When I try to read the file, I get IOException
"The process cannot access the file ... because it is being used by
another process."
What I tried using so far are the following, but none of them fix the problem
var log = File.ReadAllText(logPath);
var stream = new FileStream(logPath, FileMode.Open);
using (var stream = File.Open(logPath, FileMode.Open))
{
}
try this:
FileStream logFileStream = new FileStream("c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
StreamReader logFileReader = new StreamReader(logFileStream);
while (!logFileReader.EndOfStream)
{
string line = logFileReader.ReadLine();
// Your code here
}
// Clean up
logFileReader.Close();
logFileStream.Close();
edited with MethodMan's suggestions
using(FileStream logFileStream = new FileStream(#"c:\test.txt", FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using(StreamReader logFileReader = new StreamReader(logFileStream))
{
string text = logFileReader.ReadToEnd();
// Your code..
}
}
You can do nothing, if the "another app" does not use Share.Read while creating/opening the file.

Save image in Existing PDF using iTextSharp. Not Working

I am using the below mentioned code. Due to some reasons I am not able to save the image in output PDF. is there anything I am missing ?
string imageFileName = Path.Combine(Application.StartupPath, "a.jpg");
var inputpdf = Path.Combine(Application.StartupPath, "b.pdf");
var outputpdf = Path.Combine(Application.StartupPath, "output.pdf");
using (Stream inputPdfStream = new FileStream(inputpdf, FileMode.Open, FileAccess.Read,
FileShare.Read))
{
using (Stream inputImageStream = new FileStream(imageFileName, FileMode.Open,
FileAccess.Read, FileShare.Read))
{
var reader = new PdfReader(inputPdfStream);
var stamper = new PdfStamper(reader, new FileStream(outputpdf, FileMode.Create),
'\0', true);
var pdfContentByte = stamper.GetOverContent(1);
iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(inputImageStream);
image.SetAbsolutePosition(10, 10);
pdfContentByte.AddImage(image);
stamper.Close();
}
}

Coludn't acces the XML file in Visual Studio c#

When executing the code below it shows an error at opening the file
it says the file dict.xml is already in use by some process
It shows error here
FileStream fs = new FileStream("dict.xml", FileMode.Open, FileAccess.Read);
FileStream fs = new FileStream("dict.xml", FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.GetElementsByTagName("dict");
for (i = 0; i <= xmlnode.Count - 1; i++)
{
w[i] = xmlnode[i].ChildNodes.Item(0).InnerText;
m[i] = xmlnode[i].ChildNodes.Item(1).InnerText;
}
If something else has a lock on the file you should still be able to read it if you specify FileShare.ReadWrite:
using (FileStream fs = new System.IO.FileStream("dict.xml", FileMode.Open, FileAccess.Read, FileShare.ReadWrite) {

Managed memory mapped file

My problem is that I can not open the file. In another process or in the same process!
Code:
var path = #"c:\work\mmf.dat";
var map = "testmap123";
var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
if (fs.Length == 0)
{
fs.SetLength(1024);
}
var sec = new MemoryMappedFileSecurity();
var mem = MemoryMappedFile.CreateFromFile(fs, map, fs.Length, MemoryMappedFileAccess.ReadWrite, sec, HandleInheritability.Inheritable, false);
// Problem here System.UnauthorizedAccessException
var tmp = MemoryMappedFile.OpenExisting(map, MemoryMappedFileRights.FullControl, HandleInheritability.Inheritable);
Try the following:
var path = #"c:\diverse\mmf.dat";
var map = "testmap123";
using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))
{
if (fs.Length == 0)
{
fs.SetLength(1024);
}
}
using (var mem = MemoryMappedFile.CreateFromFile(path, FileMode.Open, map, 1024, MemoryMappedFileAccess.Read))
{
using (var tmp = MemoryMappedFile.OpenExisting(map))
{
//work with tmp
}
}

Categories