linux下使用system调用解压命令实现解压文件的函数
说明:分享一个能够解压linux下大部分压缩文件的类,废话不多说啦,直接上代码
/**************************************.h***************************/
#ifndef ZLIBUNCOMPRESS_H
#define ZLIBUNCOMPRESS_H
#include <iostream>
#include <stdio.h>
using namespace std;
class ZlibUncompress
{
public:
ZlibUncompress();
~ZlibUncompress();
// 解压文件
static void uncompressFile(const string srcFilePath, const string destFilePath/*="./"*/, const string format=".tar");
};
#endif // ZLIBUNCOMPRESS_H
/*********************************************.cpp************************/
#include "ZlibUncompress.h"
#include <time.h>
ZlibUncompress::ZlibUncompress()
{
}
ZlibUncompress::~ZlibUncompress()
{
}
//************************************
// 函数名称: uncompressFile
// 函数说明:解压文件
// 返 回 值: void
// 参 数: const string srcFilePath
// 参 数: const string destFilePath
// 参 数: const string format=".tar"
// 作 者:ISMILELI
// 作成日期:2018/08/05
// 修改记录:
//************************************
void ZlibUncompress::uncompressFile(const string srcFilePath, const string destFilePath, const string format/*=".tar"*/)
{
time_t start,finish;
start = time(NULL);
string systemUnzlib = "tar xvf ";
string unzlibStr = "";
if(format.compare(".tar") == 0)//(以通过)
{
unzlibStr = systemUnzlib + srcFilePath + " -C" + destFilePath;
}
else if(format.compare(".tar.gz") == 0)//(以通过)
{
systemUnzlib = "tar zxvf ";
unzlibStr = systemUnzlib + srcFilePath + " -C " + destFilePath;
}
else if(format.compare(".gz") == 0)//目标文件
{
systemUnzlib = "gunzip "; // 没有找到解压后能保留源文件的方法
string gunzipStr = systemUnzlib + srcFilePath;
system(gunzipStr.c_str());
string mvStr = "mv" + string(srcFilePath,0,srcFilePath.length()-3) + destFilePath;
system(mvStr.c_str());
}
else if(format.compare(".bz2") == 0 || format.compare(".bz") == 0)//目标文件
{
systemUnzlib = "bunzip2 -k"; // 如果不需要保留源文件则把-k去掉就行
string bunzip2Str = systemUnzlib + srcFilePath;
system(bunzip2Str.c_str());
string mvStr = "mv" + srcFilePath.substr(0,srcFilePath.rfind('.')) + destFilePath;
system(mvStr.c_str());
}
else if(format.compare(".tar.bz2") == 0)//(以通过)
{
systemUnzlib = "tar jxvf ";
unzlibStr = systemUnzlib + srcFilePath + " -C " + destFilePath;
}
else if(format.compare(".tar.bz") == 0)//(以通过)
{
systemUnzlib = "tar jxvf ";
unzlibStr = systemUnzlib + srcFilePath + " -C " + destFilePath;
}
else if(format.compare(".Z") == 0)//
{
systemUnzlib = "uncompress ";
string uncompressStr = systemUnzlib + srcFilePath /*+ " -C " + destFilePath*/;
system(uncompressStr.c_str());
string mvStr = "mv" + srcFilePath.substr(0,srcFilePath.find_last_of('.')) + destFilePath;
system(mvStr.c_str());
}
else if(format.compare(".tar.Z") == 0)//(以通过)
{
systemUnzlib = "tar Zxvf ";
unzlibStr = systemUnzlib + srcFilePath + " -C " + destFilePath;
}
else if(format.compare(".zip") == 0)//(以通过)
{
systemUnzlib = "unzip ";
unzlibStr = systemUnzlib + srcFilePath + " -d " + destFilePath;
}
else
{
cout << "don't support"
" this compress file." << endl;
}
finish = time(NULL);
cout << "uncompress file use time seconds:" << difftime(finish,start) << endl;
system(unzlibStr.c_str());
}
本文为博主原创文章,未经博主允许请勿转载!作者:ISmileLi