复制时可执行文件以某种方式损坏

时间:2015-04-09 16:08:20

标签: c++ qt file file-copying

我正在使用Qt的QNetworkAccessManager从一个地方(当前是本地机器,但将来它将是一个HTTP服务器)下载文件并暂时将其存储在TEMP文件(linux ubuntu)中。我发现的问题是文件(可执行文件)在此过程中被破坏:当我尝试将文件作为可执行文件运行时,它返回有问题的交叉编译的经典错误。

现在这很有趣,因为该文件是嵌入式Linux设备的可执行文件 - 我正在将可执行文件下载到我的TEMP中,以便稍后将其发送到设备。但是,当发生这种情况时(使用FileZilla),会出现此错误消息:

  

./ re8k_interface-tgt:第1行:语法错误:意外的单词(期待“)”)

现在我知道将原始文件复制到设备并运行它是正常的,所以我知道它与复制文件的过程有关,无论是在下载时还是在写入QFile对象时。以下是我现在正在做的事情:

//Call to download
QUrl ulrTemp("//" + downloadUrls[downloadStep].arg(ui->sbID->text()));
ulrTemp.setScheme("file");

qDebug() << "Downloading from" << ulrTemp;

poReply = downloadNetworkManager->get(QNetworkRequest(ulrTemp));

connect(poReply,SIGNAL(downloadProgress(qint64,qint64)),this,SLOT(slotTransferProgress(qint64,qint64)));
connect(poReply,SIGNAL(error(QNetworkReply::NetworkError)),this,SLOT(slotTransferError(QNetworkReply::NetworkError)));


//When finished
QByteArray downloadedData;
downloadedData = reply->readAll();

reply->deleteLater();
poReply->deleteLater();

static const QString tempFilePath = QDir::tempPath();

QFile file(tempFilePath + "/" + downloadNames[downloadStep]);

if (!file.open(QFile::WriteOnly | QFile::Truncate))
{
    qDebug() << "Failure opening temp file to write: " << file.fileName();

    return;
}

QDataStream stream(&file);

stream << downloadedData;

file.close();

P.s。:我知道设置权限的必要性

复制的文件大小与原始大小完全匹配。那么,我看不到的问题在哪里?

1 个答案:

答案 0 :(得分:2)

将字节数组写入QDataStream时,也会写入数组的长度。

只是不要使用数据流,直接使用QFile或更好QTemporaryFile

下面的示例演示了如何利用C ++ 11和Qt 5使其变得非常简单:

Writing to: /var/folders/yy/2tl/T/download-29543601.L91178
Wrote  55015 bytes.
Downloaded 55015 of -1 bytes
Wrote  7572 bytes.
Wrote  6686 bytes.
Wrote  5104 bytes.
Downloaded 74377 of 74377 bytes
Successfully wrote /var/folders/yy/2tl/T/download-29543601.L91178
#include <QCoreApplication>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QTemporaryFile>
#include <QUrl>
#include <QByteArray>
#include <QTextStream>
#include <QDebug>
#include <cstdio>

QTextStream out(stdout);
QTextStream in(stdin);

int main(int argc, char *argv[])
{
   QCoreApplication a(argc, argv);
   QNetworkAccessManager mgr;

   auto url = QUrl("http://stackoverflow.com/questions/29543601/"
                   "executable-getting-somehow-corrupted-when-being-copied");
   auto reply = mgr.get(QNetworkRequest(url));

   QTemporaryFile file;
   if (!file.open()) {
      qDebug() << "Can't open file for writing.";
      return -1;
   }
   out << "Writing to: " << file.fileName() << endl;

   QObject::connect(reply, &QNetworkReply::downloadProgress, [](qint64 rx, qint64 total) {
      qDebug() << "Downloaded" << rx << "of" << total << "bytes";
   });

   QObject::connect(reply, &QIODevice::readyRead, [reply, &file]{
      auto data = reply->readAll();
      auto written = file.write(data);
      if (data.size() != written) {
         qDebug() << "Write failed, wrote" << written << "out of" << data.size() << "bytes.";
      } else {
         qDebug() << "Wrote " << written << "bytes.";
      }
   });

   QObject::connect(reply, &QNetworkReply::finished, [reply, &file]{
      if (reply->error() != QNetworkReply::NoError) {
         qDebug() << "The request was unsuccessful. Error:" << reply->error();
         qApp->quit();
         return;
      }
      if (file.flush()) {
         out << "Successfully wrote " << file.fileName();
         out << "\nPress Enter to remove the file and exit." << flush;
         in.readLine();
      } else {
         qDebug() << "The file flush has failed";
      }
      qApp->quit();
   });

   return a.exec();
}