简体   繁体   English

从C ++使用tar创建压缩档案

[英]Creating compressed archives with tar from C++

if(cmd == "zip")
        {
            string z = "tar cvf " + destination + "  " + file;
            system(z.c_str());
        }

I wanted to compress a file. 我想压缩一个文件。 to a given destination. 到给定的目的地。 But what is happening in my case is it makes a compressed file of destination. 但是在我的情况下,发生的是将其压缩为目标文件。 Like I am using it in Linux, and in Destination I give "Desktop" and in file I give filename. 就像我在Linux中使用它一样,在Destination(目标)中输入“ Desktop”,在文件中输入文件名。 so it only makes Desktop compress file with no extension on Desktop. 因此它只能使Desktop压缩文件,而在Desktop上没有扩展名。

You can do: 你可以做:

string z = "tar cvzf " + destination + "/" + file + ".tar.gz  " + file;

(the variables, if untrusted, should be sanitized, keeping in mind that system() invokes a POSIX shell) (变量,如果不受信任,应进行清理,记住system()调用POSIX shell)

Without the "z", tar creates a .tar file, but you have to specify the file name and extension yourself—it's not automatic. 没有“ z”,tar将创建一个.tar文件,但是您必须自己指定文件名和扩展名-这不是自动的。

With the "z", it creates a .tar.gz file (which is different from a zip file—a common tar doesn't do zip files). 使用“ z”,它将创建一个.tar.gz文件(与zip文件不同,常见的tarzip文件)。

There are other options. 还有其他选择。 See tar(1) for more details. 有关更多详细信息,请参见tar(1)


BTW, a call to the system function is technically not a system call . 顺便说一句,对system功能的调用从技术上讲不是系统调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM