1

I have implemented a code that creates a zip file. The zip file gets successfully created but however when I manually (without code) try and check the contents of the zip file by unzipping it, I get an error saying No archive found and the unzipping process stops.. Why is this problem occuring.

Here is my code

#include <QCoreApplication>
#include <QByteArray>
#include <QBitArray>
#include <QString>
#include <QDebug>
#include <QFile>

void Zip(QString filename , QString zipfilename);

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

  Zip("C:\\programs\\zipping_qt\\sample.txt",
      "C:\\programs\\zipping_qt\\samples.zip");
  qDebug() << "Done zipping";

  return a.exec();
}

void  Zip (QString filename , QString zipfilename){
  QFile infile(filename);
  QFile outfile(zipfilename);
  infile.open(QIODevice::ReadOnly);
  outfile.open(QIODevice::WriteOnly);
  QByteArray uncompressedData = infile.readAll();
  QByteArray compressedData = qCompress(uncompressedData,9);
  outfile.write(compressedData);
  infile.close();
  outfile.close();
}  
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
Parth Doshi
  • 4,200
  • 15
  • 79
  • 129

1 Answers1

3

qCompress does not create zip files. It uses zlib to create a compressed data block. Such blocks can only be directly decompressed by using qUncompress, or by calling zlib directly. There's no standard stand-alone utility that does such decompression. Not even gunzip does it, since zlib-style API uses different headers than gzip.

There are some other nitpicks about your code:

  1. There's no need to call a.exec() if you don't need to run the event loop.

  2. There's no need to explicitly close the files, since QFile is a proper C++ class and implements RAII.

  3. You never check if open, readAll or write succeeds.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • can u provide an example or tutorial for Qt that does zipping and unzipping for text files or any kind of files for that matter – Parth Doshi Feb 25 '14 at 17:13
  • @ParthDoshi There's nothing Qt specific here, since Qt doesn't provide this functionality. You can use one of the multitude of zip file support libraries out there. Say [libzip](http://www.nih.at/libzip/), or look at [this question](http://stackoverflow.com/q/262899/1329652). – Kuba hasn't forgotten Monica Feb 25 '14 at 18:49