繁体   English   中英

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

[英]Storing the o/p of Runtime.getRuntime().exec(command);

我是Java新手.....我在zip文件夹中有3个文件,我正在使用以下文件解压缩该文件

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

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

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();

我尝试了这段代码,但没有得到想要的结果。

您可以使用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();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM