使用7zip解压缩特定文件夹的命令

我正在使用Windows来更具体,我正在从java程序使用过程和getRuntime().exec()调用cmd命令。 我尝试了像-r选项,但它不工作。 我尝试了代码行

 Process proc = prog.exec(System.getenv("ProgramFiles").concat("\\7-Zip\\7z x " + "\""+inputZIPFile+"\""+ " -o"+outputFolder+"SpecificFolder\\* -r")); 

提前致谢

Solutions Collecting From Web of "使用7zip解压缩特定文件夹的命令"

改用ProcessBuilder来代替。 它更好地处理空格参数,并允许你做重定向输出流,并指定命令的起始目录…

 public static void main(String[] args) { ProcessBuilder pb = new ProcessBuilder( System.getenv("ProgramFiles") + "/7-Zip/7z.exe", "x", inputZIPFile, "-o" + outputFolder+"/SpecificFolder", "-r" ); pb.redirectError(); try { Process p = pb.start(); new Thread(new InputConsumer(p.getInputStream())).start(); System.out.println("Exited with: " + p.waitFor()); } catch (Exception ex) { ex.printStackTrace(); } } public static class InputConsumer implements Runnable { private InputStream is; public InputConsumer(InputStream is) { this.is = is; } @Override public void run() { try { int value = -1; while ((value = is.read()) != -1) { System.out.print((char) value); } } catch (IOException exp) { exp.printStackTrace(); } System.out.println(""); } } 

您可能还想考虑Apache Commons Compress ,它提供对7zip的读取支持

为什么不使用java解压缩? 使用Java API压缩和解压缩数据 :

 import java.io.*; import java.util.zip.*; public class UnZip { final int BUFFER = 2048; public static void main (String argv[]) { try { BufferedOutputStream dest = null; FileInputStream fis = new FileInputStream(argv[0]); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); ZipEntry entry; while((entry = zis.getNextEntry()) != null) { System.out.println("Extracting: " +entry); int count; byte data[] = new byte[BUFFER]; // write the files to the disk FileOutputStream fos = new FileOutputStream(entry.getName()); dest = new BufferedOutputStream(fos, BUFFER); while ((count = zis.read(data, 0, BUFFER)) != -1) { dest.write(data, 0, count); } dest.flush(); dest.close(); } zis.close(); } catch(Exception e) { e.printStackTrace(); } } }