Windows二进制文件合并工具

    为了合并一些媒体文件,网上搜了半天,没找到合适的。自己用 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;
}


你可能感兴趣的:(Qt,泛技术)