How to check integrity of ZIP-file (Office Open XML file) with C/C++?

547 views Asked by At

Good day!

Sometimes it happens that some part of the file in the Office Open XML format (docx, pptx, xslx) beats. It is known that these files are in fact the ZIP-files (possibly with a special compression algorithm?).

Is there any library (preferably with an example in C/C ++), which can check (not unpack) the integrity of the ZIP-file (check the CRC checksum of the archive contents)?

1

There are 1 answers

0
askar On

I have used miniz:

BOOL isZipCorrect(const char *pFilename) {
  int ci;
  void *p;
  size_t uncomp_size;
  mz_bool status;
  mz_zip_archive zip_archive;
  // open the archive.
  memset(&zip_archive, 0, sizeof(zip_archive));
  status = mz_zip_reader_init_file(&zip_archive, pFilename, 0);
  if (!status) {
      return FALSE;
  }

  // go throw all files
  for (ci = 0; ci < (int)mz_zip_reader_get_num_files(&zip_archive); ci++) {
    // get information about each file in the archive
    mz_zip_archive_file_stat file_stat;
    if (!mz_zip_reader_file_stat(&zip_archive, ci, &file_stat))
    {
      mz_zip_reader_end(&zip_archive);
      return FALSE;
    }

    // try to extract this file
    p = mz_zip_reader_extract_file_to_heap(&zip_archive, file_stat.m_filename, &uncomp_size, 0);
    if (!p)
    {
      mz_zip_reader_end(&zip_archive);
      return FALSE;
    }

    // we're done.
    mz_free(p);
  }

  // close the archive, freeing any resources it was using
  mz_zip_reader_end(&zip_archive);

  // return ok state
  return TRUE;
}