我正在尝试使用 7zip 扩展 zip 文件,但我不断收到 7zip 使用打印输出。
该 zip 存在于 c:\temp
相同的命令在批处理窗口中成功:
C:\TEMP>7z x "tryThis.zip"
我尝试将工作目录路径添加到文件中,并且也没有工作目录, 没有任何帮助。 - 我可能可以使用 CMD/c 命令运行它,但我更喜欢保持代码干净
我做错了什么?
谢谢!
String pathTo7ZipExe = "c:\\program files\\7-zip\\7z.exe";
String fileName ="tryThis.zip";
String workingDir = "c:\\temp\\";
Process process = Runtime.getRuntime().exec(
new String[]{pathTo7ZipExe},
new String[]{" x \"" + fileName +"\""},
new File(workingDir));
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
System.out.println(line);
}
// wait for zip to end.
int exitVal = process.waitFor();
最佳答案
请查看 Runtime.exec 的文档
您实际上想做的是不带参数调用 7-zip 并提供参数作为您的环境。环境是 Windows PATH 等。
所以你可能想做这样的事情:
Runtime.getRuntime().exec(new String[]{pathToZipExe, "x", fileName}, null, new File(workingDir));
另一方面,我强烈建议您查看 ZipInputStream它包含在 java 中 - 使用它您还可以解压 zip 文件。
干杯
关于java - 使用 7zip 扩展文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904026/