How to unzip a zip file with qt fuction?



  • Is there a way to unzip a zip file with a qt fuction..? ( lets say the zip file has some songs in it )
    I have heard about quncompress.. when i tried this:

    @QFile infile(QDir::homePath()+"/Downloads/ena.zip");
    QFile outfile(QDir::homePath()+"/Downloads");
    infile.open(QIODevice::ReadOnly);
    outfile.open(QIODevice::WriteOnly);
    QByteArray uncompressedData = infile.readAll();
    QByteArray compressedData = qUncompress(uncompressedData);
    outfile.write(compressedData);
    infile.close();
    outfile.close();@

    an error like this appeared:

    @qUncompress: Z_DATA_ERROR: Input data is corrupted
    @


  • Moderators

    The archives have typically some file management involved. That is not part of zlib.
    Some useful information you may find "here":http://qt-project.org/faq/answer/how_to_compress_data_with_qt

    [quote]
    How to compress data with Qt?

    Qt uses the zlib library to provide compression. This means that you can compress and decompress your bytearrays of data using Qt. See the documentation on qUncompress() [qt.nokia.com]

    Note that this does not produce file archives that can be opened by other applications, to do that you will need to write it yourself using Qt’s custom file handling system [qt.nokia.com] or use a 3rd party library.
    [/quote]



  • i didn't want to use something that is not in the default library.. but it seems i will.. i will try to use Quazip.. if i find any difficulties i will post in this thread.. :)


  • Moderators

    zlib != zip

    zip usually does contain meta information about files stored which is not handled at all by qUncompress, which just takes a chunk of memory and returns another chunk that is compressed.



  • Go with Quazip.



  • [quote author="matti-" date="1345476621"]Go with Quazip.[/quote]

    I am bringing this post back.

    1. I downloaded quazip
    2. i copied the folder quazip to my projects src folder
    3. i included at my .pro file all the headers and sources of quazip

    @HEADERS += src/quazip/crypt.h
    src/quazip/ioapi.h
    src/quazip/quazip.h
    src/quazip/quazipfile.h
    src/quazip/quazipfileinfo.h
    src/quazip/quazipnewinfo.h
    src/quazip/unzip.h
    src/quazip/zip.h
    src/quazip/JlCompress.h
    src/quazip/quaadler32.h
    src/quazip/quachecksum32.h
    src/quazip/quacrc32.h
    src/quazip/quagzipfile.h
    src/quazip/quaziodevice.h
    src/quazip/quazip_global.h
    src/quazip/quazipdir.h

    SOURCES += src/quazip/quazip.cpp
    src/quazip/quazipfile.cpp
    src/quazip/quazipnewinfo.cpp
    src/quazip/quazipfileinfo.cpp
    src/quazip/quazipdir.cpp
    src/quazip/quaziodevice.cpp
    src/quazip/quagzipfile.cpp
    src/quazip/quacrc32.cpp
    src/quazip/qioapi.cpp
    src/quazip/unzip.c
    src/quazip/JlCompress.cpp
    src/quazip/zip.c@

    1. i added the headers
      @#include "quazip/quazip.h"
      #include "quazip/quazipfile.h"@
    2. And added this code
      @static bool extract(const QString & filePath, const QString & extDirPath, const QString & singleFileName = QString("")) {

    QuaZip zip(filePath);

    if (!zip.open(QuaZip::mdUnzip)) {
    qWarning("testRead(): zip.open(): %d", zip.getZipError());
    return false;
    }

    zip.setFileNameCodec("IBM866");

    qWarning("%d entries\n", zip.getEntriesCount());
    qWarning("Global comment: %s\n", zip.getComment().toLocal8Bit().constData());

    QuaZipFileInfo info;

    QuaZipFile file(&zip);

    QFile out;
    QString name;
    char c;
    for (bool more = zip.goToFirstFile(); more; more = zip.goToNextFile()) {

    if (!zip.getCurrentFileInfo(&info)) {
        qWarning("testRead(): getCurrentFileInfo(): %d\n", zip.getZipError());
        return false;
    }
    
    if (!singleFileName.isEmpty())
        if (!info.name.contains(singleFileName))
            continue;
    
    if (!file.open(QIODevice::ReadOnly)) {
        qWarning("testRead(): file.open(): %d", file.getZipError());
        return false;
    }
    
    name = QString("%1/%2").arg(extDirPath).arg(file.getActualFileName());
    
    if (file.getZipError() != UNZ_OK) {
        qWarning("testRead(): file.getFileName(): %d", file.getZipError());
        return false;
    }
    
    //out.setFileName("out/" + name);
    out.setFileName(name);
    
    // this will fail if "name" contains subdirectories, but we don't mind that
    out.open(QIODevice::WriteOnly);
    // Slow like hell (on GNU/Linux at least), but it is not my fault.
    // Not ZIP/UNZIP package's fault either.
    // The slowest thing here is out.putChar(c).
    while (file.getChar(&c)) out.putChar(c);
    
    out.close();
    
    if (file.getZipError() != UNZ_OK) {
        qWarning("testRead(): file.getFileName(): %d", file.getZipError());
        return false;
    }
    
    if (!file.atEnd()) {
        qWarning("testRead(): read all but not EOF");
        return false;
    }
    
    file.close();
    
    if (file.getZipError() != UNZ_OK) {
        qWarning("testRead(): file.close(): %d", file.getZipError());
        return false;
    }
    

    }

    zip.close();

    if (zip.getZipError() != UNZ_OK) {
    qWarning("testRead(): zip.close(): %d", zip.getZipError());
    return false;
    }

    return true;
    }@

    After compiling all i get is 200 warnings=errors all like

    @warning: 'static bool JlCompress::compressFiles(QString, QStringList)' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes]
    bool JlCompress::compressFiles(QString fileCompressed, QStringList files)

    error: undefined reference to `_imp___ZNK6QuaZip11getZipErrorEv'@

    What am i doing wrong?
    steps/code taken from http://stackoverflow.com/questions/2598117/zipping-a-folder-file-using-qt
    ^



  • I have to add here:
    Seems like there is a problem with quazip on qt5 at windows 7+

    https://qt-project.org/forums/viewthread/26167/

    At quazip site
    Requirements
    Just zlib and Qt 4/5. Well, Qt 4 depends on zlib anyway, but you will need zlib headers to compile QuaZIP. With Qt5 sometimes you need the zlib library as well (on Windows, for example)

    But i don't get what files of the zlib folder should i copy and what to include ...



  • Why don't you just do the unzipping with a system call to the unzip executable?
    @QString program = "./path/to/Qt/examples/widgets/analogclock";
    QStringList arguments;
    arguments << "-style" << "motif";

    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);@

    Example from Qt Doc "link":http://qt-project.org/doc/qt-4.8/qprocess.html.



  • [quote author="Seba" date="1387743195"]Why don't you just do the unzipping with a system call to the unzip executable?
    @QString program = "./path/to/Qt/examples/widgets/analogclock";
    QStringList arguments;
    arguments << "-style" << "motif";

    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);@

    Example from Qt Doc "link":http://qt-project.org/doc/qt-4.8/qprocess.html.[/quote]

    this works with linux, with the command unzip
    but i want it for Windows now



  • Hi, you can use QZipReader and QZipWriter classes directly which in gui-private module.

    @
    QT+=gui-private
    @



  • QProcess works in Windows! You should only replace the strings with the program path and arguments you want to use in your computer/platform.

    Possible example with Windows:
    @QString program = "c:/program files/winrar/winrar.exe";
    QStringList arguments;
    arguments << "c:/documents/my_zip_file.zip";
    QProcess *myProcess = new QProcess(parent);
    myProcess->start(program, arguments);
    @

    You have to see the program documentation in order to give the correct command line arguments to do all the unzipping in the background (if this is what you want).



  • @Seba really?

    Do you expect to add a dependency to winrar for his program?

    There is no pre-installed MS windows executable for unzipping as far as I know.



  • Has anyone had any luck unzipping a large zip file (250MB) in iOS?

    I have successfully used QZipReader and QZipWriter classes for handling large files (following Bryan's excellent tutorial at: http://voidrealms.com/index.php?r=tutorial/view&id=365) on OSX and Android, but when I deploy my project to iOS, I get a memory error. In particular, my iPad does not want to fetch all the inflated data into memory before writing it to file.

    Has anyone had any success accessing zlib's inflate method within a loop that reads chunks / writes them to file in each pass, until Z_STREAM_END is returned?



  • Has anyone had any luck unzipping a large zip file (250MB) in iOS?

    I have successfully used QZipReader and QZipWriter classes for handling large files (following Bryan's excellent tutorial at: http://voidrealms.com/index.php?r=tutorial/view&id=365) on OSX and Android, but when I deploy my project to iOS, I get a memory error. In particular, my iPad does not want to fetch all the inflated data into memory before writing it to file.

    Has anyone had any success accessing zlib's inflate method within a loop that reads chunks / writes them to file in each pass, until Z_STREAM_END is returned?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.