我正在尝试使用7zip扩展zip文件,但我一直在获取7zip Usage打印输出.
zip存在于c:\ temp中
同一命令在批处理窗口中成功:
C:\TEMP>7z x "tryThis.zip"
Run Code Online (Sandbox Code Playgroud)
我尝试将workdir路径添加到文件中,并且没有工作目录,没有任何帮助. - 我可以使用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();
Run Code Online (Sandbox Code Playgroud)
请查看Runtime.exec的文档
你实际上要做的是调用没有参数的7-zip并将参数作为你的环境提供.环境就像Windows PATH等.
所以你可能想做类似的事情:
Runtime.getRuntime().exec(new String[]{pathToZipExe, "x", fileName}, null, new File(workingDir));
Run Code Online (Sandbox Code Playgroud)
另一方面,我强烈建议你看一下java中包含的ZipInputStream - 使用它你也可以解压缩zip文件.
干杯