我编写了一段简单的代码,用于使用 unzip 提取 zip 文件。当未设置输出目录但返回错误是设置了目录时,它工作正常
“存档:/home/vishvesh.kumar/tempFolder/test.zip checkdir:无法创建提取目录:/home/vishvesh.kumar/tempFolder 没有这样的文件或目录”
代码:
int main(int argc, char *argv1[])
{
std::cout << "Creating child process...";
std::vector<std::string> arguements;
char* argv[4] = {0};
argv [0] = "/usr/bin/unzip";
argv [2] = "/home/vishvesh.kumar/tempFolder/test.zip";
argv [1] = "-d /home/vishvesh.kumar/tempFolder/";
argv [3] = NULL;
createChildProcess(argv);
}
void createChildProcess(char* argv[])
{
pid_t pid = fork();
if (pid == -1)
{
std::cout << "error creating child process, exiting...";
exit(1);
}
else if (pid == 0)
{ // This is the child process
std::cout << "This is the child process. About to sleep\n";
std::cout << "Woke up\n";
if (execvp(argv[0], argv))
{ // execvp failed
std::cout << "fatal - execvp failed!";
exit(1);
}
}
else
{ // This is the parent process.
std::cout << "This is the parent process\n";
int status;
std::cout << " done. PID: " << pid << ".\n";
double start = 0;
waitpid(pid, &status, 0); // Wait for program to finish executing.
double dur = (clock() - start) / CLOCKS_PER_SEC; // Get execution time
std::cout << "Sad my son died\n";
std::cout << "Program returned " << WEXITSTATUS(status);
std::cout << " and lasted " << dur << " seconds.\nPress enter.\n";
std::cin.get();
}
}