У меня проблема при использовании libzip. Я использую Linux и установил библиотеку, используя sudo apt-get install libzip2 libzip-dev
(так что это не последняя версия).
Вот мой код:
#include <iostream>
#include <zip.h>
#include <unistd.h>
#include <sys/stat.h>
#define ZIP_ERROR 2
using namespace std;
bool isFilePresent(string const& path)
{
struct stat *buf;
return(stat(path.c_str(), buf)==0);
}
int main(void)
{
struct zip *zip;
struct zip_source *zip_source;
int err(0);
string zipFile("filesZip/zipTest");
string fileToZip("filesToZip/test1");
string fileToZip2("filesToZip/test2");
char tmp[] = "filesZip/zipTest\0";
// Test if the file is present
if(isFilePresent(zipFile))
{
// if(remove(tmp) != 0)
if(remove(zipFile.c_str()) != 0)
{
return ZIP_ERROR;
}
}
// Open a zip archive
zip = zip_open(zipFile.c_str(), ZIP_CREATE, &err);
// if there is an error on the opening
if(err != ZIP_ER_OK)
{
cout << "error when opening" << endl;
return ZIP_ERROR;
}
// If the zip file is not open
if(zip == NULL)
{
zip_close(zip);
cout << "error when zip opens" << endl;
return ZIP_ERROR;
}
// zip_source_file zip a file so that it can be added to the zip
if((zip_source = zip_source_file(zip, fileToZip.c_str(), (off_t)0, (off_t)0))== NULL)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when zipping file1" << endl;
return ZIP_ERROR;
}
// Add the zipped file to the zip
if(zip_add(zip, fileToZip.c_str(), zip_source)==-1)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when adding file1" << endl;
return ZIP_ERROR;
}
// zip_source_file zip a file so that it can be added to the zip
if((zip_source = zip_source_file(zip, fileToZip2.c_str(), (off_t)0, (off_t)0))== NULL)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when zipping file2" << endl;
return ZIP_ERROR;
}
if(zip_add(zip, fileToZip2.c_str(), zip_source)==-1)
{
zip_close(zip);
zip_source_free(zip_source);
cout << "pb when adding file2" << endl;
return ZIP_ERROR;
}
// sleep(180);
// Closing the archive
zip_close(zip);
return 0;
}
Этот код должен взять два файла в папке filesToZip и сжать их в файл zipTest в папке filesZip.
Для этого сначала он проверяет, существует ли уже файл zipTest. Если да, то он его удаляет. Затем он открывает zip-архив, заархивирует файлы для добавления и добавляет их в архив перед закрытием архива.
Итак, моя проблема:
когда файлы zip-архива Zip / zipTest не существуют, он работает нормально. когда файлы zip-архива Zip / zipTest действительно существуют, я получил дамп ядра.
Что я пробовал до сих пор:
- Я думал, это потому, что я использовал строки для имен файлов. Я пробовал с char, и ничего не изменилось
- тогда я подумал, что это потому, что задача удаления не была завершена, и тогда может возникнуть конфликт. Поэтому я кладу sleep (180) (в секундах) после каждого вызова функции. Ничего не изменилось
- Еще я пробовал положить в архив только один файл. Ничего не менял
- I ran gdb to see what was happening. I tried both when the zip archive already existed and did not.
- if the archive didn't already existed : everything went smoothly till the return 0 and then i saw that the program redefined fileToZip and fileToZip2, then did another return 0 then would stop.
- если архив уже существует: он делает то же самое, но затем сообщает, что не может найти границы текущей функции. (я прочитал здесь Это означает, что GDB не имеет отладочной информации и недоволен этим ..)
Кто-нибудь знает, в чем может быть моя проблема?