package com.mopon.cec;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import com.mopon.cec.common.util.DateUtil;
import com.mopon.cec.common.util.StringUtils;
public class 拷贝打包的文件到指定目录 {
static String[] zipFilePaths = { "", "", "" };
static String targetPath = "E:\\01_saas_release";
static String saasCommonJarPath = "E:\\01_saas_release\\Thrift_anyIP\\saas_common-0.0.3-SNAPSHOT.jar";
static String DIR_SEPARATOR = System.getProperty("file.separator");
public static void main(String[] args) throws Exception {
//拷贝ZIP文件
copyZips();
//解压文件
unzips();
//修改zip 包名称加上 price
String[] zipFileNames = { "scec-saas-price-client-source.zip", "scec-saas-price-server-source.zip" };
//修改 client 端的ZIP 包名
File sourceCliZipFile = new File(zipFilePaths[0]);
File targetCliZipFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+zipFileNames[0]);
sourceCliZipFile.renameTo(targetCliZipFile);
//修改Server端的ZIP 包名
File sourceSerZipFile = new File(zipFilePaths[1]);
File targetSerZipFile = new File(zipFilePaths[1].substring(0, zipFilePaths[1].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+zipFileNames[1]);
sourceSerZipFile.renameTo(targetSerZipFile);
//修改文件名称 加上price
String[] fileNames = { "scec-saas-client", "scec-saas-server" };
String[] fileTargetNames = { "scec-saas-price-client", "scec-saas-price-server" };
//修改 client 端 解压文件名
File sourceCliFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileNames[0]);
File targetCliFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileTargetNames[0]);
sourceCliFile.renameTo(targetCliFile);
//修改 server 端 解压文件名
File sourceSerFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileNames[1]);
File targetSerFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileTargetNames[1]);
sourceSerFile.renameTo(targetSerFile);
//修改解压文件名称加上 price
// unjars();
//copySaasCommonJars();
// 打开文件夹
java.awt.Desktop.getDesktop().open(new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))));
}
/**
*
* @throws IOException ZipException
*/
// private static void copySaasCommonJars() throws IOException {
// File source = new File(saasCommonJarPath);
//
// // 三个文件夹在同一个目录
// File parentDir = new File(zipFilePaths[0]).getParentFile();
// for (File tmpDir : parentDir.listFiles()) {
// if (tmpDir.isDirectory())
// FileUtils.copyFileToDirectory(source, new File(tmpDir.getAbsoluteFile() + DIR_SEPARATOR + "lib"), true);
// }
// }
/**
* 解压缩zip包
*/
public static void unjars() throws Exception {
System.out.println("JAR文件解压开始");
String[] zipFileNames = { "scec-saas-client.jar", "scec-saas-server.jar" };
modifyMainFest(getPath(zipFilePaths[0], zipFileNames[0]));
modifyMainFest(getPath(zipFilePaths[1], zipFileNames[1]));
System.out.println("所有JAR文件解压完");
}
/**
*
* @param jarPath jar路径
* @throws IOException IOException
* @throws ZipException ZipException
*/
public static void modifyMainFest(final String jarPath) throws IOException, ZipException {
JarFile jarFile = new JarFile(jarPath);
Enumeration enu = jarFile.entries();
while (enu.hasMoreElements()) {
JarEntry entry = (JarEntry) enu.nextElement();
String name = entry.getName();
// System.out.println(name);
if (name.contains("MANIFEST.MF")) {
String content = IOUtils.toString(jarFile.getInputStream(entry));
content = content.replaceAll("\r\n ", "");
content = content.replaceAll(".jar", ".jar ").replaceAll(".jar ", ".jar ");
String[] array = content.split("\r\n");
for (int idx = 0; idx < array.length; idx++) {
String tmp = array[idx];
if (tmp.contains("Class-Path")) {
System.out.println(tmp);
String array2[] = tmp.split(" ");
String array3[] = Arrays.copyOfRange(array2, 0, array2.length);
for (int tdx = 0; tdx < array2.length; tdx++) {
String tmp2 = array2[tdx];
if (tmp2.contains("saas")) {
tmp2 = tmp2.replaceAll("saas-platform-sdk.*.jar", "saas-platform-sdk-1.0.0-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_common.*.jar", "saas_common-0.0.4-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_db.*.jar", "saas_db-0.0.4-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_client.*.jar", "saas_client-0.0.4-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_server.*.jar", "saas_server-0.0.4-SNAPSHOT.jar");
array3[tdx] = tmp2;
System.out.println(tmp2);
}
}
String classPaths = Arrays.toString(array3).replace("[", "").replace("]", "").replaceAll(",", "");
content = content.replace(tmp, classPaths);
System.out.println(content);
FileUtils.write(new File("MANIFEST.MF"), content);
break;
}
}
}
}
jarFile.close();
ZipFile zipFile = new ZipFile(jarPath);
ZipParameters parameters = new ZipParameters();
parameters.setRootFolderInZip("META-INF/");
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zipFile.addFile(new File("MANIFEST.MF"), parameters);
}
private static String getPath(String allpath, String filePath) {
return allpath.substring(0, allpath.lastIndexOf('-')) + DIR_SEPARATOR + filePath;
}
/**
* 解压缩zip包
*/
public static void unzips() throws Exception {
System.out.println("文件解压开始");
unzip(zipFilePaths[0], false);
unzip(zipFilePaths[1], false);
System.out.println("所有文件解压完");
}
/**
* 解压缩zip包
* @param zipFilePath zip文件的全路径
* @param unzipFilePath 解压后的文件保存的路径
* @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含
*/
public static void unzip(String zipFilePath, boolean includeZipFileName) throws Exception {
if (StringUtils.isEmpty(zipFilePath)) {
throw new RuntimeException("文件是空的");
}
try {
File file = new File(zipFilePath);
file.setReadable(true);
String unzipFilePath = file.getParent();
if (includeZipFileName) {
unzipFilePath = zipFilePath.substring(0, zipFilePath.lastIndexOf('.'));
}
ZipFile zipFile = new ZipFile(zipFilePath);
System.out.println("被解压路径:" + zipFilePath);
System.out.println("解压路径:" + unzipFilePath);
zipFile.extractAll(unzipFilePath);
} catch (ZipException e) {
e.printStackTrace();
throw new RuntimeException("解压文件失败: " + zipFilePath);
}
}
public static void copyZips() throws IOException {
String saasSerPath = System.getProperty("user.dir");
String saasCliPath = saasSerPath.substring(0, saasSerPath.lastIndexOf(DIR_SEPARATOR) + 1) + "scec-saas-client";
String saasPlaPath = saasSerPath.substring(0, saasSerPath.lastIndexOf(DIR_SEPARATOR) + 1) + "scec-saas-platform";
String[] zipFileNames = { "scec-saas-client-source.zip", "scec-saas-server-source.zip" };
String targetDirPath = createDir(targetPath);
// E:\01_saas_release\2016_09_22
zipFilePaths[0] = copyZip(saasCliPath, targetDirPath, zipFileNames[0]);
zipFilePaths[1] = copyZip(saasSerPath, targetDirPath, zipFileNames[1]);
}
public static String createDir(String targetRootPath) throws IOException {
File[] files = new File(targetRootPath).listFiles();
if (files != null) {
}
String targetDirName = DateUtil.getToday().replaceAll("-", "_") + "_"
+ Math.round(Math.random() * 1000);
String targetDirPath = targetRootPath + DIR_SEPARATOR + targetDirName;
File file = new File(targetDirPath);
if (file.exists()) {
deleteFiles(file);
}
file.mkdirs();
return targetDirPath;
}
public static String copyZip(String jarProjectPath, String targetDirPath, String zipName) throws IOException {
String jarPath = jarProjectPath + DIR_SEPARATOR + "target" + DIR_SEPARATOR + zipName;
File source = new File(jarPath);
if (!source.exists())
throw new RuntimeException("文件不存在: " + jarPath);
File targetDir = new File(targetDirPath);
if (!targetDir.exists())
throw new RuntimeException("目标目录不存在: " + targetDirPath);
FileUtils.copyFileToDirectory(source, targetDir);
return targetDirPath + DIR_SEPARATOR + zipName;
}
public static boolean deleteFiles(File file) {
// System.out.println("删除目录:" + file.getAbsolutePath());
// return FileUtils.deleteQuietly(file);
if (file.isDirectory()) {
for (File tmp : file.listFiles()) {
deleteFile(tmp);
}
return deleteFile(file);
} else {
return deleteFile(file);
}
}
public static boolean deleteFile(File file) {
System.out.println("删除目录:" + file.getAbsolutePath());
file.setExecutable(true);
file.setWritable(true);
FileUtils.deleteQuietly(file);
return file.delete();
}
}
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import com.mopon.cec.common.util.DateUtil;
import com.mopon.cec.common.util.StringUtils;
public class 拷贝打包的文件到指定目录 {
static String[] zipFilePaths = { "", "", "" };
static String targetPath = "E:\\01_saas_release";
static String saasCommonJarPath = "E:\\01_saas_release\\Thrift_anyIP\\saas_common-0.0.3-SNAPSHOT.jar";
static String DIR_SEPARATOR = System.getProperty("file.separator");
public static void main(String[] args) throws Exception {
//拷贝ZIP文件
copyZips();
//解压文件
unzips();
//修改zip 包名称加上 price
String[] zipFileNames = { "scec-saas-price-client-source.zip", "scec-saas-price-server-source.zip" };
//修改 client 端的ZIP 包名
File sourceCliZipFile = new File(zipFilePaths[0]);
File targetCliZipFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+zipFileNames[0]);
sourceCliZipFile.renameTo(targetCliZipFile);
//修改Server端的ZIP 包名
File sourceSerZipFile = new File(zipFilePaths[1]);
File targetSerZipFile = new File(zipFilePaths[1].substring(0, zipFilePaths[1].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+zipFileNames[1]);
sourceSerZipFile.renameTo(targetSerZipFile);
//修改文件名称 加上price
String[] fileNames = { "scec-saas-client", "scec-saas-server" };
String[] fileTargetNames = { "scec-saas-price-client", "scec-saas-price-server" };
//修改 client 端 解压文件名
File sourceCliFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileNames[0]);
File targetCliFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileTargetNames[0]);
sourceCliFile.renameTo(targetCliFile);
//修改 server 端 解压文件名
File sourceSerFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileNames[1]);
File targetSerFile = new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))+DIR_SEPARATOR+fileTargetNames[1]);
sourceSerFile.renameTo(targetSerFile);
//修改解压文件名称加上 price
// unjars();
//copySaasCommonJars();
// 打开文件夹
java.awt.Desktop.getDesktop().open(new File(zipFilePaths[0].substring(0, zipFilePaths[0].lastIndexOf(DIR_SEPARATOR))));
}
/**
*
* @throws IOException ZipException
*/
// private static void copySaasCommonJars() throws IOException {
// File source = new File(saasCommonJarPath);
//
// // 三个文件夹在同一个目录
// File parentDir = new File(zipFilePaths[0]).getParentFile();
// for (File tmpDir : parentDir.listFiles()) {
// if (tmpDir.isDirectory())
// FileUtils.copyFileToDirectory(source, new File(tmpDir.getAbsoluteFile() + DIR_SEPARATOR + "lib"), true);
// }
// }
/**
* 解压缩zip包
*/
public static void unjars() throws Exception {
System.out.println("JAR文件解压开始");
String[] zipFileNames = { "scec-saas-client.jar", "scec-saas-server.jar" };
modifyMainFest(getPath(zipFilePaths[0], zipFileNames[0]));
modifyMainFest(getPath(zipFilePaths[1], zipFileNames[1]));
System.out.println("所有JAR文件解压完");
}
/**
*
* @param jarPath jar路径
* @throws IOException IOException
* @throws ZipException ZipException
*/
public static void modifyMainFest(final String jarPath) throws IOException, ZipException {
JarFile jarFile = new JarFile(jarPath);
Enumeration enu = jarFile.entries();
while (enu.hasMoreElements()) {
JarEntry entry = (JarEntry) enu.nextElement();
String name = entry.getName();
// System.out.println(name);
if (name.contains("MANIFEST.MF")) {
String content = IOUtils.toString(jarFile.getInputStream(entry));
content = content.replaceAll("\r\n ", "");
content = content.replaceAll(".jar", ".jar ").replaceAll(".jar ", ".jar ");
String[] array = content.split("\r\n");
for (int idx = 0; idx < array.length; idx++) {
String tmp = array[idx];
if (tmp.contains("Class-Path")) {
System.out.println(tmp);
String array2[] = tmp.split(" ");
String array3[] = Arrays.copyOfRange(array2, 0, array2.length);
for (int tdx = 0; tdx < array2.length; tdx++) {
String tmp2 = array2[tdx];
if (tmp2.contains("saas")) {
tmp2 = tmp2.replaceAll("saas-platform-sdk.*.jar", "saas-platform-sdk-1.0.0-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_common.*.jar", "saas_common-0.0.4-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_db.*.jar", "saas_db-0.0.4-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_client.*.jar", "saas_client-0.0.4-SNAPSHOT.jar");
tmp2 = tmp2.replaceAll("saas_server.*.jar", "saas_server-0.0.4-SNAPSHOT.jar");
array3[tdx] = tmp2;
System.out.println(tmp2);
}
}
String classPaths = Arrays.toString(array3).replace("[", "").replace("]", "").replaceAll(",", "");
content = content.replace(tmp, classPaths);
System.out.println(content);
FileUtils.write(new File("MANIFEST.MF"), content);
break;
}
}
}
}
jarFile.close();
ZipFile zipFile = new ZipFile(jarPath);
ZipParameters parameters = new ZipParameters();
parameters.setRootFolderInZip("META-INF/");
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
zipFile.addFile(new File("MANIFEST.MF"), parameters);
}
private static String getPath(String allpath, String filePath) {
return allpath.substring(0, allpath.lastIndexOf('-')) + DIR_SEPARATOR + filePath;
}
/**
* 解压缩zip包
*/
public static void unzips() throws Exception {
System.out.println("文件解压开始");
unzip(zipFilePaths[0], false);
unzip(zipFilePaths[1], false);
System.out.println("所有文件解压完");
}
/**
* 解压缩zip包
* @param zipFilePath zip文件的全路径
* @param unzipFilePath 解压后的文件保存的路径
* @param includeZipFileName 解压后的文件保存的路径是否包含压缩文件的文件名。true-包含;false-不包含
*/
public static void unzip(String zipFilePath, boolean includeZipFileName) throws Exception {
if (StringUtils.isEmpty(zipFilePath)) {
throw new RuntimeException("文件是空的");
}
try {
File file = new File(zipFilePath);
file.setReadable(true);
String unzipFilePath = file.getParent();
if (includeZipFileName) {
unzipFilePath = zipFilePath.substring(0, zipFilePath.lastIndexOf('.'));
}
ZipFile zipFile = new ZipFile(zipFilePath);
System.out.println("被解压路径:" + zipFilePath);
System.out.println("解压路径:" + unzipFilePath);
zipFile.extractAll(unzipFilePath);
} catch (ZipException e) {
e.printStackTrace();
throw new RuntimeException("解压文件失败: " + zipFilePath);
}
}
public static void copyZips() throws IOException {
String saasSerPath = System.getProperty("user.dir");
String saasCliPath = saasSerPath.substring(0, saasSerPath.lastIndexOf(DIR_SEPARATOR) + 1) + "scec-saas-client";
String saasPlaPath = saasSerPath.substring(0, saasSerPath.lastIndexOf(DIR_SEPARATOR) + 1) + "scec-saas-platform";
String[] zipFileNames = { "scec-saas-client-source.zip", "scec-saas-server-source.zip" };
String targetDirPath = createDir(targetPath);
// E:\01_saas_release\2016_09_22
zipFilePaths[0] = copyZip(saasCliPath, targetDirPath, zipFileNames[0]);
zipFilePaths[1] = copyZip(saasSerPath, targetDirPath, zipFileNames[1]);
}
public static String createDir(String targetRootPath) throws IOException {
File[] files = new File(targetRootPath).listFiles();
if (files != null) {
}
String targetDirName = DateUtil.getToday().replaceAll("-", "_") + "_"
+ Math.round(Math.random() * 1000);
String targetDirPath = targetRootPath + DIR_SEPARATOR + targetDirName;
File file = new File(targetDirPath);
if (file.exists()) {
deleteFiles(file);
}
file.mkdirs();
return targetDirPath;
}
public static String copyZip(String jarProjectPath, String targetDirPath, String zipName) throws IOException {
String jarPath = jarProjectPath + DIR_SEPARATOR + "target" + DIR_SEPARATOR + zipName;
File source = new File(jarPath);
if (!source.exists())
throw new RuntimeException("文件不存在: " + jarPath);
File targetDir = new File(targetDirPath);
if (!targetDir.exists())
throw new RuntimeException("目标目录不存在: " + targetDirPath);
FileUtils.copyFileToDirectory(source, targetDir);
return targetDirPath + DIR_SEPARATOR + zipName;
}
public static boolean deleteFiles(File file) {
// System.out.println("删除目录:" + file.getAbsolutePath());
// return FileUtils.deleteQuietly(file);
if (file.isDirectory()) {
for (File tmp : file.listFiles()) {
deleteFile(tmp);
}
return deleteFile(file);
} else {
return deleteFile(file);
}
}
public static boolean deleteFile(File file) {
System.out.println("删除目录:" + file.getAbsolutePath());
file.setExecutable(true);
file.setWritable(true);
FileUtils.deleteQuietly(file);
return file.delete();
}
}