import 部分:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
代码:
public static void generateZipFileByCall7z() {
String filePathStr = "E:\\Test_JAVAProgram\\test7z\\testFile.txt"; //需要压缩的文件
String zipToolPath = "D:\\7-Zip\\7z.exe"; //7z工具的路径
String cmd = zipToolPath + " " + "a" + " " + "E:\\Test_JAVAProgram\\test7z\\archive.zip" + " " + filePathStr;
System.out.println(cmd);
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
Process process = Runtime.getRuntime().exec(cmd);
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr != null) {
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
cmd输出是:D:\7-Zip\7z.exe a E:\Test_JAVAProgram\test7z\archive.zip E:\Test_JAVAProgram\test7z\testFile.txt
调用7z工具的命令格式,7z包自己也有说明的(7-zip.chm打开这个找到Command Line Version)