Windows二進位制檔案合併工具

NO IMAGE

    為了合併一些媒體檔案,網上搜了半天,沒找到合適的。自己用 Qt 寫了一個小工具 qjoin ,輸入一個文字檔案(內容是每行一個檔案)和輸出檔名,程式自動把列表中的檔案進行二進位制合併。

    程式碼很簡單,如下:

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
if(argc < 3)
{
qDebug() << "qjoin input_list_file output_file\n"
"  input_list_file - a text file contains"
"the files to be joined\n      "
"every line will be treated as a file name\n"
"  output_file - the output(joined) file name";
return 1;
}
QFile list_file(argv[1]);
if(!list_file.open(QIODevice::ReadOnly))
{
qDebug() << "open " << argv[1] << " failed - " << list_file.errorString();
return 1;
}
QFile out(argv[2]);
if(!out.open(QIODevice::WriteOnly | QIODevice::Truncate))
{
qDebug() << "open " << argv[2] << " for write failed - " << out.errorString();
}
QTextStream ts(&list_file);
int buf_size = 512*1024;
char *buffer = (char*)malloc(buf_size);
qint64 ret = 0;
while(!ts.atEnd())
{
QString filename = ts.readLine().trimmed();
if(filename.isEmpty()) continue;
QFile file(filename);
if(!file.open(QIODevice::ReadOnly))
{
qDebug() << "open " << filename << " for read failed - " << file.errorString();
goto err_exit;
}
while(!file.atEnd())
{
ret = file.read(buffer, buf_size);
if(ret == -1)
{
qDebug() << "read " << filename << " failed - " << file.errorString();
file.close();
goto err_exit;
}
if(ret == 0) break;
if( ret != out.write(buffer, ret) )
{
qDebug() << "write " << argv[2] << " failed - " << out.errorString();
file.close();
goto err_exit;
}
}
file.close();
}
list_file.close();
out.flush();
out.close();
qDebug() << "join successfully!";
return 0;
err_exit:
list_file.close();
out.close();
out.remove();
qDebug() << "join failed!";
return 1;
}