200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > java 上传zip压缩文件并且解压

java 上传zip压缩文件并且解压

时间:2024-07-14 04:44:29

相关推荐

java 上传zip压缩文件并且解压

public void parseAndAdd(MultipartFile zipFile) {//C:\Users\登录用户~1\AppData\Local\Temp\String pathName = System.getProperty("java.io.tmpdir") + "shpFileCache/";String dec = System.getProperty("java.io.tmpdir") + "shpFileCache/";File file = new File(pathName);//如果文件夹不存在 创建文件夹if (!file.exists()) {file.mkdir();}//获取文件名(包括后缀)String pname = zipFile.getOriginalFilename();pathName = pathName + UUID.randomUUID().toString().replaceAll("-", "") + "-" + pname;try {File dest = new File(pathName);zipFile.transferTo(dest);// 获取解压出来的文件名 不带后缀 List<String> fileNames = ZipUtil.unZip(dest, dec);//解析完成 删除本次解析中生成的文件 删除此目录下的所有文件//ZipUtil.deleteFile(dec);} catch (Exception e) {e.printStackTrace();}}

ZipUtil

import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.nio.charset.Charset;import java.util.ArrayList;import java.util.Enumeration;import java.util.List;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;/*** @author lww* @Description 文件解压相关工具类* @date /7/22 11:19*/public class ZipUtil {/*** zip解压** @param srcFilezip源文件* @param destDirPath 解压后的目标文件夹* @throws RuntimeException 解压失败会抛出运行时异常*/public static List<String> unZip(File srcFile, String destDirPath) throws RuntimeException {//记录解压出来的所有文件名List<String> filesName = new ArrayList<>();long start = System.currentTimeMillis();// 判断源文件是否存在if (!srcFile.exists()) {throw new RuntimeException(srcFile.getPath() + "所指文件不存在");}// 开始解压ZipFile zipFile = null;try {zipFile = new ZipFile(srcFile, Charset.forName("GBK"));Enumeration<?> entries = zipFile.entries();while (entries.hasMoreElements()) {ZipEntry entry = (ZipEntry) entries.nextElement();//添加进filesNamefilesName.add(entry.getName());System.out.println("解压文件:" + entry.getName());// 如果是文件夹,就创建个文件夹if (entry.isDirectory()) {String dirPath = destDirPath + "/" + entry.getName();File dir = new File(dirPath);dir.mkdirs();} else {// 如果是文件,就先创建一个文件,然后用io流把内容copy过去File targetFile = new File(destDirPath + "/" + entry.getName());// 保证这个文件的父文件夹必须要存在if (!targetFile.getParentFile().exists()) {targetFile.getParentFile().mkdirs();}targetFile.createNewFile();// 将压缩文件内容写入到这个文件中InputStream is = zipFile.getInputStream(entry);FileOutputStream fos = new FileOutputStream(targetFile);int len;byte[] buf = new byte[1024];while ((len = is.read(buf)) != -1) {fos.write(buf, 0, len);}// 关流顺序,先打开的后关闭fos.close();is.close();}}long end = System.currentTimeMillis();System.out.println("解压完成,耗时:" + (end - start) + " ms");} catch (Exception e) {throw new RuntimeException("unzip error from ZipUtils", e);} finally {if (zipFile != null) {try {zipFile.close();} catch (IOException e) {e.printStackTrace();}}}return filesName;}/*** 删除文件** @param filePath* @return*/public static boolean deleteFile(String filePath) {boolean flag = false;File file = new File(filePath);if (!file.exists()) {return flag;}if (!file.isDirectory()) {return flag;}String[] tempList = file.list();File temp;for (int i = 0; i < tempList.length; i++) {if (filePath.endsWith(File.separator)) {temp = new File(filePath + tempList[i]);} else {temp = new File(filePath + File.separator + tempList[i]);}if (temp.isFile()) {temp.delete();}if (temp.isDirectory()) {// 先删除文件夹里面的文件deleteFile(filePath + "/" + tempList[i]);// 再删除空文件夹deleteFile(filePath + "/" + tempList[i]);flag = true;}}return flag;}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。