Commit aabd384a by songxiang

配置中心切换到GIT

parent 773e7cdc
...@@ -5,7 +5,7 @@ spring: ...@@ -5,7 +5,7 @@ spring:
enabled: true enabled: true
service-id: cloud-config service-id: cloud-config
profile: default profile: default
label: prod label: master
loadbalancer: loadbalancer:
retry: retry:
enabled: true enabled: true
......
...@@ -5,7 +5,7 @@ spring: ...@@ -5,7 +5,7 @@ spring:
enabled: true enabled: true
service-id: cloud-config service-id: cloud-config
profile: default profile: default
label: uat label: master
loadbalancer: loadbalancer:
retry: retry:
enabled: true enabled: true
......
This source diff could not be displayed because it is too large. You can view the blob instead.
package com.pcloud.common.utils.zip; package com.pcloud.common.utils.zip;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.tools.ant.Project; import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand; import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream; import org.apache.tools.zip.ZipOutputStream;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.github.junrar.Archive; import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader; import com.github.junrar.rarfile.FileHeader;
import com.pcloud.common.constant.OSConstant; import com.pcloud.common.constant.OSConstant;
import com.pcloud.common.entity.UploadResultInfo; import com.pcloud.common.entity.UploadResultInfo;
import com.pcloud.common.exceptions.BizException; import com.pcloud.common.exceptions.BizException;
import com.pcloud.common.exceptions.FileException; import com.pcloud.common.exceptions.FileException;
import com.pcloud.common.utils.FileUtils; import com.pcloud.common.utils.FileUtils;
import com.pcloud.common.utils.ListUtils; import com.pcloud.common.utils.ListUtils;
import com.pcloud.common.utils.UUIDUitl; import com.pcloud.common.utils.UUIDUitl;
import com.pcloud.common.utils.aliyun.OssUtils; import com.pcloud.common.utils.aliyun.OssUtils;
import com.pcloud.common.utils.string.StringUtil; import com.pcloud.common.utils.string.StringUtil;
/** /**
* @描述:文件压缩和解压缩 * @描述:文件压缩和解压缩
* @作者:songx * @作者:songx
* @创建时间:2016年7月13日,上午11:36:37 @版本:1.0 * @创建时间:2016年7月13日,上午11:36:37 @版本:1.0
*/ */
public class CompressUtils { public class CompressUtils {
/** /**
* *
*/ */
private static final Logger LOGGER = LoggerFactory.getLogger(CompressUtils.class); private static final Logger LOGGER = LoggerFactory.getLogger(CompressUtils.class);
/** /**
* 压缩文件临时基础路径 * 压缩文件临时基础路径
*/ */
private static String FILE_LOCAL_PATH = OSConstant.USERDIR + "/download/"; private static String FILE_LOCAL_PATH = OSConstant.USERDIR + "/download/";
/** /**
* 压缩文件临时基础路径 * 压缩文件临时基础路径
*/ */
private static String ZIP_FILE_PATH = OSConstant.USERDIR + "/zip/"; private static String ZIP_FILE_PATH = OSConstant.USERDIR + "/zip/";
/** /**
* 压缩文件 * 压缩文件
* *
* @param fileUrlList * @param fileUrlList
* String[0]=文件名称, String[1]=文件地址 * String[0]=文件名称, String[1]=文件地址
* @param zipName * @param zipName
* @return * @return
* @throws BizException * @throws BizException
*/ */
public static UploadResultInfo zip(List<String[]> fileUrlList, String zipName) throws BizException { public static UploadResultInfo zip(List<String[]> fileUrlList, String zipName) throws BizException {
LOGGER.info("【压缩】压缩文件.<START>"); LOGGER.info("【压缩】压缩文件.<START>");
if (ListUtils.isEmpty(fileUrlList)) { if (ListUtils.isEmpty(fileUrlList)) {
return null; return null;
} }
String tempZipName = FileUtils.formatName(zipName) + "_" + UUIDUitl.generateString(12); zipName = FileUtils.formatName(zipName);
// 检查临时文件夹是否存在,不存在就创建 String tempZipName = zipName + "_" + UUIDUitl.generateString(12);
String fileFolderPath = FILE_LOCAL_PATH + tempZipName; // 检查临时文件夹是否存在,不存在就创建
FileUtils.isDir(fileFolderPath); String fileFolderPath = FILE_LOCAL_PATH + tempZipName;
// 下载文件到文件夹中 FileUtils.isDir(fileFolderPath);
int idx = 1; // 下载文件到文件夹中
for (String[] files : fileUrlList) { int idx = 1;
String fileType = FileUtils.getFileType(files[1]); for (String[] files : fileUrlList) {
String fileName = FileUtils.formatName(files[0]); String fileType = FileUtils.getFileType(files[1]);
String downloadLocalPath = fileFolderPath + "/" + fileName + "_" + idx + "." + fileType; String fileName = FileUtils.formatName(files[0]);
FileUtils.downloadFileFromUrl(files[1], downloadLocalPath); String downloadLocalPath = fileFolderPath + "/" + fileName + "_" + idx + "." + fileType;
idx++; FileUtils.downloadFileFromUrl(files[1], downloadLocalPath);
} idx++;
// 检查压缩包临时文件夹是否存在,不存在就创建 }
FileUtils.isDir(ZIP_FILE_PATH); // 检查压缩包临时文件夹是否存在,不存在就创建
String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip"; FileUtils.isDir(ZIP_FILE_PATH);
try { String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip";
CompressUtils.zip(fileFolderPath, zipFilePath); try {
} catch (Exception e) { CompressUtils.zip(fileFolderPath, zipFilePath);
LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e); } catch (Exception e) {
throw new FileException(FileException.ZIP_ERROR, "压缩失败!"); LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e);
} throw new FileException(FileException.ZIP_ERROR, "压缩失败!");
// 上传文件到服务器中 }
UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName); // 上传文件到服务器中
// 删除产生的文件 UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName);
FileUtils.deleteDirectory(fileFolderPath); // 删除产生的文件
FileUtils.deleteFile(zipFilePath); FileUtils.deleteDirectory(fileFolderPath);
LOGGER.info("【压缩】压缩文件.<END>"); FileUtils.deleteFile(zipFilePath);
return uploadResultInfo; LOGGER.info("【压缩】压缩文件.<END>");
} return uploadResultInfo;
}
/** /**
* 压缩文件(带目录) * 压缩文件(带目录)
* *
* @param catalogFiles key : 目录名 * @param catalogFiles
* String[0]=文件名称, String[1]=文件地址 * key : 目录名 String[0]=文件名称, String[1]=文件地址
* @param zipName * @param zipName
* @return * @return
* @throws BizException * @throws BizException
*/ */
public static UploadResultInfo zipByCatalog(Map<String, List<String[]>> catalogFiles, String zipName) throws BizException { public static UploadResultInfo zipByCatalog(Map<String, List<String[]>> catalogFiles, String zipName)
LOGGER.info("【压缩】压缩文件.<START>"); throws BizException {
if (MapUtils.isEmpty(catalogFiles)) { LOGGER.info("【压缩】压缩文件.<START>");
return null; if (MapUtils.isEmpty(catalogFiles)) {
} return null;
String tempZipName = FileUtils.formatName(zipName) + "_" + UUIDUitl.generateString(12); }
String parentPath = FILE_LOCAL_PATH + tempZipName; zipName = FileUtils.formatName(zipName);
FileUtils.isDir(parentPath); String tempZipName = zipName + "_" + UUIDUitl.generateString(12);
for(String catalog : catalogFiles.keySet()) { String parentPath = FILE_LOCAL_PATH + tempZipName;
String downloadPath; FileUtils.isDir(parentPath);
// 检查临时文件夹是否存在,不存在就创建 for (String catalog : catalogFiles.keySet()) {
if(!StringUtil.isEmpty(catalog)) { String downloadPath;
String catalogFolderPath = parentPath +"/"+ catalog; // 检查临时文件夹是否存在,不存在就创建
FileUtils.isDir(catalogFolderPath); if (!StringUtil.isEmpty(catalog)) {
downloadPath = catalogFolderPath; String catalogFolderPath = parentPath + "/" + catalog;
}else { FileUtils.isDir(catalogFolderPath);
downloadPath = parentPath; downloadPath = catalogFolderPath;
} } else {
List<String[]> fileUrlList = catalogFiles.get(catalog); downloadPath = parentPath;
// 下载文件到文件夹中 }
int idx = 1; List<String[]> fileUrlList = catalogFiles.get(catalog);
for (String[] files : fileUrlList) { // 下载文件到文件夹中
String fileType = FileUtils.getFileType(files[1]); int idx = 1;
String fileName = FileUtils.formatName(files[0]); for (String[] files : fileUrlList) {
String downloadLocalPath = downloadPath + "/" + fileName + "_" + idx + "." String fileType = FileUtils.getFileType(files[1]);
+ fileType; String fileName = FileUtils.formatName(files[0]);
FileUtils.downloadFileFromUrl(files[1], downloadLocalPath); String downloadLocalPath = downloadPath + "/" + fileName + "_" + idx + "." + fileType;
idx++; FileUtils.downloadFileFromUrl(files[1], downloadLocalPath);
} idx++;
} }
// 检查压缩包临时文件夹是否存在,不存在就创建 }
FileUtils.isDir(ZIP_FILE_PATH); // 检查压缩包临时文件夹是否存在,不存在就创建
String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip"; FileUtils.isDir(ZIP_FILE_PATH);
try { String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip";
CompressUtils.zip(parentPath, zipFilePath); try {
} catch (Exception e) { CompressUtils.zip(parentPath, zipFilePath);
LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e); } catch (Exception e) {
throw new FileException(FileException.ZIP_ERROR, "压缩失败!"); LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e);
} throw new FileException(FileException.ZIP_ERROR, "压缩失败!");
// 上传文件到服务器中 }
UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName); // 上传文件到服务器中
// 删除产生的文件 UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName);
FileUtils.deleteDirectory(parentPath); // 删除产生的文件
FileUtils.deleteFile(zipFilePath); FileUtils.deleteDirectory(parentPath);
LOGGER.info("【压缩】压缩文件.<END>"); FileUtils.deleteFile(zipFilePath);
return uploadResultInfo; LOGGER.info("【压缩】压缩文件.<END>");
} return uploadResultInfo;
}
/** /**
* @Desc ZIP文件解压 * @Desc ZIP文件解压
* @param zipPath * @param zipPath
* ZIP文件目录 * ZIP文件目录
* @param destDir * @param destDir
* 解压目录 * 解压目录
* @throws Exception * @throws Exception
*/ */
private static void unzip(String zipPath, String destDir) throws Exception { private static void unzip(String zipPath, String destDir) throws Exception {
try { try {
Project p = new Project(); Project p = new Project();
Expand e = new Expand(); Expand e = new Expand();
e.setProject(p); e.setProject(p);
e.setSrc(new File(zipPath)); e.setSrc(new File(zipPath));
e.setOverwrite(false); e.setOverwrite(false);
e.setDest(new File(destDir)); e.setDest(new File(destDir));
e.setEncoding("gbk"); e.setEncoding("gbk");
e.execute(); e.execute();
} catch (Exception e) { } catch (Exception e) {
throw e; throw e;
} }
} }
/** /**
* @Desc rar文件解压 * @Desc rar文件解压
* @param rarPath * @param rarPath
* rar路径 * rar路径
* @param destDir * @param destDir
* 解压到的文件夹 * 解压到的文件夹
* @throws Exception * @throws Exception
*/ */
public static void unrar(String rarPath, String destDir) throws Exception { public static void unrar(String rarPath, String destDir) throws Exception {
if (!rarPath.toLowerCase().endsWith(".rar")) { if (!rarPath.toLowerCase().endsWith(".rar")) {
throw new Exception("非rar文件!"); throw new Exception("非rar文件!");
} }
File dstDiretory = new File(destDir); File dstDiretory = new File(destDir);
if (!dstDiretory.exists()) { if (!dstDiretory.exists()) {
dstDiretory.mkdirs(); dstDiretory.mkdirs();
} }
Archive a = new Archive(new File(rarPath)); Archive a = new Archive(new File(rarPath));
if (a != null) { if (a != null) {
FileHeader fh = a.nextFileHeader(); FileHeader fh = a.nextFileHeader();
while (fh != null) { while (fh != null) {
String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW(); String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW();
if (fh.isDirectory()) { if (fh.isDirectory()) {
File fol = new File(destDir + File.separator + fileName); File fol = new File(destDir + File.separator + fileName);
fol.mkdirs(); fol.mkdirs();
} else { } else {
File out = new File(destDir + File.separator + fileName.trim()); File out = new File(destDir + File.separator + fileName.trim());
if (!out.exists()) { if (!out.exists()) {
if (!out.getParentFile().exists()) { if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs(); out.getParentFile().mkdirs();
} }
out.createNewFile(); out.createNewFile();
} }
FileOutputStream os = new FileOutputStream(out); FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os); a.extractFile(fh, os);
os.close(); os.close();
} }
fh = a.nextFileHeader(); fh = a.nextFileHeader();
} }
a.close(); a.close();
} }
} }
/** /**
* @Desc 解压缩文件 * @Desc 解压缩文件
* @param zip或者rar文件路径 * @param zip或者rar文件路径
* @param 解压目录 * @param 解压目录
* @throws Exception * @throws Exception
*/ */
public static void deCompress(String sourceFile, String destDir) throws Exception { public static void deCompress(String sourceFile, String destDir) throws Exception {
// 保证文件夹路径最后是"/"或者"\" // 保证文件夹路径最后是"/"或者"\"
char lastChar = destDir.charAt(destDir.length() - 1); char lastChar = destDir.charAt(destDir.length() - 1);
if (lastChar != '/' && lastChar != '\\') { if (lastChar != '/' && lastChar != '\\') {
destDir += File.separator; destDir += File.separator;
} }
// 根据类型,进行相应的解压缩 // 根据类型,进行相应的解压缩
String type = sourceFile.substring(sourceFile.lastIndexOf(".") + 1).toLowerCase(); String type = sourceFile.substring(sourceFile.lastIndexOf(".") + 1).toLowerCase();
if (type.equals("zip")) { if (type.equals("zip")) {
unzip(sourceFile, destDir); unzip(sourceFile, destDir);
} else if (type.equals("rar")) { } else if (type.equals("rar")) {
unrar(sourceFile, destDir); unrar(sourceFile, destDir);
} else { } else {
throw new Exception("只支持zip和rar格式的压缩包!"); throw new Exception("只支持zip和rar格式的压缩包!");
} }
} }
/** /**
* @Desc 压缩zip文件 @param inputFilename 待压缩的文件名称或文件夹路径名称 @param zipFilename * @Desc 压缩zip文件 @param inputFilename 待压缩的文件名称或文件夹路径名称 @param zipFilename
* 压缩后的文件完整的路径名称 @throws * 压缩后的文件完整的路径名称 @throws
*/ */
public static void zip(String inputFilename, String zipFilename) throws IOException { public static void zip(String inputFilename, String zipFilename) throws IOException {
File inputFile = new File(inputFilename); File inputFile = new File(inputFilename);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename)); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
try { try {
compressbyType(inputFile, out, ""); compress(inputFile, out);
// zip(inputFile, out, ""); // zip(inputFile, out, "");
} catch (Exception e) { } catch (Exception e) {
throw e; throw e;
} finally { } finally {
out.close(); out.close();
} }
} }
/** /**
* 压缩方法 * 压缩方法
* *
* @param inputFile * @param inputFile
* @param out * @param out
* @param base * @param base
* @throws IOException * @throws IOException
*/ */
@SuppressWarnings("unused") @SuppressWarnings("unused")
private static void zip(File inputFile, ZipOutputStream out, String base) throws IOException { private static void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
if (inputFile.isDirectory()) { if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles(); File[] inputFiles = inputFile.listFiles();
if (!base.equals("")) if (!base.equals(""))
out.putNextEntry(new ZipEntry(base + "/")); out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/"; base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) { for (int i = 0; i < inputFiles.length; i++) {
zip(inputFiles[i], out, base + inputFiles[i].getName()); zip(inputFiles[i], out, base + inputFiles[i].getName());
} }
} else { } else {
if (base.length() > 0) { if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base)); out.putNextEntry(new ZipEntry(base));
} else { } else {
out.putNextEntry(new ZipEntry(inputFile.getName())); out.putNextEntry(new ZipEntry(inputFile.getName()));
} }
FileInputStream in = new FileInputStream(inputFile); FileInputStream in = new FileInputStream(inputFile);
try { try {
int c; int c;
byte[] by = new byte[1024]; byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) { while ((c = in.read(by)) != -1) {
out.write(by, 0, c); out.write(by, 0, c);
} }
} catch (IOException e) { } catch (IOException e) {
throw e; throw e;
} finally { } finally {
in.close(); in.close();
} }
} }
} }
/** /**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩, * 按照原路径的类型就行压缩。文件路径直接把文件压缩,
* *
* @param src * @param src
* @param zos * @param zos
* @param baseDir * @param baseDir
*/ */
private static void compressbyType(File src, ZipOutputStream zos, String baseDir) { private static void compress(File src, ZipOutputStream zos) {
compressbyType(src, zos, "", true);
if (!src.exists()) }
return;
// 判断文件是否是文件,如果是文件调用compressFile方法,如果是路径,则调用compressDir方法; /**
if (src.isFile()) { * 按照原路径的类型就行压缩。文件路径直接把文件压缩,
// src是文件,调用此方法 *
compressFile(src, zos, baseDir); * @param src
* @param zos
} else if (src.isDirectory()) { * @param baseDir
// src是文件夹,调用此方法 */
compressDir(src, zos, baseDir); private static void compressbyType(File src, ZipOutputStream zos, String baseDir, boolean isFirst) {
if (!src.exists())
} return;
// 判断文件是否是文件,如果是文件调用compressFile方法,如果是路径,则调用compressDir方法;
} if (src.isFile()) {
// src是文件,调用此方法
/** compressFile(src, zos, baseDir);
* 压缩文件 } else if (src.isDirectory()) {
*/ // src是文件夹,调用此方法
private static void compressFile(File file, ZipOutputStream zos, String baseDir) { compressDir(src, zos, baseDir, isFirst);
if (!file.exists())
return; }
try { }
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
ZipEntry entry = new ZipEntry(baseDir + file.getName()); /**
zos.putNextEntry(entry); * 压缩文件
int count; */
byte[] buf = new byte[1024]; private static void compressFile(File file, ZipOutputStream zos, String baseDir) {
while ((count = bis.read(buf)) != -1) { if (!file.exists())
zos.write(buf, 0, count); return;
} try {
bis.close(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
} catch (Exception e) { ZipEntry entry = new ZipEntry(baseDir + file.getName());
} zos.putNextEntry(entry);
} int count;
byte[] buf = new byte[1024];
/** while ((count = bis.read(buf)) != -1) {
* 压缩文件夹 zos.write(buf, 0, count);
*/ }
private static void compressDir(File dir, ZipOutputStream zos, String baseDir) { bis.close();
if (!dir.exists()) } catch (Exception e) {
return; }
File[] files = dir.listFiles(); }
if (files.length == 0) {
try { /**
zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator)); * 压缩文件夹
} catch (IOException e) { */
e.printStackTrace(); private static void compressDir(File dir, ZipOutputStream zos, String baseDir, boolean isFirst) {
} if (!dir.exists())
} return;
for (File file : files) { File[] files = dir.listFiles();
compressbyType(file, zos, baseDir + dir.getName()+ File.separator); if (files.length == 0 && !StringUtil.isEmpty(baseDir)) {
} try {
} zos.putNextEntry(new ZipEntry(baseDir + dir.getName() + File.separator));
} catch (IOException e) {
} e.printStackTrace();
}
}
for (File file : files) {
if (isFirst) {
compressbyType(file, zos, baseDir, false);
} else {
compressbyType(file, zos, baseDir + dir.getName() + File.separator, false);
}
}
}
/**
* @Desc 压缩zip文件 @param inputFilename 待压缩的文件名称或文件夹路径名称 @param zipFilename
* 压缩后的文件完整的路径名称 @throws
*/
public static void zip(String inputFilename, ZipOutputStream out) throws IOException {
File inputFile = new File(inputFilename);
try {
compress(inputFile, out);
// zip(inputFile, out, "");
} catch (Exception e) {
throw e;
} finally {
out.close();
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment