Because we cannot directly replace the files in a certain folder in the compressed package, we need to extract them first.
Import part:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Code:
public static void extractFileOnArchiveByCall7z() { String filePathStr = "testUpdateZipFile"; // The extracted file name String zipToolPath = "D:\\7-Zip\\7z.exe"; // 7z tool path String sourceFilePath = "E:\ \Test_JAVAProgram\\test7z\\testUpdateZipFile.zip"; // Compressed package, there are many types of compressed package supported, I have only tried zip at present, ear String outputFilePath = "E:\\Test_JAVAProgram\\test7z"; // Extract the path where the file is placed String cmd = zipToolPath + "" + "x" + "" + sourceFilePath + "" + "-o" + outputFilePath + "" + filePathStr + "" + "-y"; 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();
}
}
}
}