Commit 278fbfa1 by songxiang

ZIP打包下载支持分大小打包

parent bec5df1d
......@@ -9,6 +9,8 @@ package com.pcloud.common.entity;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
/**
*
* @描述:
......@@ -107,6 +109,9 @@ public class UploadResultInfo extends BaseEntity {
*/
private String m3u8Url;
@ApiModelProperty("子文件数量")
private int fileItemCount;
public UploadResultInfo() {
super();
}
......@@ -152,6 +157,14 @@ public class UploadResultInfo extends BaseEntity {
this.url = url;
}
public int getFileItemCount() {
return fileItemCount;
}
public void setFileItemCount(int fileItemCount) {
this.fileItemCount = fileItemCount;
}
public String getM3u8Url() {
return m3u8Url;
}
......
......@@ -21,6 +21,7 @@ import java.util.List;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
......@@ -50,366 +51,370 @@ import it.sauronsoftware.jave.InputFormatException;
*/
public class FileUtils {
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
/**
* PDF
*/
public final static String PDF = "PDF";
/**
* XML
*/
public final static String XML = "XML";
/**
* IMAGE
*/
public final static String IMAGE = "IMAGE";
/**
* 音频
*/
public final static String AUDIO = "AUDIO";
/**
* 视频格式
*/
public final static String VIDEO = "VIDEO";
/**
* 压缩格式
*/
public final static String COMPRESS = "COMPRESS";
/**
* 办公软件
*/
public final static String EDITOR = "EDITOR";
/**
* 文本格式
*/
public final static String TEXT = "TEXT";
/**
* PDF文件格式
*/
public static final String[] PDF_GATHER = { "PDF" };
/**
* XML文件格式
*/
public static final String[] XML_GATHER = { "XML" };
/**
* 图片文件格式
*/
public static final String[] IMAGE_GATHER = { "BMP", "JPG", "GIF", "PNG", "PCX", "TIF", "JPEG", "SVG" };
/**
* 音頻文件格式 【"LRC" 为歌词字幕(这里作为音頻文件上传)】
*/
public static final String[] AUDIO_GATHER = { "AMR", "MP3", "WMA", "WAV", "AAC", "APE", "M4A" };
/**
* 視頻文件格式 【"SRT", "SSA", "SMI", "SUB" 为视频“外挂型”字幕(这里作为视频文件上传)】
*/
public static final String[] VIDEO_GATHER = { "MPG", "MPEG", "AVI", "MOV", "ASF", "WMV", "NAVI", "3GP", "MKV",
"FLV", "F4V", "RMVB", "RM", "MP4", "SRT", "SSA", "SMI", "SUB", "ASX", "M3U8" };
/**
* 压缩包文件格式
*/
public static final String[] COMPRESS_GATHER = { "RAR", "ZIP" };
/**
* 文件格式
*/
public static final String[] TEXT_GATHER = { "TXT", "LRC" };
/**
* 编辑器格式
*/
public static final String[] EDITOR_GATHER = { "DOC", "DOCX", "XLS", "XLSX", "PPT", "PPTX" };
/**
* word格式
*/
public static final String[] WORD_GATHER = { "DOC", "DOCX" };
/**
* excel格式
*/
public static final String[] EXCEL_GATHER = { "XLS", "XLSX", "XLSM", "XLT", "XLTX", "XLTM", "CSV" };
/**
* PPT格式
*/
public static final String[] PPT_GATHER = { "PPT", "PPTX" };
/**
* 根据文件类型获取文件分类
*
* @param fileType
* @return
*/
public static String getGatherName(String fileType) {
if (StringUtil.isEmpty(fileType)) {
return null;
}
// IMAGE
if (Arrays.asList(IMAGE_GATHER).contains(fileType.toUpperCase())) {
return IMAGE;
}
// AUDIO
if (Arrays.asList(AUDIO_GATHER).contains(fileType.toUpperCase())) {
return AUDIO;
}
// VIDEO
if (Arrays.asList(VIDEO_GATHER).contains(fileType.toUpperCase())) {
return VIDEO;
}
// PDF
if (Arrays.asList(PDF_GATHER).contains(fileType.toUpperCase())) {
return PDF;
}
// XML
if (Arrays.asList(XML_GATHER).contains(fileType.toUpperCase())) {
return XML;
}
// COMPRESS
if (Arrays.asList(COMPRESS_GATHER).contains(fileType.toUpperCase())) {
return COMPRESS;
}
// EDITOR
if (Arrays.asList(EDITOR_GATHER).contains(fileType.toUpperCase())) {
return EDITOR;
}
// COMPRESS
if (Arrays.asList(TEXT_GATHER).contains(fileType.toUpperCase())) {
return TEXT;
}
return null;
}
/**
* 传入文件夹路径,该方法能够实现创建整个路径
*
* @param path
* 文件夹路径,不包含文件名称及后缀名
*/
public static boolean isDir(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
String[] paths = filePath.split("/");
String temp = "";
for (int i = 0; i < paths.length; i++) {
if (i == 0) {
temp = paths[0];
} else {
temp += "/" + paths[i];
}
creatDir(temp);
}
return true;
}
/**
* 该方法用来判断文件是否存在,如果不存在则创建
*
* @param filePath
*/
public static boolean creatFile(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
return true;
} catch (Exception e) {
LOGGER.error("【文件工具】创建文件失败,<ERROR>:" + e.getMessage(), e);
}
}
return false;
}
/**
* 该方法用来判断文件是否存在,如果不存在则创建,该方法能够实现创建整个路径
*
* @param filePath
*/
public static boolean creatFiles(String filePath) {
LOGGER.info("【文件API】创建文件,同时创建整个路径.<START>.[filePath]=" + filePath);
if (StringUtil.isEmpty(filePath)) {
return false;
}
filePath = filePath.replace(File.separator, "/");
String dirPath = filePath.substring(0, filePath.lastIndexOf("/"));
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
try {
dirFile.mkdirs();
} catch (Exception e) {
LOGGER.error("【文件工具】创建文件夹失败,<ERROR>:" + e.getMessage(), e);
return false;
}
}
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
return true;
} catch (Exception e) {
LOGGER.error("【文件工具】创建文件失败,<ERROR>:" + e.getMessage(), e);
}
}
LOGGER.info("【文件API】创建文件,同时创建整个路径.<END>");
return false;
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做
*
* @param filePath
*/
public static boolean creatDir(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
return true;
}
return false;
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做 该方法能够实现创建整个路径
*
* @param filePath
*/
public static boolean creatDirs(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
return true;
}
return false;
}
/**
* 获取文件类型
*
* @param filePath
*/
public static String getFileType(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
int index1 = filePath.lastIndexOf(".");
if (index1 < 1) {
return null;
}
int index2 = filePath.lastIndexOf("?");
if (index2 > index1) {
filePath = filePath.substring(0, filePath.lastIndexOf("?"));
}
filePath = filePath.replace("/", OSConstant.SEPARATOR);
int index3 = filePath.lastIndexOf(OSConstant.SEPARATOR);
if (index3 > index1) {
return null;
}
return filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length()).toLowerCase();
}
/**
* 获取文件名称
*
* @param filePath
*/
public static String getFileName(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
String fileName = null;
filePath = filePath.replace("/", OSConstant.SEPARATOR);
int index1 = filePath.lastIndexOf(".");
if (index1 > -1) {
int index2 = filePath.lastIndexOf("?");
if (index2 > index1) {
filePath = filePath.substring(0, filePath.lastIndexOf("?"));
}
int index3 = filePath.lastIndexOf(OSConstant.SEPARATOR);
if (index3 > index1) {
fileName = filePath.substring(filePath.lastIndexOf(OSConstant.SEPARATOR) + 1);
} else {
fileName = filePath.substring(filePath.lastIndexOf(OSConstant.SEPARATOR) + 1,
filePath.lastIndexOf("."));
}
} else {
fileName = filePath.substring(filePath.lastIndexOf(OSConstant.SEPARATOR) + 1);
}
return formatName(fileName);
}
/**
* 获取文件名称包括后缀
*
* @param filePath
* @return
*/
public static String getFileNameAll(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
String fileName = getFileName(filePath);
if (StringUtil.isEmpty(fileName)) {
return null;
}
String fileType = getFileType(filePath);
return StringUtil.isEmpty(fileType) ? fileName : fileName + "." + fileType;
}
/**
* 格式化文件名称,过滤特殊字符(名称太长进行截断)
*
* @param fileName
*/
public static String formatName(String fileName) {
if (StringUtil.isEmpty(fileName)) {
return null;
}
String regEx = "[*/\\\\:?\"<|>\\s+%#&=.()]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(fileName);
String result = m.replaceAll("").trim();
// 文件名称过长的话,限制40个字
return result.length() > 40 ? result.substring(0, 40) : result;
}
/**
* 获取文件所在的目录
*
* @param filePath
* @return
*/
public static String getFileFolder(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
return filePath.substring(0, filePath.lastIndexOf(OSConstant.SEPARATOR));
}
/**
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
/**
* PDF
*/
public final static String PDF = "PDF";
/**
* XML
*/
public final static String XML = "XML";
/**
* IMAGE
*/
public final static String IMAGE = "IMAGE";
/**
* 音频
*/
public final static String AUDIO = "AUDIO";
/**
* 视频格式
*/
public final static String VIDEO = "VIDEO";
/**
* 压缩格式
*/
public final static String COMPRESS = "COMPRESS";
/**
* 办公软件
*/
public final static String EDITOR = "EDITOR";
/**
* 文本格式
*/
public final static String TEXT = "TEXT";
/**
* PDF文件格式
*/
public static final String[] PDF_GATHER = {"PDF"};
/**
* XML文件格式
*/
public static final String[] XML_GATHER = {"XML"};
/**
* 图片文件格式
*/
public static final String[] IMAGE_GATHER = {"BMP", "JPG", "GIF", "PNG", "PCX", "TIF", "JPEG", "SVG"};
/**
* 音頻文件格式 【"LRC" 为歌词字幕(这里作为音頻文件上传)】
*/
public static final String[] AUDIO_GATHER = {"AMR", "MP3", "WMA", "WAV", "AAC", "APE", "M4A"};
/**
* 視頻文件格式 【"SRT", "SSA", "SMI", "SUB" 为视频“外挂型”字幕(这里作为视频文件上传)】
*/
public static final String[] VIDEO_GATHER = {"MPG", "MPEG", "AVI", "MOV", "ASF", "WMV", "NAVI", "3GP", "MKV",
"FLV", "F4V", "RMVB", "RM", "MP4", "SRT", "SSA", "SMI", "SUB", "ASX", "M3U8"};
/**
* 压缩包文件格式
*/
public static final String[] COMPRESS_GATHER = {"RAR", "ZIP"};
/**
* 文件格式
*/
public static final String[] TEXT_GATHER = {"TXT", "LRC"};
/**
* 编辑器格式
*/
public static final String[] EDITOR_GATHER = {"DOC", "DOCX", "XLS", "XLSX", "PPT", "PPTX"};
/**
* word格式
*/
public static final String[] WORD_GATHER = {"DOC", "DOCX"};
/**
* excel格式
*/
public static final String[] EXCEL_GATHER = {"XLS", "XLSX", "XLSM", "XLT", "XLTX", "XLTM", "CSV"};
/**
* PPT格式
*/
public static final String[] PPT_GATHER = {"PPT", "PPTX"};
/**
* 根据文件类型获取文件分类
*
* @param fileType
* @return
*/
public static String getGatherName(String fileType) {
if (StringUtil.isEmpty(fileType)) {
return null;
}
// IMAGE
if (Arrays.asList(IMAGE_GATHER).contains(fileType.toUpperCase())) {
return IMAGE;
}
// AUDIO
if (Arrays.asList(AUDIO_GATHER).contains(fileType.toUpperCase())) {
return AUDIO;
}
// VIDEO
if (Arrays.asList(VIDEO_GATHER).contains(fileType.toUpperCase())) {
return VIDEO;
}
// PDF
if (Arrays.asList(PDF_GATHER).contains(fileType.toUpperCase())) {
return PDF;
}
// XML
if (Arrays.asList(XML_GATHER).contains(fileType.toUpperCase())) {
return XML;
}
// COMPRESS
if (Arrays.asList(COMPRESS_GATHER).contains(fileType.toUpperCase())) {
return COMPRESS;
}
// EDITOR
if (Arrays.asList(EDITOR_GATHER).contains(fileType.toUpperCase())) {
return EDITOR;
}
// COMPRESS
if (Arrays.asList(TEXT_GATHER).contains(fileType.toUpperCase())) {
return TEXT;
}
return null;
}
/**
* 传入文件夹路径,该方法能够实现创建整个路径
*
* @param path 文件夹路径,不包含文件名称及后缀名
*/
public static boolean isDir(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
String[] paths = filePath.split("/");
String temp = "";
for (int i = 0; i < paths.length; i++) {
if (i == 0) {
temp = paths[0];
} else {
temp += "/" + paths[i];
}
creatDir(temp);
}
return true;
}
/**
* 该方法用来判断文件是否存在,如果不存在则创建
*
* @param filePath
*/
public static boolean creatFile(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
return true;
} catch (Exception e) {
LOGGER.error("【文件工具】创建文件失败,<ERROR>:" + e.getMessage(), e);
}
}
return false;
}
/**
* 该方法用来判断文件是否存在,如果不存在则创建,该方法能够实现创建整个路径
*
* @param filePath
*/
public static boolean creatFiles(String filePath) {
LOGGER.info("【文件API】创建文件,同时创建整个路径.<START>.[filePath]=" + filePath);
if (StringUtil.isEmpty(filePath)) {
return false;
}
filePath = filePath.replace(File.separator, "/");
String dirPath = filePath.substring(0, filePath.lastIndexOf("/"));
File dirFile = new File(dirPath);
if (!dirFile.exists()) {
try {
dirFile.mkdirs();
} catch (Exception e) {
LOGGER.error("【文件工具】创建文件夹失败,<ERROR>:" + e.getMessage(), e);
return false;
}
}
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
return true;
} catch (Exception e) {
LOGGER.error("【文件工具】创建文件失败,<ERROR>:" + e.getMessage(), e);
}
}
LOGGER.info("【文件API】创建文件,同时创建整个路径.<END>");
return false;
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做
*
* @param filePath
*/
public static boolean creatDir(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdir();
return true;
}
return false;
}
/**
* 该方法用来判断文件夹是否存在,如果不存在则创建,存在则什么都不做 该方法能够实现创建整个路径
*
* @param filePath
*/
public static boolean creatDirs(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.exists()) {
file.mkdirs();
return true;
}
return false;
}
/**
* 获取文件类型
*
* @param filePath
*/
public static String getFileType(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
int index1 = filePath.lastIndexOf(".");
if (index1 < 1) {
return null;
}
int index2 = filePath.lastIndexOf("?");
if (index2 > index1) {
filePath = filePath.substring(0, filePath.lastIndexOf("?"));
}
filePath = filePath.replace("/", OSConstant.SEPARATOR);
int index3 = filePath.lastIndexOf(OSConstant.SEPARATOR);
if (index3 > index1) {
return null;
}
return filePath.substring(filePath.lastIndexOf(".") + 1, filePath.length()).toLowerCase();
}
/**
* 获取文件名称
*
* @param filePath
*/
public static String getFileName(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
String fileName = null;
filePath = filePath.replace("/", OSConstant.SEPARATOR);
int index1 = filePath.lastIndexOf(".");
if (index1 > -1) {
int index2 = filePath.lastIndexOf("?");
if (index2 > index1) {
filePath = filePath.substring(0, filePath.lastIndexOf("?"));
}
int index3 = filePath.lastIndexOf(OSConstant.SEPARATOR);
if (index3 > index1) {
fileName = filePath.substring(filePath.lastIndexOf(OSConstant.SEPARATOR) + 1);
} else {
fileName = filePath.substring(filePath.lastIndexOf(OSConstant.SEPARATOR) + 1,
filePath.lastIndexOf("."));
}
} else {
fileName = filePath.substring(filePath.lastIndexOf(OSConstant.SEPARATOR) + 1);
}
return formatName(fileName);
}
/**
* 获取文件名称包括后缀
*
* @param filePath
* @return
*/
public static String getFileNameAll(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
String fileName = getFileName(filePath);
if (StringUtil.isEmpty(fileName)) {
return null;
}
String fileType = getFileType(filePath);
return StringUtil.isEmpty(fileType) ? fileName : fileName + "." + fileType;
}
/**
* 格式化文件名称,过滤特殊字符(名称太长进行截断)
*
* @param fileName
*/
public static String formatName(String fileName) {
if (StringUtil.isEmpty(fileName)) {
return null;
}
try {
String regEx = "[*/\\\\:?\"<|>\\s+%#&=.()]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(fileName);
String result = m.replaceAll("").trim();
// 文件名称过长的话,限制40个字
return result.length() > 40 ? result.substring(0, 40) : result;
} catch (Exception e) {
LOGGER.error("【文件API】格式化文件名称.[formatName]:" + e.getMessage(), e);
return UUIDUitl.taskName();
}
}
/**
* 获取文件所在的目录
*
* @param filePath
* @return
*/
public static String getFileFolder(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return null;
}
return filePath.substring(0, filePath.lastIndexOf(OSConstant.SEPARATOR));
}
/**
* 获取http文件路径的域名
......@@ -429,675 +434,662 @@ public class FileUtils {
return null;
}
/**
* FileUtils工具 File @throws
*/
public static File copyURLToFile(String url, String dir) throws Exception {
try {
URL httpurl = new URL(url);
File f = new File(dir);
org.apache.commons.io.FileUtils.copyURLToFile(httpurl, f);
return f;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath
* 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean deleteFolder(String sPath) {
if (StringUtil.isEmpty(sPath)) {
return false;
}
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(sPath);
} else { // 为目录时调用删除目录方法
return deleteDirectory(sPath);
}
}
}
/**
* 删除单个文件
*
* @param sPath
* 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
if (StringUtil.isEmpty(sPath)) {
return false;
}
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath
* 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
if (StringUtil.isEmpty(sPath)) {
return false;
}
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
// 删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} // 删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
// 删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* 检查文件是否存在
*
* @param path
* @return
*/
public static boolean checkFile(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.isFile()) {
return false;
}
return true;
}
/**
* | FileUtils工具
*/
public static void downloadFileFromUrl(String url, String localFilePath) throws FileException {
LOGGER.info("【文件API】下载文件[本地文件].<START>.[url]=" + url + ",[localFilePath]=" + localFilePath);
creatFiles(localFilePath);
try {
if (StringTools.contains(url, AliyunConstant.OSS_CDN_URLS)) {
OssUtils.downloadFile(url, localFilePath);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value), localFilePath);
} else {
File f = new File(localFilePath);
URL httpurl = new URL(url);
org.apache.commons.io.FileUtils.copyURLToFile(httpurl, f);
}
} catch (Exception e) {
LOGGER.error("【文件API】下载文件.[copyURLToFile]:" + e.getMessage(), e);
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
}
}
/**
* IOUtils工具
*/
public static byte[] downloadByteFromUrl(String url) throws FileException {
LOGGER.info("【文件API】下载文件[byte].<START>.[url]=" + url);
if (StringUtil.isEmpty(url)) {
return null;
}
byte[] data = null;
try {
if (StringTools.contains(url, AliyunConstant.OSS_CDN_URLS)) {
data = OssUtils.downloadFile2Byte(url);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// data =
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value));
} else {
URL httpurl = new URL(url);
data = IOUtils.toByteArray(httpurl);
}
} catch (Exception e) {
LOGGER.error("【文件API】下载文件.[toByteArray]:" + e.getMessage(), e);
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
}
return data;
}
/**
* InputStream
*/
@Deprecated
public static InputStream downloadStreamFromUrl(String url) throws FileException {
try {
URL httpurl = new URL(url);
return httpurl.openStream();
} catch (Exception e) {
LOGGER.error("【文件API】下载文件.[openStream]:" + e.getMessage(), e);
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
}
}
/**
* 普通情况下保存字节数据到本地文件
*/
public synchronized static boolean saveBytesTofile(File file, byte[] data) throws BizException {
FileOutputStream fos = null;
// 建立输出字节流
try {
fos = new FileOutputStream(file);
fos.write(data); // 用FileOutputStream 的write方法写入字节数组
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 复制一个文件到另一个目录下
*
* @throws BizException
*/
public static void copyFileToFolder(File inputFile, File outputFile) throws BizException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 合并两个文件
*
* @param outFile
* 目标文件
* @param files
* 源文件
*/
public static void mergeFiles(File outFile, File leafFile) {
FileOutputStream fos = null;
FileInputStream fis = null;
if (!outFile.exists()) {
try {
outFile.createNewFile();
} catch (Exception e) {
throw new FileException(FileException.FILE_NOT_EXIST, "创建临时存储文件失败");
}
}
try {
// 合并其实就是文件的续写,写成true
fos = new FileOutputStream(outFile, true);
fis = new FileInputStream(leafFile);
int len = 0;
for (byte[] buf = new byte[1024 * 1024]; (len = fis.read(buf)) != -1;) {
fos.write(buf, 0, len);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
} catch (Exception e) {
}
}
}
/**
* 合并多个文件
*
* @param outFile
* 输出文件,
* @param leafFile
* 文件碎片集
*/
public static void mergeFileList(File outFile, List<File> leafFiles) {
FileOutputStream fos = null;
FileInputStream fis = null;
if (!outFile.exists()) {
try {
outFile.createNewFile();
} catch (Exception e) {
throw new FileException(FileException.FILE_NOT_EXIST, "创建临时存储文件失败");
}
}
try {
// 合并其实就是文件的续写,写成true
fos = new FileOutputStream(outFile, true);
byte[] buf = new byte[1024 * 1024];
for (File leafFile : leafFiles) {
fis = new FileInputStream(leafFile);
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
} catch (Exception e) {
}
}
}
/**
* 音频文件转换
*
* @author PENG
* @param data
* 音频byte数组
* @param sourExt
* 原始后缀
* @param ext
* 新后缀
* @return
*/
public static String audioFormatConvert(byte[] data, String sourExt, String ext) {
File tmpdir = new File("tempdir/local");
if (!tmpdir.exists()) {
try {
tmpdir.mkdirs();
} catch (SecurityException ex) {
System.out.println("无法创建临时文件夹");
ex.printStackTrace();
}
}
String name = UUID.randomUUID().toString();
File source = new File(tmpdir, name + "." + sourExt);
boolean creat = saveBytesTofile(source, data);
if (creat) {
File target = new File(tmpdir, name + "." + ext);
AudioAttributes audio = new AudioAttributes();
Encoder encoder = new Encoder();
audio.setCodec("libmp3lame");
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(ext.toLowerCase());
attrs.setAudioAttributes(audio);
try {
encoder.encode(source, target, attrs);
return target.getPath();
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
} catch (InputFormatException e) {
e.printStackTrace();
return null;
} catch (EncoderException e) {
e.printStackTrace();
return target.getPath();
}
} else {
return null;
}
}
/**
* 获取文本的内容
*
* @param url
* @return
*/
public static String getTextString(String url) {
LOGGER.info("【文件API】获取文本的内容.<START>.[url]=" + url);
if (StringUtil.isEmpty(url)) {
return null;
}
StringBuilder lrcString = new StringBuilder();
InputStream is = null;
InputStreamReader isr = null;
try {
byte[] bt = downloadByteFromUrl(url);
is = new ByteArrayInputStream(bt);
boolean isUTF8Bom = CpdetectorEncoding.isUTF8Bom(is);
if (isUTF8Bom) {
isr = new InputStreamReader(is, "UTF-8");
} else {
Object charSet = CpdetectorEncoding.getEncoding(bt, false);
if (charSet.toString().equals("UTF-8")) {
isr = new InputStreamReader(is, "UTF-8");
} else {
isr = new InputStreamReader(new ByteArrayInputStream(bt), "GBK");
}
}
BufferedReader br = new BufferedReader(isr);// 构造一个BufferedReader类来读取文件
String s;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
lrcString.append(System.lineSeparator() + s);
}
} catch (Exception e) {
LOGGER.error("【文件工具】读取文件内容.<ERROR>:" + e.getMessage(), e);
} finally {
try {
if (isr != null)
isr.close();
if (is != null)
is.close();
} catch (Exception e2) {
}
}
LOGGER.info("【文件API】获取文本的内容.<END>");
return lrcString.toString();
}
/**
* java 8 读取文本内容
*
* @param url
* @return
*/
public static String getTextString8(String url) {
LOGGER.info("【文件API】获取文本的内容8.<START>.[url]=" + url);
StringBuilder result = new StringBuilder();
String fileType = getFileType(url);
String localFilePath = FilePathConst.TEXT_PATH + UUIDUitl.taskName() + "." + fileType;
downloadFileFromUrl(url, localFilePath);
try {
List<String> lines = Files.readAllLines(Paths.get(localFilePath), StandardCharsets.UTF_8);
for (String line : lines) {
result.append(line + System.lineSeparator());
}
} catch (Exception e) {
LOGGER.error("【文件工具】读取文件内容8.<ERROR>:" + e.getMessage(), e);
}
deleteFile(localFilePath);
LOGGER.info("【文件API】获取文本的内容8.<END>");
return result.toString();
}
/**
* 文件剪切,网络路径、按文件大小的百分比
*
* @param inputFile
* @param cutPercent
* 1~100的整数
* @return 返回剪切后上传后的文件路径
* @throws BizException
*/
public static String clipByPercent(String url, int percent) throws BizException {
LOGGER.info("【文件API】文件剪切,按百分比.<START>.[url]=" + url);
if (StringUtil.isEmpty(url)) {
return null;
}
String fileType = FileUtils.getFileType(url);
if (!FileUtils.VIDEO.equals(FileUtils.getGatherName(fileType))) {
return null;
}
// 下载文件
String downloadPath = FilePathConst.DOWNLOAD_PATH + UUIDUitl.generateString(32) + "." + fileType;
FileUtils.downloadFileFromUrl(url, downloadPath);
// 剪切文件
String cutPath = clipByPercent(new File(downloadPath), percent);
// 重新上传文件
UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile(cutPath, null);
// 删除本地文件
FileUtils.deleteFile(downloadPath);
FileUtils.deleteFile(cutPath);
LOGGER.info("【文件API】文件剪切,按百分比.<END>.[uploadResultInfo]=" + uploadResultInfo);
return uploadResultInfo == null ? null : uploadResultInfo.getUrl();
}
/**
* 文件剪切,本地路径、按百分比
*
* @param inputFile
* @param cutPercent
* 1~100的整数
* @return 返回本地路径地址
* @throws BizException
*/
public static String clipByPercent(File inputFile, int cutPercent) throws BizException {
LOGGER.info("【文件API】文件剪切.<START>.[cutPercent]=" + cutPercent);
if (inputFile == null || inputFile.length() <= 0) {
throw new FileException(FileException.FILE_NOT_EXIST, "不是有效的文件");
}
if (cutPercent > 100 || cutPercent < 1) {
throw new FileException(FileException.FILE_READ_FAILURE, "百分比必须是1~100之间的整数");
}
return clipByLength(inputFile, Math.round(inputFile.length() * cutPercent / 100.0d));
}
/**
* 文件剪切
*
* @param inputFile
* 待剪切的文件
* @param outputFile
* 剪切后的文件
* @param cutLength
* 剪切长度(Byte)
*/
public static String clipByLength(File inputFile, long cutLength) throws BizException {
LOGGER.info("【文件API】文件剪切.<START>.[cutLength]=" + cutLength);
if (inputFile == null || inputFile.length() <= 0) {
throw new FileException(FileException.FILE_NOT_EXIST, "不是有效的文件");
}
if (cutLength < 1024) {
throw new FileException(FileException.FILE_READ_FAILURE, "剪切的文件必须大于1KB");
}
// 输出文件路径
String outFilePath = FilePathConst.CUT_PATH + UUIDUitl.generateString(12) + "_" + inputFile.getName();
creatFiles(outFilePath);
// 剪切长度
cutLength = cutLength > inputFile.length() ? inputFile.length() : cutLength;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 两个输入流
bis = new BufferedInputStream(new FileInputStream(inputFile));
// 缓冲字节输出流
bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
// 读取多少次
long index = cutLength % 1024 > 0 ? cutLength / 1024 + 1 : cutLength / 1024;
int mod = (int) cutLength % 1024;
byte[] bt = new byte[1024];
int len = 0;
for (int i = 0; i < index; i++) {
if (i + 1 == index && mod != 0) {
bt = new byte[mod];
len = bis.read(bt);
} else {
len = bis.read(bt);
}
if (len != -1) {
bos.write(bt, 0, len);
}
}
bos.flush();
} catch (IOException e) {
LOGGER.error("【文件工具】剪切文件内容:" + e.getMessage(), e);
throw new FileException(FileException.FILE_READ_FAILURE, "文件剪切失败");
} finally {
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
LOGGER.error("【文件工具】文件流关闭失败:" + e.getMessage(), e);
}
}
LOGGER.info("【文件API】文件剪切.<END>");
return outFilePath;
}
/**
* 将文件转成base64 字符串
*
* @param path文件路径
* @return *
* @throws Exception
*/
public static String encodeBase64File(byte[] data) throws BizException {
try {
return new String(Base64.encodeBase64(data));
} catch (Exception e) {
LOGGER.error("【文件工具】将base64字符解码保存文件失败:" + e.getMessage(), e);
return null;
}
}
/**
* 将base64字符解码保存文件
*
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static boolean decoderBase64File(String base64Code, String targetPath) throws BizException {
if (base64Code == null)
return false;
OutputStream out = null;
try {
// Base64解码
byte[] b = Base64.decodeBase64(targetPath);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
out = new FileOutputStream(targetPath);
out.write(b);
out.flush();
} catch (Exception e) {
LOGGER.error("【文件工具】将base64字符解码保存文件失败:" + e.getMessage(), e);
return false;
} finally {
try {
if (out != null)
out.close();
} catch (Exception e) {
}
}
return true;
}
/**
* 本地文件到byte数组
*
* @param path
* @return
*/
public static byte[] file2byte(String path) {
byte[] data = null;
FileInputStream fis = null;
ByteArrayOutputStream os = null;
try {
fis = new FileInputStream(new File(path));
os = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = fis.read(buf)) != -1) {
os.write(buf, 0, numBytesRead);
}
data = os.toByteArray();
} catch (Exception e) {
LOGGER.error("【文件工具】文件到byte数组:" + e.getMessage(), e);
} finally {
try {
if (fis != null)
fis.close();
if (os != null)
os.close();
} catch (IOException e) {
LOGGER.error("【文件工具】文件流关闭失败:" + e.getMessage(), e);
}
}
return data;
}
/**
* 从输入流中获取数据
*
* @param inStream
* 输入流
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
try {
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
} catch (Exception e) {
LOGGER.error("【文件工具】文件流到byte数组:" + e.getMessage(), e);
} finally {
inStream.close();
}
return outStream.toByteArray();
}
/**
* FileUtils工具 File @throws
*/
public static File copyURLToFile(String url, String dir) throws Exception {
try {
URL httpurl = new URL(url);
File f = new File(dir);
org.apache.commons.io.FileUtils.copyURLToFile(httpurl, f);
return f;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 根据路径删除指定的目录或文件,无论存在与否
*
* @param sPath 要删除的目录或文件
* @return 删除成功返回 true,否则返回 false。
*/
public static boolean deleteFolder(String sPath) {
if (StringUtil.isEmpty(sPath)) {
return false;
}
boolean flag = false;
File file = new File(sPath);
// 判断目录或文件是否存在
if (!file.exists()) { // 不存在返回 false
return flag;
} else {
// 判断是否为文件
if (file.isFile()) { // 为文件时调用删除文件方法
return deleteFile(sPath);
} else { // 为目录时调用删除目录方法
return deleteDirectory(sPath);
}
}
}
/**
* 删除单个文件
*
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
if (StringUtil.isEmpty(sPath)) {
return false;
}
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 删除目录(文件夹)以及目录下的文件
*
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
if (StringUtil.isEmpty(sPath)) {
return false;
}
// 如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
// 如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
// 删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
// 删除子文件
if (files[i].isFile()) {
flag = deleteFile(files[i].getAbsolutePath());
if (!flag)
break;
} // 删除子目录
else {
flag = deleteDirectory(files[i].getAbsolutePath());
if (!flag)
break;
}
}
if (!flag)
return false;
// 删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
/**
* 检查文件是否存在
*
* @param filePath
* @return
*/
public static boolean checkFile(String filePath) {
if (StringUtil.isEmpty(filePath)) {
return false;
}
File file = new File(filePath);
if (!file.isFile()) {
return false;
}
return true;
}
/**
* | FileUtils工具
*/
public static void downloadFileFromUrl(String url, String localFilePath) throws FileException {
LOGGER.info("【文件API】下载文件[本地文件].<START>.[url]=" + url + ",[localFilePath]=" + localFilePath);
creatFiles(localFilePath);
try {
if (StringTools.contains(url, AliyunConstant.OSS_CDN_URLS)) {
OssUtils.downloadFile(url, localFilePath);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value), localFilePath);
} else {
File f = new File(localFilePath);
URL httpurl = new URL(url);
org.apache.commons.io.FileUtils.copyURLToFile(httpurl, f);
}
} catch (Exception e) {
LOGGER.error("【文件API】下载文件.[copyURLToFile]:" + e.getMessage(), e);
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
}
}
/**
* IOUtils工具
*/
public static byte[] downloadByteFromUrl(String url) throws FileException {
LOGGER.info("【文件API】下载文件[byte].<START>.[url]=" + url);
if (StringUtil.isEmpty(url)) {
return null;
}
byte[] data = null;
try {
if (StringTools.contains(url, AliyunConstant.OSS_CDN_URLS)) {
data = OssUtils.downloadFile2Byte(url);
// } else if (url.contains("/group")) {
// Map<String, String> paramMap = FdfsUtils.splitFilePath(url);
// data =
// FdfsUtils.download(paramMap.get(FdfsEnum.GROUP_NAME.value),
// paramMap.get(FdfsEnum.FILE_ID.value));
} else {
URL httpurl = new URL(url);
data = IOUtils.toByteArray(httpurl);
}
} catch (Exception e) {
LOGGER.error("【文件API】下载文件.[toByteArray]:" + e.getMessage(), e);
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
}
return data;
}
/**
* InputStream
*/
@Deprecated
public static InputStream downloadStreamFromUrl(String url) throws FileException {
try {
URL httpurl = new URL(url);
return httpurl.openStream();
} catch (Exception e) {
LOGGER.error("【文件API】下载文件.[openStream]:" + e.getMessage(), e);
throw new FileException(FileException.FILE_DOWNLOAD_FAILURE, "下载文件失败");
}
}
/**
* 普通情况下保存字节数据到本地文件
*/
public synchronized static boolean saveBytesTofile(File file, byte[] data) throws BizException {
FileOutputStream fos = null;
// 建立输出字节流
try {
fos = new FileOutputStream(file);
fos.write(data); // 用FileOutputStream 的write方法写入字节数组
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 复制一个文件到另一个目录下
*
* @throws BizException
*/
public static void copyFileToFolder(File inputFile, File outputFile) throws BizException {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(inputFile);
fos = new FileOutputStream(outputFile);
int len = 0;
byte[] buf = new byte[1024];
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fis.close();
fos.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
/**
* 合并两个文件
*
* @param outFile 目标文件
* @param leafFile 源文件
*/
public static void mergeFiles(File outFile, File leafFile) {
FileOutputStream fos = null;
FileInputStream fis = null;
if (!outFile.exists()) {
try {
outFile.createNewFile();
} catch (Exception e) {
throw new FileException(FileException.FILE_NOT_EXIST, "创建临时存储文件失败");
}
}
try {
// 合并其实就是文件的续写,写成true
fos = new FileOutputStream(outFile, true);
fis = new FileInputStream(leafFile);
int len = 0;
for (byte[] buf = new byte[1024 * 1024]; (len = fis.read(buf)) != -1; ) {
fos.write(buf, 0, len);
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
} catch (Exception e) {
}
}
}
/**
* 合并多个文件
*
* @param outFile 输出文件,
* @param leafFiles 文件碎片集
*/
public static void mergeFileList(File outFile, List<File> leafFiles) {
FileOutputStream fos = null;
FileInputStream fis = null;
if (!outFile.exists()) {
try {
outFile.createNewFile();
} catch (Exception e) {
throw new FileException(FileException.FILE_NOT_EXIST, "创建临时存储文件失败");
}
}
try {
// 合并其实就是文件的续写,写成true
fos = new FileOutputStream(outFile, true);
byte[] buf = new byte[1024 * 1024];
for (File leafFile : leafFiles) {
fis = new FileInputStream(leafFile);
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (fis != null)
fis.close();
if (fos != null)
fos.close();
} catch (Exception e) {
}
}
}
/**
* 音频文件转换
*
* @param data 音频byte数组
* @param sourExt 原始后缀
* @param ext 新后缀
* @return
* @author PENG
*/
public static String audioFormatConvert(byte[] data, String sourExt, String ext) {
File tmpdir = new File("tempdir/local");
if (!tmpdir.exists()) {
try {
tmpdir.mkdirs();
} catch (SecurityException ex) {
System.out.println("无法创建临时文件夹");
ex.printStackTrace();
}
}
String name = UUID.randomUUID().toString();
File source = new File(tmpdir, name + "." + sourExt);
boolean creat = saveBytesTofile(source, data);
if (creat) {
File target = new File(tmpdir, name + "." + ext);
AudioAttributes audio = new AudioAttributes();
Encoder encoder = new Encoder();
audio.setCodec("libmp3lame");
EncodingAttributes attrs = new EncodingAttributes();
attrs.setFormat(ext.toLowerCase());
attrs.setAudioAttributes(audio);
try {
encoder.encode(source, target, attrs);
return target.getPath();
} catch (IllegalArgumentException e) {
e.printStackTrace();
return null;
} catch (InputFormatException e) {
e.printStackTrace();
return null;
} catch (EncoderException e) {
e.printStackTrace();
return target.getPath();
}
} else {
return null;
}
}
/**
* 获取文本的内容
*
* @param url
* @return
*/
public static String getTextString(String url) {
LOGGER.info("【文件API】获取文本的内容.<START>.[url]=" + url);
if (StringUtil.isEmpty(url)) {
return null;
}
StringBuilder lrcString = new StringBuilder();
InputStream is = null;
InputStreamReader isr = null;
try {
byte[] bt = downloadByteFromUrl(url);
is = new ByteArrayInputStream(bt);
boolean isUTF8Bom = CpdetectorEncoding.isUTF8Bom(is);
if (isUTF8Bom) {
isr = new InputStreamReader(is, "UTF-8");
} else {
Object charSet = CpdetectorEncoding.getEncoding(bt, false);
if (charSet.toString().equals("UTF-8")) {
isr = new InputStreamReader(is, "UTF-8");
} else {
isr = new InputStreamReader(new ByteArrayInputStream(bt), "GBK");
}
}
BufferedReader br = new BufferedReader(isr);// 构造一个BufferedReader类来读取文件
String s;
while ((s = br.readLine()) != null) {// 使用readLine方法,一次读一行
lrcString.append(System.lineSeparator() + s);
}
} catch (Exception e) {
LOGGER.error("【文件工具】读取文件内容.<ERROR>:" + e.getMessage(), e);
} finally {
try {
if (isr != null)
isr.close();
if (is != null)
is.close();
} catch (Exception e2) {
}
}
LOGGER.info("【文件API】获取文本的内容.<END>");
return lrcString.toString();
}
/**
* java 8 读取文本内容
*
* @param url
* @return
*/
public static String getTextString8(String url) {
LOGGER.info("【文件API】获取文本的内容8.<START>.[url]=" + url);
StringBuilder result = new StringBuilder();
String fileType = getFileType(url);
String localFilePath = url;
if (url.startsWith("http")) {
localFilePath = FilePathConst.TEXT_PATH + UUIDUitl.taskName() + "." + fileType;
downloadFileFromUrl(url, localFilePath);
}
try {
Stream<String> lines = Files.lines(Paths.get(localFilePath), StandardCharsets.UTF_8);
lines.forEach(line -> {
result.append(line + System.lineSeparator());
});
} catch (Exception e) {
LOGGER.error("【文件工具】读取文件内容8.<ERROR>:" + e.getMessage(), e);
} finally {
deleteFile(localFilePath);
}
LOGGER.info("【文件API】获取文本的内容8.<END>");
return result.toString();
}
/**
* 文件剪切,网络路径、按文件大小的百分比
*
* @param url
* @param percent 1~100的整数
* @return 返回剪切后上传后的文件路径
* @throws BizException
*/
public static String clipByPercent(String url, int percent) throws BizException {
LOGGER.info("【文件API】文件剪切,按百分比.<START>.[url]=" + url);
if (StringUtil.isEmpty(url)) {
return null;
}
String fileType = FileUtils.getFileType(url);
if (!FileUtils.VIDEO.equals(FileUtils.getGatherName(fileType))) {
return null;
}
// 下载文件
String downloadPath = FilePathConst.DOWNLOAD_PATH + UUIDUitl.generateString(32) + "." + fileType;
FileUtils.downloadFileFromUrl(url, downloadPath);
// 剪切文件
String cutPath = clipByPercent(new File(downloadPath), percent);
// 重新上传文件
UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile(cutPath, null);
// 删除本地文件
FileUtils.deleteFile(downloadPath);
FileUtils.deleteFile(cutPath);
LOGGER.info("【文件API】文件剪切,按百分比.<END>.[uploadResultInfo]=" + uploadResultInfo);
return uploadResultInfo == null ? null : uploadResultInfo.getUrl();
}
/**
* 文件剪切,本地路径、按百分比
*
* @param inputFile
* @param cutPercent 1~100的整数
* @return 返回本地路径地址
* @throws BizException
*/
public static String clipByPercent(File inputFile, int cutPercent) throws BizException {
LOGGER.info("【文件API】文件剪切.<START>.[cutPercent]=" + cutPercent);
if (inputFile == null || inputFile.length() <= 0) {
throw new FileException(FileException.FILE_NOT_EXIST, "不是有效的文件");
}
if (cutPercent > 100 || cutPercent < 1) {
throw new FileException(FileException.FILE_READ_FAILURE, "百分比必须是1~100之间的整数");
}
return clipByLength(inputFile, Math.round(inputFile.length() * cutPercent / 100.0d));
}
/**
* 文件剪切
*
* @param inputFile 待剪切的文件
* @param cutLength 剪切长度(Byte)
*/
public static String clipByLength(File inputFile, long cutLength) throws BizException {
LOGGER.info("【文件API】文件剪切.<START>.[cutLength]=" + cutLength);
if (inputFile == null || inputFile.length() <= 0) {
throw new FileException(FileException.FILE_NOT_EXIST, "不是有效的文件");
}
if (cutLength < 1024) {
throw new FileException(FileException.FILE_READ_FAILURE, "剪切的文件必须大于1KB");
}
// 输出文件路径
String outFilePath = FilePathConst.CUT_PATH + UUIDUitl.generateString(12) + "_" + inputFile.getName();
creatFiles(outFilePath);
// 剪切长度
cutLength = cutLength > inputFile.length() ? inputFile.length() : cutLength;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 两个输入流
bis = new BufferedInputStream(new FileInputStream(inputFile));
// 缓冲字节输出流
bos = new BufferedOutputStream(new FileOutputStream(outFilePath));
// 读取多少次
long index = cutLength % 1024 > 0 ? cutLength / 1024 + 1 : cutLength / 1024;
int mod = (int) cutLength % 1024;
byte[] bt = new byte[1024];
int len = 0;
for (int i = 0; i < index; i++) {
if (i + 1 == index && mod != 0) {
bt = new byte[mod];
len = bis.read(bt);
} else {
len = bis.read(bt);
}
if (len != -1) {
bos.write(bt, 0, len);
}
}
bos.flush();
} catch (IOException e) {
LOGGER.error("【文件工具】剪切文件内容:" + e.getMessage(), e);
throw new FileException(FileException.FILE_READ_FAILURE, "文件剪切失败");
} finally {
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
LOGGER.error("【文件工具】文件流关闭失败:" + e.getMessage(), e);
}
}
LOGGER.info("【文件API】文件剪切.<END>");
return outFilePath;
}
/**
* 将文件转成base64 字符串
*
* @param data
* @return *
* @throws Exception
*/
public static String encodeBase64File(byte[] data) throws BizException {
try {
return new String(Base64.encodeBase64(data));
} catch (Exception e) {
LOGGER.error("【文件工具】将base64字符解码保存文件失败:" + e.getMessage(), e);
return null;
}
}
/**
* 将base64字符解码保存文件
*
* @param base64Code
* @param targetPath
* @throws Exception
*/
public static boolean decoderBase64File(String base64Code, String targetPath) throws BizException {
if (base64Code == null)
return false;
OutputStream out = null;
try {
// Base64解码
byte[] b = Base64.decodeBase64(targetPath);
for (int i = 0; i < b.length; ++i) {
if (b[i] < 0) {// 调整异常数据
b[i] += 256;
}
}
// 生成jpeg图片
out = new FileOutputStream(targetPath);
out.write(b);
out.flush();
} catch (Exception e) {
LOGGER.error("【文件工具】将base64字符解码保存文件失败:" + e.getMessage(), e);
return false;
} finally {
try {
if (out != null)
out.close();
} catch (Exception e) {
}
}
return true;
}
/**
* 本地文件到byte数组
*
* @param path
* @return
*/
public static byte[] file2byte(String path) {
byte[] data = null;
FileInputStream fis = null;
ByteArrayOutputStream os = null;
try {
fis = new FileInputStream(new File(path));
os = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int numBytesRead = 0;
while ((numBytesRead = fis.read(buf)) != -1) {
os.write(buf, 0, numBytesRead);
}
data = os.toByteArray();
} catch (Exception e) {
LOGGER.error("【文件工具】文件到byte数组:" + e.getMessage(), e);
} finally {
try {
if (fis != null)
fis.close();
if (os != null)
os.close();
} catch (IOException e) {
LOGGER.error("【文件工具】文件流关闭失败:" + e.getMessage(), e);
}
}
return data;
}
/**
* 从输入流中获取数据
*
* @param inStream 输入流
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
try {
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
} catch (Exception e) {
LOGGER.error("【文件工具】文件流到byte数组:" + e.getMessage(), e);
} finally {
inStream.close();
}
return outStream.toByteArray();
}
}
......@@ -18,12 +18,16 @@ import org.slf4j.LoggerFactory;
import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;
import com.google.common.collect.Lists;
import com.pcloud.common.constant.OSConstant;
import com.pcloud.common.dto.CompressDTO;
import com.pcloud.common.dto.CompressDTO.CompressFileDTO;
import com.pcloud.common.entity.UploadResultInfo;
import com.pcloud.common.exceptions.BizException;
import com.pcloud.common.exceptions.FileException;
import com.pcloud.common.utils.FileUtils;
import com.pcloud.common.utils.ListUtils;
import com.pcloud.common.utils.LocalDateUtils;
import com.pcloud.common.utils.UUIDUitl;
import com.pcloud.common.utils.aliyun.OssUtils;
import com.pcloud.common.utils.string.StringUtil;
......@@ -35,357 +39,406 @@ import com.pcloud.common.utils.string.StringUtil;
*/
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
* String[0]=文件名称, String[1]=文件地址
* @param zipName
* @return
* @throws BizException
*/
public static UploadResultInfo zip(List<String[]> fileUrlList, String zipName) throws BizException {
LOGGER.info("【压缩】压缩文件.<START>");
if (ListUtils.isEmpty(fileUrlList)) {
return null;
}
zipName = FileUtils.formatName(zipName);
String tempZipName = zipName + "_" + UUIDUitl.generateString(12);
// 检查临时文件夹是否存在,不存在就创建
String fileFolderPath = FILE_LOCAL_PATH + tempZipName;
FileUtils.isDir(fileFolderPath);
// 下载文件到文件夹中
int idx = 1;
for (String[] files : fileUrlList) {
String fileType = FileUtils.getFileType(files[1]);
String fileName = FileUtils.formatName(files[0]);
String downloadLocalPath = fileFolderPath + "/" + fileName + "_" + idx + "." + fileType;
FileUtils.downloadFileFromUrl(files[1], downloadLocalPath);
idx++;
}
// 检查压缩包临时文件夹是否存在,不存在就创建
FileUtils.isDir(ZIP_FILE_PATH);
String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip";
try {
CompressUtils.zip(fileFolderPath, zipFilePath);
} catch (Exception e) {
LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.ZIP_ERROR, "压缩失败!");
}
// 上传文件到服务器中
UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName);
// 删除产生的文件
FileUtils.deleteDirectory(fileFolderPath);
FileUtils.deleteFile(zipFilePath);
LOGGER.info("【压缩】压缩文件.<END>");
return uploadResultInfo;
}
/**
* 分拆大小值
*/
private static long CUT_SIZE = 1 * 1024 * 1024 * 1024;
/**
* 压缩文件(带目录)
*
* @param catalogFiles
* key : 目录名 String[0]=文件名称, String[1]=文件地址
* @param zipName
* @return
* @throws BizException
*/
public static UploadResultInfo zipByCatalog(Map<String, List<String[]>> catalogFiles, String zipName)
throws BizException {
LOGGER.info("【压缩】压缩文件.<START>");
if (MapUtils.isEmpty(catalogFiles)) {
return null;
}
zipName = FileUtils.formatName(zipName);
String tempZipName = zipName + "_" + UUIDUitl.generateString(12);
String parentPath = FILE_LOCAL_PATH + tempZipName;
FileUtils.isDir(parentPath);
for (String catalog : catalogFiles.keySet()) {
String downloadPath;
// 检查临时文件夹是否存在,不存在就创建
if (!StringUtil.isEmpty(catalog)) {
String catalogFolderPath = parentPath + "/" + catalog;
FileUtils.isDir(catalogFolderPath);
downloadPath = catalogFolderPath;
} else {
downloadPath = parentPath;
}
List<String[]> fileUrlList = catalogFiles.get(catalog);
// 下载文件到文件夹中
int idx = 1;
for (String[] files : fileUrlList) {
String fileType = FileUtils.getFileType(files[1]);
String fileName = FileUtils.formatName(files[0]);
String downloadLocalPath = downloadPath + "/" + fileName + "_" + idx + "." + fileType;
FileUtils.downloadFileFromUrl(files[1], downloadLocalPath);
idx++;
}
}
// 检查压缩包临时文件夹是否存在,不存在就创建
FileUtils.isDir(ZIP_FILE_PATH);
String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip";
try {
CompressUtils.zip(parentPath, zipFilePath);
} catch (Exception e) {
LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.ZIP_ERROR, "压缩失败!");
}
// 上传文件到服务器中
UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName);
// 删除产生的文件
FileUtils.deleteDirectory(parentPath);
FileUtils.deleteFile(zipFilePath);
LOGGER.info("【压缩】压缩文件.<END>");
return uploadResultInfo;
}
/**
* 分批次打包,压缩文件集超过指定大小后,分多个包压缩,目前限定1个压缩包1G。
*
* @param compressDTO
* @return
* @throws BizException
*/
public static List<UploadResultInfo> zips(CompressDTO compressDTO) throws BizException {
LOGGER.info("【压缩】分批次打包文件.<START>.[compressDTO]=" + compressDTO);
if (compressDTO == null) {
return null;
}
List<CompressFileDTO> compressFileDTOs = compressDTO.getCompressFileDTOs();
if (ListUtils.isEmpty(compressFileDTOs)) {
return null;
}
List<UploadResultInfo> uploadResultInfos = Lists.newArrayList();
List<String[]> fileUrlList = Lists.newArrayList();
long totalFileSize = 0;
int size = compressFileDTOs.size();
int index = 1;
for (int i = 0; i < size; i++) {
CompressFileDTO compressFileDTO = compressFileDTOs.get(i);
totalFileSize += compressFileDTO.getFileSize();
fileUrlList.add(new String[]{compressFileDTO.getFileName(), compressFileDTO.getFileUrl()});
if (totalFileSize > CUT_SIZE) {
String zipName = compressDTO.getCompressName() + "_" + index;
UploadResultInfo uploadResultInfo = zip(fileUrlList, zipName);
uploadResultInfo.setUrl(OssUtils.urlAddKeyLong2Short(uploadResultInfo.getUrl()));
uploadResultInfo.setFileItemCount(fileUrlList.size());
uploadResultInfos.add(uploadResultInfo);
// 零时变量复位
totalFileSize = 0;
fileUrlList = Lists.newArrayList();
index++;
}
}
// 打包剩余总大小不足1G的文件
if (fileUrlList.size() > 0) {
UploadResultInfo uploadResultInfo = zip(fileUrlList, compressDTO.getCompressName() + "_" + index);
uploadResultInfo.setUrl(OssUtils.urlAddKeyLong2Short(uploadResultInfo.getUrl()));
uploadResultInfo.setFileItemCount(fileUrlList.size());
uploadResultInfos.add(uploadResultInfo);
}
LOGGER.info("【压缩】分批次打包文件.<END>");
return uploadResultInfos;
}
/**
* @Desc ZIP文件解压
* @param zipPath
* ZIP文件目录
* @param destDir
* 解压目录
* @throws Exception
*/
private static void unzip(String zipPath, String destDir) throws Exception {
try {
Project p = new Project();
Expand e = new Expand();
e.setProject(p);
e.setSrc(new File(zipPath));
e.setOverwrite(false);
e.setDest(new File(destDir));
e.setEncoding("gbk");
e.execute();
} catch (Exception e) {
throw e;
}
}
/**
* 压缩文件
*
* @param fileUrlList String[0]=文件名称, String[1]=文件地址
* @param zipName 压缩包的名称
* @return
* @throws BizException
*/
public static UploadResultInfo zip(List<String[]> fileUrlList, String zipName) throws BizException {
LOGGER.info("【压缩】压缩文件.<START>");
if (ListUtils.isEmpty(fileUrlList)) {
return null;
}
zipName = FileUtils.formatName(zipName);
String tempZipName = zipName + "_" + LocalDateUtils.getYmdhmss();
String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip";
// 检查临时文件夹是否存在,不存在就创建
String fileFolderPath = FILE_LOCAL_PATH + tempZipName;
FileUtils.isDir(fileFolderPath);
UploadResultInfo uploadResultInfo = null;
try {
// 下载文件到文件夹中
int idx = 1;
for (String[] files : fileUrlList) {
String fileType = FileUtils.getFileType(files[1]);
String fileName = FileUtils.formatName(files[0]);
String downloadLocalPath = fileFolderPath + "/" + fileName + "_" + idx + "." + fileType;
FileUtils.downloadFileFromUrl(files[1], downloadLocalPath);
idx++;
}
// 检查压缩包临时文件夹是否存在,不存在就创建
FileUtils.isDir(ZIP_FILE_PATH);
CompressUtils.zip(fileFolderPath, zipFilePath);
// 上传文件到服务器中
uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName);
uploadResultInfo.setFileName(zipName);
} catch (Exception e) {
LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.ZIP_ERROR, "压缩失败!");
} finally {
// 删除产生的文件
FileUtils.deleteDirectory(fileFolderPath);
FileUtils.deleteFile(zipFilePath);
}
LOGGER.info("【压缩】压缩文件.<END>");
return uploadResultInfo;
}
/**
* @Desc rar文件解压
* @param rarPath
* rar路径
* @param destDir
* 解压到的文件夹
* @throws Exception
*/
public static void unrar(String rarPath, String destDir) throws Exception {
if (!rarPath.toLowerCase().endsWith(".rar")) {
throw new Exception("非rar文件!");
}
File dstDiretory = new File(destDir);
if (!dstDiretory.exists()) {
dstDiretory.mkdirs();
}
Archive a = new Archive(new File(rarPath));
if (a != null) {
FileHeader fh = a.nextFileHeader();
while (fh != null) {
String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW();
if (fh.isDirectory()) {
File fol = new File(destDir + File.separator + fileName);
fol.mkdirs();
} else {
File out = new File(destDir + File.separator + fileName.trim());
if (!out.exists()) {
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
}
fh = a.nextFileHeader();
}
a.close();
}
}
/**
* 压缩文件(带目录)
*
* @param catalogFiles key : 目录名 String[0]=文件名称, String[1]=文件地址
* @param zipName
* @return
* @throws BizException
*/
public static UploadResultInfo zipByCatalog(Map<String, List<String[]>> catalogFiles, String zipName)
throws BizException {
LOGGER.info("【压缩】压缩文件.<START>");
if (MapUtils.isEmpty(catalogFiles)) {
return null;
}
zipName = FileUtils.formatName(zipName);
String tempZipName = zipName + "_" + UUIDUitl.generateString(12);
String parentPath = FILE_LOCAL_PATH + tempZipName;
FileUtils.isDir(parentPath);
for (String catalog : catalogFiles.keySet()) {
String downloadPath;
// 检查临时文件夹是否存在,不存在就创建
if (!StringUtil.isEmpty(catalog)) {
String catalogFolderPath = parentPath + "/" + catalog;
FileUtils.isDir(catalogFolderPath);
downloadPath = catalogFolderPath;
} else {
downloadPath = parentPath;
}
List<String[]> fileUrlList = catalogFiles.get(catalog);
// 下载文件到文件夹中
int idx = 1;
for (String[] files : fileUrlList) {
String fileType = FileUtils.getFileType(files[1]);
String fileName = FileUtils.formatName(files[0]);
String downloadLocalPath = downloadPath + "/" + fileName + "_" + idx + "." + fileType;
FileUtils.downloadFileFromUrl(files[1], downloadLocalPath);
idx++;
}
}
// 检查压缩包临时文件夹是否存在,不存在就创建
FileUtils.isDir(ZIP_FILE_PATH);
String zipFilePath = ZIP_FILE_PATH + tempZipName + ".zip";
try {
CompressUtils.zip(parentPath, zipFilePath);
} catch (Exception e) {
LOGGER.error("【压缩】压缩失败,<ERROR>:" + e.getMessage(), e);
throw new FileException(FileException.ZIP_ERROR, "压缩失败!");
}
// 上传文件到服务器中
UploadResultInfo uploadResultInfo = OssUtils.uploadLocalFile4CustomName(zipFilePath, zipName);
// 删除产生的文件
FileUtils.deleteDirectory(parentPath);
FileUtils.deleteFile(zipFilePath);
LOGGER.info("【压缩】压缩文件.<END>");
return uploadResultInfo;
}
/**
* @Desc 解压缩文件
* @param zip或者rar文件路径
* @param 解压目录
* @throws Exception
*/
public static void deCompress(String sourceFile, String destDir) throws Exception {
// 保证文件夹路径最后是"/"或者"\"
char lastChar = destDir.charAt(destDir.length() - 1);
if (lastChar != '/' && lastChar != '\\') {
destDir += File.separator;
}
// 根据类型,进行相应的解压缩
String type = sourceFile.substring(sourceFile.lastIndexOf(".") + 1).toLowerCase();
if (type.equals("zip")) {
unzip(sourceFile, destDir);
} else if (type.equals("rar")) {
unrar(sourceFile, destDir);
} else {
throw new Exception("只支持zip和rar格式的压缩包!");
}
}
/**
* @param zipPath ZIP文件目录
* @param destDir 解压目录
* @throws Exception
* @Desc ZIP文件解压
*/
private static void unzip(String zipPath, String destDir) throws Exception {
try {
Project p = new Project();
Expand e = new Expand();
e.setProject(p);
e.setSrc(new File(zipPath));
e.setOverwrite(false);
e.setDest(new File(destDir));
e.setEncoding("gbk");
e.execute();
} catch (Exception e) {
throw e;
}
}
/**
* @Desc 压缩zip文件 @param inputFilename 待压缩的文件名称或文件夹路径名称 @param zipFilename
* 压缩后的文件完整的路径名称 @throws
*/
public static void zip(String inputFilename, String zipFilename) throws IOException {
File inputFile = new File(inputFilename);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
try {
compress(inputFile, out);
// zip(inputFile, out, "");
} catch (Exception e) {
throw e;
} finally {
out.close();
}
}
/**
* @param rarPath rar路径
* @param destDir 解压到的文件夹
* @throws Exception
* @Desc rar文件解压
*/
public static void unrar(String rarPath, String destDir) throws Exception {
if (!rarPath.toLowerCase().endsWith(".rar")) {
throw new Exception("非rar文件!");
}
File dstDiretory = new File(destDir);
if (!dstDiretory.exists()) {
dstDiretory.mkdirs();
}
Archive a = new Archive(new File(rarPath));
if (a != null) {
FileHeader fh = a.nextFileHeader();
while (fh != null) {
String fileName = fh.getFileNameW().isEmpty() ? fh.getFileNameString() : fh.getFileNameW();
if (fh.isDirectory()) {
File fol = new File(destDir + File.separator + fileName);
fol.mkdirs();
} else {
File out = new File(destDir + File.separator + fileName.trim());
if (!out.exists()) {
if (!out.getParentFile().exists()) {
out.getParentFile().mkdirs();
}
out.createNewFile();
}
FileOutputStream os = new FileOutputStream(out);
a.extractFile(fh, os);
os.close();
}
fh = a.nextFileHeader();
}
a.close();
}
}
/**
* 压缩方法
*
* @param inputFile
* @param out
* @param base
* @throws IOException
*/
@SuppressWarnings("unused")
private static void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
if (!base.equals(""))
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
zip(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
}
/**
* @param sourceFile zip或者rar文件路径
* @param destDir 解压目录
* @throws Exception
* @Desc 解压缩文件
*/
public static void deCompress(String sourceFile, String destDir) throws Exception {
// 保证文件夹路径最后是"/"或者"\"
char lastChar = destDir.charAt(destDir.length() - 1);
if (lastChar != '/' && lastChar != '\\') {
destDir += File.separator;
}
// 根据类型,进行相应的解压缩
String type = sourceFile.substring(sourceFile.lastIndexOf(".") + 1).toLowerCase();
if (type.equals("zip")) {
unzip(sourceFile, destDir);
} else if (type.equals("rar")) {
unrar(sourceFile, destDir);
} else {
throw new Exception("只支持zip和rar格式的压缩包!");
}
}
/**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩,
*
* @param src
* @param zos
* @param baseDir
*/
private static void compress(File src, ZipOutputStream zos) {
compressbyType(src, zos, "", true);
}
/**
* @Desc 压缩zip文件 @param inputFilename 待压缩的文件名称或文件夹路径名称 @param zipFilename
* 压缩后的文件完整的路径名称 @throws
*/
public static void zip(String inputFilename, String zipFilename) throws IOException {
File inputFile = new File(inputFilename);
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFilename));
try {
compress(inputFile, out);
// zip(inputFile, out, "");
} catch (Exception e) {
throw e;
} finally {
out.close();
}
}
/**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩,
*
* @param src
* @param zos
* @param 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是文件夹,调用此方法
compressDir(src, zos, baseDir, isFirst);
/**
* 压缩方法
*
* @param inputFile
* @param out
* @param base
* @throws IOException
*/
@SuppressWarnings("unused")
private static void zip(File inputFile, ZipOutputStream out, String base) throws IOException {
if (inputFile.isDirectory()) {
File[] inputFiles = inputFile.listFiles();
if (!base.equals(""))
out.putNextEntry(new ZipEntry(base + "/"));
base = base.length() == 0 ? "" : base + "/";
for (int i = 0; i < inputFiles.length; i++) {
zip(inputFiles[i], out, base + inputFiles[i].getName());
}
} else {
if (base.length() > 0) {
out.putNextEntry(new ZipEntry(base));
} else {
out.putNextEntry(new ZipEntry(inputFile.getName()));
}
FileInputStream in = new FileInputStream(inputFile);
try {
int c;
byte[] by = new byte[1024];
while ((c = in.read(by)) != -1) {
out.write(by, 0, c);
}
} catch (IOException e) {
throw e;
} finally {
in.close();
}
}
}
}
}
/**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩,
*
* @param src
* @param zos
*/
private static void compress(File src, ZipOutputStream zos) {
compressbyType(src, zos, "", true);
}
/**
* 压缩文件
*/
private static void compressFile(File file, ZipOutputStream zos, String baseDir) {
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];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch (Exception e) {
}
}
/**
* 按照原路径的类型就行压缩。文件路径直接把文件压缩,
*
* @param src
* @param zos
* @param 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是文件夹,调用此方法
compressDir(src, zos, baseDir, isFirst);
/**
* 压缩文件夹
*/
private static void compressDir(File dir, ZipOutputStream zos, String baseDir, boolean isFirst) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
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();
}
}
/**
* 压缩文件
*/
private static void compressFile(File file, ZipOutputStream zos, String baseDir) {
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];
while ((count = bis.read(buf)) != -1) {
zos.write(buf, 0, count);
}
bis.close();
} catch (Exception e) {
}
}
/**
* 压缩文件夹
*/
private static void compressDir(File dir, ZipOutputStream zos, String baseDir, boolean isFirst) {
if (!dir.exists())
return;
File[] files = dir.listFiles();
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