我已经为当前正在使用的各种不同(WinForms)应用程序将文件复制例程构建到公共库中。我构建的工具实现了常用的#include <iostream>
#include <stdio.h>
using namespace std;
class Machine {
public:
// attributes
int x, j;
int const constexpr static size = 2;
int A1[size][size];
int A2[size][size];
// functions
// creategrid: user sets the state at the beginning
auto creategrid() {
for (x = 0; x < size; x++) {
for (j = 0; j < size; j++) {
std::cout << "1 or 0";
printf("\n");
std::cin >> A1[x][j];
}
}
}
auto statetransition() {
int a, m;
for (a = 0; a < size; a++) {
for (m = 0; m < size; m++) {
if (A1[a][m] == 0) {
A2[a][m] = 1;
}
A2[a][m] = 0;
printf("%d ", A2[a][m]);
}
}
printf("\n");
}
};
int main(){
Machine MA;
MA.createfield();
MA.statetransition();
}
方法,以在显示进度的同时实际执行文件复制,这似乎效果很好。
我遇到的唯一真正的问题是,因为我正在执行的大多数文件复制都是出于存档目的,所以一旦复制了文件,我想“验证”文件的新副本。我采用以下方法进行比较/验证。我敢肯定,你们中的许多人会很快看到“问题”在哪里:
CopyFileEx
此Public Shared Function CompareFiles(ByVal File1 As IO.FileInfo, ByVal File2 As IO.FileInfo) As Boolean
Dim Match As Boolean = False
If File1.FullName = File2.FullName Then
Match = True
Else
If File.Exists(File1.FullName) AndAlso File.Exists(File2.FullName) Then
If File1.Length = File2.Length Then
If File1.LastWriteTime = File2.LastWriteTime Then
Try
Dim File1Hash As String = HashFileForComparison(File1)
Dim File2Hash As String = HashFileForComparison(File2)
If File1Hash = File2Hash Then
Match = True
End If
Catch ex As Exception
Dim CompareError As New ErrorHandler(ex)
CompareError.LogException()
End Try
End If
End If
End If
End If
Return Match
End Function
Private Shared Function HashFileForComparison(ByVal OriginalFile As IO.FileInfo) As String
Using BufferedFileReader As New IO.BufferedStream(File.OpenRead(OriginalFile.FullName), 1200000)
Using MD5 As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim FileHash As Byte() = MD5.ComputeHash(BufferedFileReader)
Return System.Text.Encoding.Unicode.GetString(FileHash)
End Using
End Using
End Function
方法首先检查一些“简单”元素:
CompareFiles()
)但是,您猜到了,这是表演受到打击的地方。特别是对于大文件,True
方法的MD5.ComputeHash
方法可能要花费一些时间-500MB文件大约需要1.25分钟,总共需要大约2.5分钟才能计算出两个哈希值以进行比较。有谁对如何更有效地验证文件的新副本有更好的建议?