Home > Blockchain >  Binary files get corrupted while unzipping with libzip
Binary files get corrupted while unzipping with libzip

Time:11-27

I was required to unzip .zip files in my Qt project. So I installed libzip. I wrote a function to unzip a .zip file given its location and destination directory path. The function is correctly able to unzip plain text files (like .json, .txt, and others) but it always corrupts any type of binary files like PNGs or MP4s. Here is the function I wrote:

void Converter::unzipFile(QString srcFilePath_, QString destinationDirectoryPath_) {
    std::string srcFilePath = srcFilePath_.toStdString();
    std::string destinationDirectoryPath = destinationDirectoryPath_.toStdString();

    char bufferStr[100];
    int error = 0;
    int fileHandle;

    struct zip *zipArchive = zip_open(srcFilePath.c_str(), 0, &error);
    struct zip_file *zippedFile;
    struct zip_stat zippedFileStats;

    if (not QDir().exists(destinationDirectoryPath_)) {
        QDir().mkpath(destinationDirectoryPath_);
    }

    if (zipArchive == NULL) {
        zip_error_to_str(bufferStr, sizeof(bufferStr), error, errno);
        std::cout << "[ERROR] Can not open the zip file: " << error <<  ":\n" << bufferStr;
    }

    for (int index = 0; index < zip_get_num_entries(zipArchive, 0); index  ) {
        if (zip_stat_index(zipArchive, index, 0, &zippedFileStats) == 0) {
            int zipFileNameLength = strlen(zippedFileStats.name);

            if (zippedFileStats.name[zipFileNameLength - 1] == '/') {  // i.e. folder
                QDir().mkpath(destinationDirectoryPath_   "/"   zippedFileStats.name);
            } else {  // i.e. file
                zippedFile = zip_fopen_index(zipArchive, index, 0);
                if (zippedFile == NULL) {
                    qDebug() << "[ERROR] Can not open the file in zip archive.";
                    continue;
                }

                fileHandle = open((destinationDirectoryPath   "/"   zippedFileStats.name).c_str(), O_RDWR | O_TRUNC | O_CREAT, 0644);
                if (fileHandle < 0) {
                    qDebug() << "[ERROR] Can not create the file (into which zipped data is to be extracted).";
                    continue;
                }

                int totalFileDataLength = 0;
                while (totalFileDataLength != (long long) zippedFileStats.size) {
                    int fileDataLength = zip_fread(zippedFile, bufferStr, 100);

                    if (fileDataLength < 0) {
                        qDebug() << "[ERROR] Can not read the zipped file.";
                        exit(1);
                    }

                    write(fileHandle, bufferStr, fileDataLength);
                    totalFileDataLength  = fileDataLength;
                }

                close(fileHandle);
                zip_fclose(zippedFile);
            }
        } else {
            qDebug() << "IDK what is here            
  • Related