When we want to update the files (including folders) in the compressed package (zip, ear should also include many), we can do it by calling the 7z tool.
Note: You cannot update the files in a folder in the compressed package . You need to use other methods, such as: first extract this folder from the compressed package, then modify the files in the extracted folder, and finally use this update method to replace the old one inside
Import part:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Code:
public static void updateZipFileByCall7z() { String filePathStr = "E:\\Test_JAVAProgram\\test7z\\testUpdateZipFile"; // updated folder String updateFilePath = "E:\\Test_JAVAProgram\\test7z\\testFile.txt"; / / Updated file String zipToolPath = "D:\\7-Zip\\7z.exe"; // 7z tool path String sourceFilePath = "E:\\Test_JAVAProgram\\test7z\\testUpdateZipFile.zip"; // updated Zip file // String cmd = zipToolPath + "" + "u" + "" + sourceFilePath + "" + filePathStr; String cmd = zipToolPath + "" + "u" + "" + sourceFilePath + "" + updateFilePath; 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);
System.out.println(line.toLowerCase().indexOf("Everything is Ok".toLowerCase()));
if(line.toLowerCase().indexOf("Everything is Ok".toLowerCase()) != -1) {
System.out.println("成功");
}
}
} 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();
}
}
}
}