麻蛋,本来是想用IE缓冲文件提取页游美术资源的
结果每次只能复制一张图片搞毛线啊
所以写了下面这个提取器
路径无论是 "/" 或 "\\" 都没所谓反正会转
其实就是利用cmd进行复制
先提取文件名到1.txt
然后生成copy命令
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class QFile;
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
signals:
void readyReadStandardOutput();
void finished();
private slots:
void on_ExtractButton_clicked();
void on_DirectoryButton_clicked();
void on_coverBox_stateChanged(int arg1);
private:
void setUIEnabled(bool isEnabled);
void startExtraction(const QString &savePath, const QString &buffPath);
void writeCommand(QFile &file, const QString &savePath);
void writeDirectory(QFile &file, const QString &buffPath);
QString replace(const QString &path);
QStringList readTextFile(const QString &path);
void writeTextFile(const QString &path, const QStringList &command);
QString command(const QString path, const QString ©Path);
QString synthesisCommand(const QString &path, const QString ©Path);
private:
Ui::MainWindow *ui;
QString _applicationDirPath;
bool _check;
};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtConcurrent>
#include <QFileDialog>
#include <QTextStream>
#include <QFileInfo>
#include <QProcess>
#include <QFile>
#include <QDir>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
_check = false;
_applicationDirPath = QCoreApplication::applicationDirPath();
// QLabel *label = new QLabel("123",this);
// connect(this, &MainWindow::readyReadStandardOutput,this,[=,&label](){
// QProcess *pro = (QProcess*)QObject::sender();
// label->setText("321");
// //ui->statusBar->showMessage(QString::fromLocal8Bit( pro->readAllStandardOutput()));
// });
connect(this,&MainWindow::finished, this, [=](){
setUIEnabled(true);
});
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_ExtractButton_clicked()
{
QString save = ui->SaveEidt->text();
QString buff = ui->BuffEdit->text();
if(save.isEmpty() || buff.isEmpty())
return;
save = replace(save);
buff = replace(buff);
setUIEnabled(false);
QtConcurrent::run([=,save,buff](){
startExtraction(save,buff);
});
}
void MainWindow::on_DirectoryButton_clicked()
{
QString dirName = QFileDialog::getExistingDirectory(this);
if(!dirName.isEmpty())
ui->SaveEidt->setText(dirName);
}
void MainWindow::setUIEnabled(bool isEnabled)
{
ui->ExtractButton->setEnabled(isEnabled);
ui->SaveEidt->setEnabled(isEnabled);
ui->BuffEdit->setEnabled(isEnabled);
ui->DirectoryButton->setEnabled(isEnabled);
ui->coverBox->setEnabled(isEnabled);
}
void MainWindow::startExtraction(const QString &savePath, const QString &buffPath)
{
QFile file(_applicationDirPath+"/1.bat");
if(!file.open(QIODevice::Text|QIODevice::WriteOnly)){
return ;
}
if(!file.exists()){
file.close();
}
writeDirectory(file,buffPath);
QProcess *pro = new QProcess(this);
//不知为何没有输出
// connect(pro, &QProcess::readyReadStandardOutput,this, &MainWindow::readyReadStandardOutput);
// pro->start(_applicationDirPath+"/1.bat");
pro->execute(_applicationDirPath+"/1.bat");
writeCommand(file, savePath);
pro->execute(_applicationDirPath+"/2.bat");
emit finished();
QFile::remove(_applicationDirPath+"/1.bat");
QFile::remove(_applicationDirPath+"/2.bat");
QFile::remove(_applicationDirPath+"/1.txt");
//disconnect(pro,&QProcess::readyReadStandardOutput,this,&MainWindow::readyReadStandardOutput);
}
void MainWindow::writeCommand(QFile &file, const QString &savePath)
{
while(!file.exists());
auto list = this->readTextFile(_applicationDirPath+"/1.txt");
for(int i=0;i<list.length();i++){
list[i] = this->command(list[i],savePath);
}
this->writeTextFile(_applicationDirPath+"/2.bat",list);
}
void MainWindow::writeDirectory(QFile &file, const QString &buffPath)
{
QTextStream cin(&file);
cin.setCodec("ANSI");
cin << "F:"<<endl;
cin << "cd "+buffPath<<endl;
cin << "dir *.* /s /b > "+_applicationDirPath+"/1.txt"<<endl;
file.close();
}
QString MainWindow::replace(const QString &path)
{
return ((QString)path).replace("/","\\");
}
QStringList MainWindow::readTextFile(const QString &path){
QStringList str;
QFile file(path);
if(!file.open(QIODevice::Text|QIODevice::ReadOnly)){
return str;
}
QTextStream out(&file);
while(!out.atEnd()){
str << out.readLine();
}
file.close();
return str;
}
void MainWindow::writeTextFile(const QString &path, const QStringList &command){
QFile file(path);
if(!file.open(QIODevice::Text|QIODevice::ReadWrite)){
return;
}
QTextStream cin(&file);
cin.setCodec("ANSI");
foreach(auto str, command){
cin << str<<endl;
}
file.close();
}
QString MainWindow::command(const QString path,const QString ©Path){
QFileInfo info(path);
QString Folder = ((QString)copyPath).replace("\\","/") + "/"+info.suffix();
QDir dir;
//如果没有文件夹
if(!dir.exists(Folder)){
//如果创建失败
if(!dir.mkdir(Folder)){
return synthesisCommand(path,Folder);
// file.setFileName(QString("\"%2\\%3\"").arg(copyPath).arg(QFileInfo(path).fileName()));
// if(file.exists() && !_check)
// return "-1";
// return QString("copy \"%1\" %2").arg(path).arg(file.fileName());
}
}
Folder = Folder.replace("/","\\");
return synthesisCommand(path,Folder);
/*file.setFileName(QString("\"%2\\%3\"").arg(Folder).arg(QFileInfo(path).fileName()));
if(file.exists() && !_check)
return "-1";
return QString("copy \"%1\" %2").arg(path).arg(file.fileName());*/
}
QString MainWindow::synthesisCommand(const QString &path, const QString ©Path)
{
QFile file(QString("\"%2\\%3\"").arg(copyPath).arg(QFileInfo(path).fileName()));
if(file.exists() && !_check)
return "";
return QString("copy \"%1\" %2").arg(path).arg(file.fileName());
}
void MainWindow::on_coverBox_stateChanged(int arg1)
{
if(arg1 == Qt::Checked){
_check = true;
}
else{
_check = false;
}
}