获取一个命令行字符串:unzip -d ./123 1.zip ,如何在Linux上解析到解压的压缩包路径,用c++实现
时间: 2024-02-20 19:56:33 浏览: 27
可以使用C++中的字符串处理函数来解析命令行字符串,具体步骤如下:
1. 通过命令行参数获取到字符串 "unzip -d ./123 1.zip"。
2. 使用C++中的字符串处理函数,比如 std::string::find() 和 std::string::substr(),来解析出目标压缩包路径。
代码示例:
```c++
#include <iostream>
#include <string>
int main(int argc, char** argv) {
std::string cmd = "unzip -d ./123 1.zip";
std::string unzip = "unzip ";
size_t pos = cmd.find(unzip);
if (pos != std::string::npos) { // 找到了 unzip
std::string zipPath = cmd.substr(pos + unzip.length()); // 获取压缩包路径
std::cout << zipPath << std::endl;
}
return 0;
}
```
在上述示例中,使用 std::string::find() 函数找到了 "unzip " 的位置,然后使用 std::string::substr() 函数获取了压缩包路径。最终输出的结果是 "./123 1.zip",可以根据实际需求再进行解析。
相关问题
解读命令行: cd /opt/weaver cp /Filesystem/updatezip/128.zip ./ unzip -o 128.zip
这是一系列命令行操作的指令。下面是对每个命令的解读:
1. `cd /opt/weaver`:这条命令将当前工作目录更改为`/opt/weaver`,即切换到路径`/opt/weaver`。
2. `cp /Filesystem/updatezip/128.zip ./`:这条命令将位于`/Filesystem/updatezip/`路径下的文件`128.zip`复制到当前工作目录(即`.`代表当前目录)。
3. `unzip -o 128.zip`:这条命令解压缩名为`128.zip`的文件到当前目录。选项`-o`表示覆盖已存在的文件,以确保解压缩过程中不会询问是否覆盖文件。
综上所述,这段命令行的作用是切换到路径`/opt/weaver`,然后将位于`/Filesystem/updatezip/`路径下的文件`128.zip`复制到当前目录,并解压缩该文件。
unzip filename.zip -d ./ -O CP936没反应
如果你使用的是 Windows 操作系统,可以尝试使用以下命令解压缩:
```
powershell -command "Expand-Archive -Path 'filename.zip' -DestinationPath './' -Force"
```
如果你使用的是 Linux 或 macOS 操作系统,可以尝试使用以下命令解压缩:
```
unzip -O CP936 filename.zip -d ./
```
这些命令可以将压缩文件解压缩到当前目录中,并且使用 CP936 编码进行解压缩。如果仍然无法解决问题,请提供更多详细信息,以便我更好地帮助你。