You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
98 lines
2.9 KiB
98 lines
2.9 KiB
//
|
|
// Created by 12038 on 2022/6/16.
|
|
//
|
|
#include <QProcess>
|
|
#include "DuplicateFiles.h"
|
|
#include <QUrl>
|
|
|
|
DuplicateFiles::DuplicateFiles(QObject *) {
|
|
|
|
}
|
|
|
|
DuplicateFiles::~DuplicateFiles() {
|
|
|
|
}
|
|
|
|
void DuplicateFiles::calMd5Slot(const QString &filePath) {
|
|
QByteArray md5 = calMd5(filePath);
|
|
emit md5Signal(md5);
|
|
}
|
|
|
|
QByteArray DuplicateFiles::calMd5(const QString &filePath) {
|
|
QFile file(filePath);
|
|
QByteArray md5 = "";
|
|
if(file.open(QIODevice::ReadOnly)){
|
|
QCryptographicHash hash(QCryptographicHash::Md5);
|
|
while(!file.atEnd()){
|
|
QByteArray buff = file.read(100 * 1024 * 1024);
|
|
hash.addData(buff);
|
|
}
|
|
md5 = hash.result().toHex();
|
|
file.close();
|
|
}
|
|
return md5;
|
|
}
|
|
|
|
void DuplicateFiles::getFilesSlot(const QString &filesDirPath) {
|
|
//获取文件夹下所有的文件
|
|
QStringList files= this->getFiles(filesDirPath);
|
|
//计算md5,并存到map中
|
|
QHash<QByteArray, QStringList> md5Results;
|
|
for (int i = 0; i < files.count(); i++){
|
|
QString fileName = files.at(i);
|
|
QByteArray fileMd5 = calMd5(fileName);
|
|
md5Results[fileMd5].append(fileName);
|
|
qDebug() <<"file:" << fileName << "md5:"<< fileMd5;
|
|
emit process(i + 1, files.count());
|
|
}
|
|
//找出map中value大于1的
|
|
for (QHash<QByteArray, QStringList>::iterator itr = md5Results.begin(); itr != md5Results.end(); itr++){
|
|
if(itr.value().count() > 1){
|
|
qDebug()<< "md5:"<<itr.key()<<"file:"<<itr.value();
|
|
duplicateFiles[itr.key()] = itr.value();
|
|
}
|
|
}
|
|
// emit filesSignal(files);
|
|
emit duplicateFilesSignal(duplicateFiles);
|
|
}
|
|
|
|
|
|
QStringList DuplicateFiles::getFiles(const QString &filesDirPath) {
|
|
QStringList files;
|
|
QDir filesDir(filesDirPath);
|
|
QList<QFileInfo> fileInfoList = filesDir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
|
|
for(int i = 0; i < fileInfoList.count(); i++){
|
|
QFileInfo fileInfo = fileInfoList.at(i);
|
|
if(fileInfo.isDir()){
|
|
QStringList subFiles= getFiles(fileInfo.absoluteFilePath());
|
|
files.append(subFiles);
|
|
}
|
|
else{
|
|
QString fileName = fileInfo.absoluteFilePath();
|
|
files.append(fileName);
|
|
}
|
|
}
|
|
return files;
|
|
}
|
|
|
|
void DuplicateFiles::getTextSlot(const QString &text) {
|
|
qDebug()<<"widget_2当前的text为:"<< text;
|
|
currentText = text;
|
|
}
|
|
|
|
void DuplicateFiles::openDirSlot() {
|
|
QFileInfo fileInfo(currentText);
|
|
QString filePath = fileInfo.absolutePath();
|
|
qDebug()<<"filePath"<<filePath;
|
|
QString path = filePath.replace("/", "\\");
|
|
qDebug()<<"path"<<path;
|
|
QProcess::startDetached("explorer " + path);
|
|
}
|
|
|
|
void DuplicateFiles::delActionTriggeredSlot() {
|
|
QFile file(currentText);
|
|
qDebug()<<"即将删除:"<<currentText;
|
|
emit delActionFeedbackSignal(file.remove());
|
|
qDebug()<<"删除成功";
|
|
}
|
|
|
|
|