获取两个文件的差异 [英] Get the differences of two files
问题描述
在.NET中,我需要一种比较两个文件的方法.我想到了一个代表差异的类:
In .NET, I need a way to compare two files. I thought of a class, which represents a diff:
public enum DiffEntryState
{
New,
Removed,
Changed
}
public class DiffEntry
{
public byte[] Bytes;
public long FileOffset;
public DiffEntryState State = BackupByteEntryState.Changed;
}
名称应该非常不言自明.我想为每个条目添加一个状态,以便区分第一个文件大于第二个文件的情况,反之亦然.
The names should be pretty self-explanatory. I thought of adding a State to each entry, so that I can distinguish between the cases were the first file is larger than the second or vice versa.
我想知道,是否存在一种通用且快速的方法来检索两个文件的逐字节差异.我只是为每个文件创建一个流,然后比较这些流的大块直到一个结束.有没有更好的方法,或者框架有内置的解决方案?请记住,我需要差异本身,而不仅仅是存在差异的反馈.
I'm wondering, if there is a common and fast way to retrieve the byte-by-byte differences of two files. I would simply create a stream for each file and compare chunks of these streams until one ends. Is there a better way, or does the Framework have a built-in solution? Keep in mind that I need the differences itself, not only the feedback that there ARE differences.
//
在问题上睡了一个晚上之后,我想我在这里采用了错误的方法.整个工具是一个备份解决方案,它将只能保存更改后的字节,从而减少了备份所需的总体空间.不会每次只保存压缩的14 MB文件,而只会保存200k或更少.
但是,在考虑了问题之后,我意识到仅逐字节保存差异是不够的.以文本为例:
这是一个字符串."这是一个字符串."
事实上,这里唯一的变化是从是"到是".但是我的方法将假定更改后的内容现在是字符串".如果这是在一个大文件的开头发生的,那么,这种方法是没有用的.显然,我需要一种索引文件并检测与原始文件相比所有已移动,复制或更改的块的方法.ew ...
推荐答案
看看 Diff.NET ,可能会有所帮助.
Take a look at Diff.NET,could be helpful .
这篇关于获取两个文件的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!