解压zip文件并按文件名进行分类(.mp4文件)
文件结构及名字
controller.unzip
package com.lp.test.controller;
import com.lp.test.utils.UploadUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
@Api("压缩文件")
@Controller
@RequestMapping(value = "/user")
public class Unzip {
@PostMapping(value = "/unzip")
@ApiOperation(value = "压缩文件")
@ConfigurationProperties("application.properties")
public void msave(@ApiParam(value = "压缩zip文件") MultipartFile jarFile, HttpServletRequest request) {
String path = "E:\\upload\\录屏\\";
// 解压
try {
UploadUtil.resolveCompressUploadFile(request, jarFile, path);
} catch (Exception e) {
e.printStackTrace();
}
}
}
utils.CompressFileUtils
package com.lp.test.utils;
import com.lp.test.controller.UpLoad;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
import java.io.*;
import java.util.Enumeration;
public class CompressFileUtils {
/**
* 解压zip文件
*
* @param sourceFile ,待解压的zip文件; toFolder,解压后的存放路径
* @throws Exception
**/
public static void zipToFile(String sourceFile, String toFolder) throws Exception {
ZipFile zfile = new ZipFile(sourceFile, "GBK");// 连接待解压文件
Enumeration zList = zfile.getEntries();// 得到zip包里的所有元素
ZipEntry ze = null;
byte[] buf = new byte[1073741824];
while (zList.hasMoreElements()) {
ze = (ZipEntry) zList.nextElement();
String name = ze.getName();
UpLoad upLoad = new UpLoad();
String mdir = upLoad.mdir(name);// 接收解压后的存放路径
if (ze.isDirectory()) {
// log.info("打开zip文件里的文件夹:" + ze.getName() + "skipped...");
continue;
}
OutputStream outputStream = null;
InputStream inputStream = null;
try {
// 以ZipEntry为参数得到一个InputStream,并写到OutputStream中
outputStream = new BufferedOutputStream(new FileOutputStream(
getRealFileName(mdir, ze.getName())));
inputStream = new BufferedInputStream(zfile.getInputStream(ze));
int readLen = 0;
while ((readLen = inputStream.read(buf, 0, 1024)) != -1) {
outputStream.write(buf, 0, readLen);
}
inputStream.close();
outputStream.close();
} catch (Exception e) {
// log.info("解压失败:" + e.toString());
throw new IOException("解压失败:" + e.toString());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ex) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
inputStream = null;
outputStream = null;
}
}
zfile.close();
}
/**
* 给定根目录,返回一个相对路径所对应的实际文件名.
*
* @param zippath 指定根目录
* @param absFileName 相对路径名,来自于ZipEntry中的name
* @return java.io.File 实际的文件
*/
private static File getRealFileName(String zippath, String absFileName) {
// log.info("文件名:" + absFileName);
String[] dirs = absFileName.split("/", absFileName.length());
File ret = new File(zippath);// 创建文件对象
if (dirs.length > 1) {
for (int i = 0; i < dirs.length - 1; i++) {
ret = new File(ret, dirs[i]);
}
}
if (!ret.exists()) {// 检测文件是否存在
ret.mkdirs();// 创建此抽象路径名指定的目录
}
ret = new File(ret, dirs[dirs.length - 1]);// 根据 ret 抽象路径名和 child
// 路径名字符串创建一个新 File 实例
return ret;
}
}
utils.UploadUtil
package com.lp.test.utils;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
public class UploadUtil {
public static File[] resolveCompressUploadFile(HttpServletRequest request, MultipartFile file, String path) throws Exception {
request.setCharacterEncoding("utf-8");
/* 截取后缀名 */
if (file.isEmpty()) {
throw new Exception("文件不能为空");
}
// String fileName = file.getOriginalFilename();
System.out.println("sssss" + file.getOriginalFilename());
String fileName = file.getOriginalFilename();
System.out.println("fileName" + fileName);
int pos = fileName.lastIndexOf(".");
String extName = fileName.substring(pos + 1).toLowerCase();
// 判断上传文件必须是zip或者是rar否则不允许上传
if (!extName.equals("zip") && !extName.equals("rar")) {
throw new Exception("上传文件格式错误,请重新上传");
}
// 时间加后缀名保存
String saveName = "aaas" + "." + extName;
// 根据服务器的文件保存地址和原文件名创建目录文件全路径
File pushFile = new File(path + "/" + saveName);
File descFile = new File(path);
if (!descFile.exists()) {
descFile.mkdirs();
}
// 解压目的文件
String descDir = path;
System.out.println("desc:" + descDir);
file.transferTo(pushFile);
// 开始解压zip
if (extName.equals("zip")) {
CompressFileUtils.zipToFile(path + saveName, descDir);
} else {
throw new Exception("文件格式不正确不能解压");
}
// 删除 解压完的压缩文件
doDeleteEmptyDir(path + saveName);
return returnFiles(descDir + "");
}
//返回 File[]
public static File[] returnFiles(String strPath) {
File dir = new File(strPath);
File[] files = dir.listFiles(); // 该文件目录下文件全部放入数组
if (files != null) {
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
File f = new File(strPath + File.separator + fileName);
System.out.println("文件名称:" + fileName);
if (f.isDirectory()) {// 如果f是目录
File[] newfiles = f.listFiles();
return newfiles;
}
}
}
return files;
}
//删除指定文件
private static void doDeleteEmptyDir(String dir) {
boolean success = (new File(dir)).delete();
if (success) {
System.out.println("Successfully deleted empty directory: " + dir);
} else {
System.out.println("Failed to delete empty directory: " + dir);
}
}
// 删除目录下的所有
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
//递归删除目录中的子目录下
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
}
utils.Classify 分类
package com.lp.test.utils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
public class Classify {
public String mdir(MultipartFile file) {
String[] split = file.getOriginalFilename().split("-");
String[] split1 = split[1].split(".mp4");
File tmpFile1 = new File("E:\\upload\\" + split[0]);
File tmpFile2 = new File(tmpFile1 + "\\" + split1[0]);
//判断是否存在该文件夹,若不存在则创建文件夹
if (!tmpFile1.exists()) {
tmpFile1.mkdir();
}
if (!tmpFile2.exists()) {
tmpFile2.mkdir();
}
String url = tmpFile2 + "\\";
return url;
}
public String mdir(String file) {
String[] split = file.split("-");
String[] split1 = split[1].split("\\.",4);
File tmpFile1 = new File("E:\\upload\\录屏\\" + split[0]);
File tmpFile2=new File(tmpFile1+"\\"+split1[0]+"."+split1[1]+"."+split1[2]);
//判断是否存在该文件夹,若不存在则创建文件夹
if (!tmpFile1.exists()) {
tmpFile1.mkdir();
}
if (!tmpFile2.exists()) {
tmpFile2.mkdir();
}
return tmpFile2 + "\\";
}
}