| | |
| | | package com.zbooksoft.gdmis.common; |
| | | |
| | | import com.ruili.wcp.configsettting.SystemConfig; |
| | | import org.apache.commons.io.FileUtils; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.*; |
| | | import java.nio.charset.Charset; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.Enumeration; |
| | | import java.util.zip.ZipEntry; |
| | | import java.util.zip.ZipFile; |
| | | import java.util.zip.ZipOutputStream; |
| | | |
| | | /** |
| | |
| | | boolean directory = path.isDirectory(); |
| | | System.out.println(directory); |
| | | } |
| | | |
| | | /** |
| | | * 解压zip文件 |
| | | * |
| | | * @param multipartFile |
| | | */ |
| | | private String unzip(MultipartFile multipartFile, String dataSource) throws Exception { |
| | | try { |
| | | //metaBusActivity |
| | | String fileName = multipartFile.getOriginalFilename(); |
| | | SystemConfig config = SystemConfig.getInstance(); |
| | | Date date = new Date(); |
| | | SimpleDateFormat formatter = new SimpleDateFormat("yyyyMM"); |
| | | SimpleDateFormat formatterYear = new SimpleDateFormat("yyyy"); |
| | | String strDate = formatter.format(date); |
| | | String formatYear = formatterYear.format(date); |
| | | // 待接收 /部门编号/年度/年度月份/ |
| | | String destDir = config.getAttachUploadPath() + "待接收" + File.separator + formatYear + File.separator + strDate + File.separator + dataSource; |
| | | |
| | | String filePath = destDir + File.separator + fileName.substring(0, fileName.indexOf('.')); |
| | | |
| | | File fileFolder = new File(destDir); |
| | | // 存入文件夹 |
| | | if (!fileFolder.exists() && !fileFolder.isDirectory()) { |
| | | fileFolder.mkdirs(); |
| | | } |
| | | |
| | | File targetFile = new File(fileFolder, fileName); |
| | | multipartFile.transferTo(targetFile); |
| | | ZipFile zp = null; |
| | | |
| | | //指定编码,否则压缩包里面不能有中文目录 |
| | | zp = new ZipFile(targetFile, Charset.forName("gbk")); |
| | | //遍历里面的文件及文件夹 |
| | | Enumeration entries = zp.entries(); |
| | | while (entries.hasMoreElements()) { |
| | | ZipEntry entry = (ZipEntry) entries.nextElement(); |
| | | String zipEntryName = entry.getName(); |
| | | InputStream in = zp.getInputStream(entry); |
| | | String outpath = (filePath + File.separator + zipEntryName).replace("/", File.separator); |
| | | //判断路径是否存在,不存在则创建文件路径 |
| | | File file = new File(outpath.substring(0, outpath.lastIndexOf(File.separator))); |
| | | if (!file.exists()) { |
| | | file.mkdirs(); |
| | | } |
| | | //判断文件全路径是否为文件夹,如果是,不需要解压 |
| | | if (new File(outpath).isDirectory()) { |
| | | continue; |
| | | } |
| | | OutputStream out = new FileOutputStream(outpath); |
| | | byte[] bf = new byte[2048]; |
| | | int len; |
| | | while ((len = in.read(bf)) > 0) { |
| | | out.write(bf, 0, len); |
| | | } |
| | | in.close(); |
| | | out.close(); |
| | | } |
| | | zp.close(); |
| | | return filePath; |
| | | } catch (Exception e) { |
| | | throw new Exception("解压失败"); |
| | | } |
| | | } |
| | | } |