+ * ClassName: ZipFile + *
+ *+ * Description: 压缩&解压文件 + *
+ *+ * Author: wangkang + *
+ *+ * Date: 2014年1月4日 + *
+ */ +public class ZipUtil { + + /** + *+ * Field byteSize: 字节数组长度 + *
+ */ + private static final int BYTE_SIZE = 1024; + + /** + *+ * Field encoding: 编码格式 + *
+ */ + private String encoding; + + /** + *+ * Description: 构造方法 + *
+ * + * @param encoding 编码 + */ + public ZipUtil(String encoding) { + this.encoding = encoding; + } + + /** + *+ * Description: 把zip文件解压到指定的文件夹 + *
+ * + * @param zipFilePath zip文件路径 + * @param saveFileDir 解压后的文件存放路径 + * @throws IOException 异常 + */ + public void decompressZip(String zipFilePath, String saveFileDir) throws IOException { + File file = new File(zipFilePath); + if (file.exists()) { + this.decompressZip(file, saveFileDir); + } else { + throw new IOException("文件不存在"); + } + } + + /** + *+ * Description: 解压 + *
+ * + * @param file 压缩包文件 + * @param saveFileDir 保存路径 + * @throws IOException 异常 + */ + private void decompressZip(File file, String saveFileDir) throws IOException { + InputStream is = null; + ZipArchiveInputStream zais = null; + try { //NOSONAR + is = new FileInputStream(file); + zais = new ZipArchiveInputStream(is, this.encoding); + ArchiveEntry archiveEntry; + + // 把zip包中的每个文件读取出来 + // 然后把文件写到指定的文件夹 + while ((archiveEntry = zais.getNextEntry()) != null) { + + // 如果是目录,则自动创建 + if (archiveEntry.isDirectory()) { + File f = new File(saveFileDir + archiveEntry.getName()); + boolean rs = f.mkdir(); + if (rs) { + continue; + } else { + throw new IOException("目录创建失败"); + } + } + + // 构造解压出来的文件存放路径 + this.decompressZip(saveFileDir, archiveEntry, zais); + + } + } finally { + if (zais != null) { + zais.close(); + } + if (is != null) { + is.close(); + } + } + } + + /** + *+ * Description: 解压 + *
+ * + * @param saveFileDir 存储路径 + * @param archiveEntry 压缩相 + * @param zais 压缩流 + * @throws IOException 异常 + */ + private void decompressZip(String saveFileDir, ArchiveEntry archiveEntry, ZipArchiveInputStream zais) + throws IOException { + String entryFilePath = saveFileDir + archiveEntry.getName(); + byte[] content = new byte[BYTE_SIZE]; + OutputStream os = null; + try { //NOSONAR + // 把解压出来的文件写到指定路径 + File entryFile = new File(entryFilePath); + os = new FileOutputStream(entryFile); + int r; + while ((r = zais.read(content)) > 0) { + os.write(content, 0, r); + } + os.flush(); + } finally { + if (os != null) { + os.close(); + } + } + } + + /** + *+ * Description: 压缩文件 + *
+ * + * @param files 需要压缩的文件 + * @param zipFilePath zip文件 + * @throws IOException 异常 + */ + public void compressFiles2Zip(File[] files, String zipFilePath) throws IOException { + if (files != null && files.length > 0) { + ZipArchiveOutputStream zaos = null; + try { //NOSONAR + File zipFile = new File(zipFilePath); + zaos = new ZipArchiveOutputStream(zipFile); + + // 将每个文件用ZipArchiveEntry封装 + // 再用ZipArchiveOutputStream写到压缩文件中 + for (File file : files) { + this.compressFiles2Zip(file, zaos); + } + zaos.finish(); + } finally { + if (zaos != null) { + zaos.close(); + } + } + + } + + } + + /** + *+ * Description: 压缩 + *
+ * + * @param file 文件 + * @param zaos 压缩流 + * @throws IOException 异常 + */ + private void compressFiles2Zip(File file, ZipArchiveOutputStream zaos) throws IOException { + final int ioBuffered = 1024; + if (file != null) { + ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName()); + zaos.putArchiveEntry(zipArchiveEntry); + InputStream is = null; + try { //NOSONAR + is = new BufferedInputStream(new FileInputStream(file), ioBuffered); + byte[] buffer = new byte[BYTE_SIZE]; + int len; + while ((len = is.read(buffer)) != -1) { + zaos.write(buffer, 0, len); + } + zaos.closeArchiveEntry(); + } finally { + if (is != null) { + is.close(); + } + } + } + } +} diff --git a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessAspect.java b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessAspect.java index 375c931..96ae493 100644 --- a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessAspect.java +++ b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessAspect.java @@ -11,7 +11,9 @@ import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.itkk.udf.cache.redis.CacheRedisProperties; +import org.itkk.udf.core.ApplicationConfig; import org.itkk.udf.core.exception.SystemRuntimeException; +import org.itkk.udf.core.utils.ZipUtil; import org.itkk.udf.file.aliyun.oss.api.OssWarpper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.core.annotation.Order; @@ -19,7 +21,9 @@ import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Component; import java.io.File; +import java.io.IOException; import java.nio.file.Files; +import java.util.Date; import java.util.concurrent.TimeUnit; /** @@ -41,7 +45,7 @@ public class DownLoadProcessAspect { /** * DOWNLOAD_WAITING_CACHE_EXPIRATION(待执行,执行中,缓存过期时间为1天) */ - public static final long DOWNLOAD_WAITING_CACHE_EXPIRATION = 60 * 24; + public static final long DOWNLOAD_WAITING_CACHE_EXPIRATION = 60L * 24L; /** * DOWNLOAD_CACHE_EXPIRATION(执行完成,执行错误,缓存过期时间为10分钟) @@ -66,6 +70,12 @@ public class DownLoadProcessAspect { @Autowired private OssWarpper ossWarpper; + /** + * applicationConfig + */ + @Autowired + private ApplicationConfig applicationConfig; + /** * around * @@ -87,29 +97,56 @@ public class DownLoadProcessAspect { //构造对象 DownloadInfo info = new DownloadInfo(); info.setId(param.getId()); + //定义文件变量 + File file = null; + File zipFile = null; + //开始处理 try { //更新缓存 info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_2.value()); + info.setProcessStartDate(new Date()); redisTemplate.opsForValue().set(key, info, DOWNLOAD_WAITING_CACHE_EXPIRATION, TimeUnit.MINUTES); //执行 result = proceedingJoinPoint.proceed(); //获得本地文件 - File file = (File) result; + file = (File) result; + //文件zip压缩 + ZipUtil zipUtil = new ZipUtil(applicationConfig.getEncoding()); + String zipFilePath = DownConstant.tempdir() + param.getId() + ".zip"; + File[] files = {file}; + zipUtil.compressFiles2Zip(files, zipFilePath); + zipFile = new File(zipFilePath); //上传文件到阿里云OSS - String objectKey = ossWarpper.uploadFile(param.getOssCode(), file); - //删除本地文件 - Files.delete(file.toPath()); + String objectKey = ossWarpper.uploadFile(param.getOssCode(), zipFile); //更新缓存 info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_3.value()); + info.setProcessEndDate(new Date()); info.setObjectKey(objectKey); redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES); } catch (Throwable e) { //更新缓存 info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_4.value()); + info.setProcessEndDate(new Date()); info.setErrorMsg(e.getMessage()); redisTemplate.opsForValue().set(key, info, DOWNLOAD_CACHE_EXPIRATION, TimeUnit.MINUTES); //抛出异常 throw new SystemRuntimeException(e); + } finally { + //删除本地文件 + if (file != null) { + try { + Files.delete(file.toPath()); + } catch (IOException e) { + log.error("error:", e); + } + } + if (zipFile != null) { + try { + Files.delete(zipFile.toPath()); + } catch (IOException e) { + log.error("error:", e); + } + } } } //返回 diff --git a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessService.java b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessService.java index 6e3b540..46521c7 100644 --- a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessService.java +++ b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownLoadProcessService.java @@ -6,6 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import java.util.Date; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -46,6 +47,7 @@ public class DownLoadProcessService { DownloadInfo info = new DownloadInfo(); info.setId(id); info.setStatus(DownConstant.DOWNLOAD_PROCESS_STATUS.STATUS_1.value()); + info.setProcessInitDate(new Date()); //更新缓存 redisTemplate.opsForValue().set(key, info, DownLoadProcessAspect.DOWNLOAD_WAITING_CACHE_EXPIRATION, TimeUnit.MINUTES); //返回ID diff --git a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownloadInfo.java b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownloadInfo.java index f40183c..65a3378 100644 --- a/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownloadInfo.java +++ b/udf-starter-file-aliyun-oss-api/src/main/java/org/itkk/udf/file/aliyun/oss/api/download/DownloadInfo.java @@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; +import java.util.Date; + /** * DownloadInfo */ @@ -29,6 +31,24 @@ public class DownloadInfo { @ApiModelProperty(value = "阿里云OSS的objectKey", required = true, dataType = "string") private String objectKey; + /** + * 初始化时间 + */ + @ApiModelProperty(value = "初始化时间", required = true, dataType = "date") + private Date processInitDate; + + /** + * processStartDate + */ + @ApiModelProperty(value = "开始时间", required = true, dataType = "date") + private Date processStartDate; + + /** + * processEndDate + */ + @ApiModelProperty(value = "结束时间", required = true, dataType = "date") + private Date processEndDate; + /** * errorMsg */