Am using Qt for my development. Below is the code which i used for creating tar.gz files.
Few times while creating tar.gz it creates an empty file T with 0 bytes of memory usage. What am i doing wrong here
/**
* @brief Create a tar bundle
* @param QMap<SaveFileName, SaveFilePath> m_SaveFile - url of file
* @param QMap<FileName , FilePath> m_FilesToTar - Files to be tar bundled
* @return 0 on failure 1 on success
*/
bool SSL_ED::CreateTarBundle(QMap<QString, QString> m_SaveFile, QMap<QString, QString> m_FilesToTar, unsigned int i_FileType)
{
if( m_SaveFile.size() != 1 ) {
qDebug() << "Invalid tar bundle save location or filename";
return false;
}
if ( !QDir::setCurrent(m_SaveFile.value(m_SaveFile.firstKey())) ) {
qDebug() << "Bundle creation: Unable to change dir";
return false;
}
struct archive *a;
struct archive_entry *entry;
struct stat st;
a = archive_write_new();
archive_write_add_filter_gzip(a);
archive_write_set_format_pax_restricted(a);
archive_write_open_filename(a, m_SaveFile.firstKey().toUtf8().constData());
QMapIterator<QString, QString> mItr(m_FilesToTar);
while(mItr.hasNext())
{
mItr.next();
if( !QDir::setCurrent(mItr.value()) ) {
qDebug() << "Bundle creation: Unable to change dir to read file";
return false;
}
const char *filename = mItr.key().toUtf8().constData();
stat(filename, &st);
entry = archive_entry_new();
archive_entry_set_pathname(entry, filename);
archive_entry_set_size(entry, st.st_size);
archive_entry_set_filetype(entry, AE_IFREG);
archive_entry_set_perm(entry, 0644);
archive_write_header(a, entry);
QFile file(mItr.value()+mItr.key());
qDebug() << "FileName:"<<mItr.value()+mItr.key();
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "Unable to read file for creating tar:"<<file.fileName();
return false;
}
char c_ReadData[512000];
qint64 i_ReadData = file.read(c_ReadData,512000);
while( i_ReadData > 0 )
{
int i = archive_write_data(a, c_ReadData, static_cast<size_t>(i_ReadData));
if ( i == ARCHIVE_RETRY || i == ARCHIVE_WARN ||
i == ARCHIVE_FAILED || i == ARCHIVE_FATAL )
{
qDebug() <<"Fatal error while creating tar file: "<<i;
return false;
}
i_ReadData = file.read(c_ReadData,512000);
}
file.close();
file.flush();
archive_entry_free(entry);
}
if ( ARCHIVE_OK != archive_write_close(a) ){
return false;
}
if ( ARCHIVE_OK != archive_write_free(a) ){
return false;
}
return true;
}