Answer the question
In order to leave comments, you need to log in
How to check the integrity of a ZIP archive (specifically an Office Open XML file) programmatically (in C or C++)?
Good afternoon!
Sometimes it happens that part of a file in the Office Open XML format (docx, pptx, xslx) breaks. It is known that these files are essentially ZIP archives (perhaps with a special compression algorithm?).
We need a library (preferably with a C/C++ example) that can check (not unpack) the integrity of this ZIP archive, that is, check the CRC checksum of the contents of the archive.
Answer the question
In order to leave comments, you need to log in
Used the miniz library and example2
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;
}
In theory, if this is a renamed zip file, then any console archiver that has the "check archive integrity" command will do.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question