存储Runtime.getRuntime()。exec(command)的o / p;

时间:2013-03-18 17:27:48

标签: java runtime.exec

我是java新手.....我在zip文件夹中有3个文件,我使用

提取它
String command = "cmd /c start cmd.exe /K \"jar -xvf record.zip\"";
     Process ps= Runtime.getRuntime().exec(command);

我需要在提取后将record.zip中存在的所有三个文件存储到String中 这些文件使用jar -xvf

BufferedReader br=new BufferedReader(new InputStreamReader(ps.getInputStream())); 
     String temp = br.readLine();
     while( temp != null ) { //!temp.equals(null)) {
         output.write(temp);
         temp = br.readLine();
         }
         output.close();

我尝试了这段代码,但它没有取得我想要的结果.... 提前谢谢....

1 个答案:

答案 0 :(得分:0)

您可以使用JDK中已有的功能来读取zip文件,例如,这会将zip中所有文件的内容逐行打印到控制台:

public static void main(String[] args) throws ZipException, IOException {
    final File file = new File("myZipFile.zip");
    final ZipFile zipFile = new ZipFile(file);
    final Enumeration<? extends ZipEntry> entries = zipFile.entries();
    while (entries.hasMoreElements()) {
        final ZipEntry entry = entries.nextElement();
        System.out.println(entry);
        if (!entry.isDirectory()) {
            final BufferedReader reader = new BufferedReader(new InputStreamReader(zipFile.getInputStream(entry)));
            for (String line = reader.readLine(); line != null; line = reader.readLine()) {
                System.out.println(line);
            }
        }
    }
    zipFile.close();
}