zip文件解压并增加到桌面

news/2024/10/25 12:25:59/

zip文件解压

/*** 解压zip** @param zipFile* @param descDir* @throws Exception*/@RequestMapping("/ff")@ResponseBodypublic  AjaxResult unZipFiles(MultipartFile fil, String descDir) throws Exception {System.out.println("******************解压开始********************");File zipFile = null;if(fil.equals("") ||fil.getSize()<=0){fil = null;}else {InputStream ins = null;ins = fil.getInputStream();zipFile = new File(fil.getOriginalFilename());inputStreamToFile(ins, zipFile);ins.close();}descDir = Global.getAvatarPath()+DateUtils.datePath() ;File pathFile = new File(descDir);if (!pathFile.exists()) {pathFile.mkdirs();}@SuppressWarnings("resource")ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));for (Enumeration<?> entries = zip.entries(); entries.hasMoreElements();) {ZipEntry entry = (ZipEntry) entries.nextElement();String zipEntryName = entry.getName();InputStream in = zip.getInputStream(entry);String outPath = (descDir + "/" + zipEntryName).replaceAll("\\*", "/");// 判断路径是否存在,不存在则创建文件路径File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));if (!file.exists()) {file.mkdirs();}// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压if (new File(outPath).isDirectory()) {continue;}OutputStream out = new FileOutputStream(outPath);byte[] buf1 = new byte[1024];int len;while ((len = in.read(buf1)) > 0) {out.write(buf1, 0, len);}if ((zipEntryName.trim().lastIndexOf("/")) == -1) {}in.close();out.close();}System.out.println("******************解压完毕********************");System.out.println("******************遍历文件夹********************");String parent_id = "";traverse(descDir, parent_id);return success();}public  void inputStreamToFile(InputStream ins, File file) {try {OutputStream os = new FileOutputStream(file);int bytesRead = 0;byte[] buffer = new byte[8192];while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {os.write(buffer, 0, bytesRead);}os.close();ins.close();} catch (Exception e) {e.printStackTrace();}}/*** 文件夹遍历* * @param path* @throws Exception*/public  void traverse(String path, String parent_id) throws Exception {System.out.println("path---->" + path);File file = new File(path);if (file.exists()) {File[] files = file.listFiles();if (files.length == 0) {System.out.println("文件夹是空的!");return;} else {String k_id = "aaa";for (File file2 : files) {if (file2.isDirectory()) {// 文件夹traverse(file2.getAbsolutePath(), parent_id);parent_id = k_id;} else if (file2.isFile()) {// 文件if (file2.getName().endsWith(".xls")||file2.getName().endsWith(".xlsx")) {ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);List<SysUser> userList = util.importExcel(new FileInputStream(file2));List<SysUser> users = new ArrayList<>();for (SysUser sysUser : userList) {if (sysUser.getRecordPicture()!=null) {sysUser.setRecordPicture(DateUtils.datePath()+"/"+sysUser.getRecordPicture());users.add(sysUser);}}String operName = ShiroUtils.getSysUser().getLoginName();userService.importUser(users, true, operName);FileUtils.deleteFile(file2.getCanonicalPath());}}}}} else {System.out.println("文件不存在!");}}

将目标文件压缩为zip文件下载到桌面

1.将文件及图片打包压缩文件zip文件

@RequestMapping("/download")@ResponseBodypublic  void download(HttpServletRequest request, HttpServletResponse response) throws Exception{response.setCharacterEncoding("UTF-8");//获得要下载的文件名String fileName = "导入用户示例.xlsx";String fileName2 = "张1.jpg";String fileSaveRootPath = "D:\\Work\\fw\\profile\\avatar\\示例\\";//得到要下载的文件File file = new File(fileSaveRootPath, fileName);File file2 = new File(fileSaveRootPath, fileName2);System.out.println("Excel文件保存路径1:" + fileSaveRootPath + fileName);System.out.println("Excel文件保存路径2:" + fileSaveRootPath + fileName2);//如果文件不存在if (!file.exists() ) {request.setAttribute("message", "您要下载的资源已被删除!!");request.getRequestDispatcher("/message.jsp").forward(request, response);return ;}//先压缩String zipName = "导入用户示例.zip";//获取当前用户桌面File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory();String desktopPath = desktopDir.getAbsolutePath();String zipPath = desktopPath+"\\" + zipName;ZipOutputStream zipOutput = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipPath)));ZipEntry zEntry = new ZipEntry(file.getName());zipOutput.putNextEntry(zEntry);BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));byte[] buffer = new byte[1024];int read = 0;while((read = bis.read(buffer)) != -1){zipOutput.write(buffer, 0, read);}zEntry = new ZipEntry(file2.getName());zipOutput.putNextEntry(zEntry);bis = new BufferedInputStream(new FileInputStream(file2));while((read = bis.read(buffer)) != -1){zipOutput.write(buffer, 0, read);}bis.close();zipOutput.close();//创建输出流,下载ziptry(OutputStream out = response.getOutputStream();FileInputStream in = new FileInputStream(new File(zipPath));){//设置响应头,控制浏览器下载该文件response.setHeader("Content-Type","application/octet-stream");response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode(zipName, "UTF-8"));while((read = in.read(buffer)) != -1){out.write(buffer, 0, read);}System.out.println("zip下载路径:"+zipPath);}finally {try {/*//删除压缩包File zfile = new File(zipPath);zfile.delete();*/}catch (Exception e){e.printStackTrace();}}}

2.将zip实例文件放到工程中通过浏览器直接下载

@RequestMapping("/download")
@ResponseBody
public  String download(HttpServletResponse response) throws Exception{response.setCharacterEncoding("UTF-8");String url = "http://localhost:8080/user.zip";Runtime runtime = Runtime.getRuntime();try {runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);} catch (IOException e) {return null;}return "";
}

http://www.ppmy.cn/news/319188.html

相关文章

UnityWWW下载压缩包并解压到桌面文件夹中

https://download.csdn.net/download/qq_30928175/10877644工程下载 using ICSharpCode.SharpZipLib.Zip; using Microsoft.Win32; using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; using UnityEngine.UI;pu…

【解压zip.00**压缩包】

【解压zip.00**压缩包】 1 解压 .zip.001 .zip.002 .zip.0031.1 方法一【网上的方法&#xff0c;亲测未成功】1.2 方法二【win11系统下&#xff0c;亲测成功】 2 解压 ***.part1.rar3 解压****.tar.gz.0 ****.tar.gz.&#xff11; ****.tar.gz.3 … 1 解压 .zip.001 .zip.002 …

7z制作自解压安装包(转载)

像7z和winRAR这样的压缩工具都支持制作自解压的文件。所谓自解压的文件就是不需要目标机器上安装解压工具&#xff0c;通过运行压缩包自己即可解压出压缩包中的文件。下面我们就介绍一下如何利用7z的自解压功能制作应用程序安装包。 熟悉应用程序安装的朋友应该清楚&#xff0…

第一章 OAuth2.0规范(史上最详细解释)——介绍

目录 一、简介 二、角色 二、协议流程 三、授权许可 1、授权码 2、隐式许可 3、资源所有者密码凭据 4、客户端凭据 四、访问令牌 五、刷新令牌 六、TLS版本 七、HTTP重定向 八、互操作性 九、符号约定 一、简介 在传统的客户端-服务器身份验证模式中&#xff0c;客…

什么是响应式设计

也叫响应式布局&#xff0c;响应式开发 实现不同屏幕分辨率的终端上浏览网页的不同展示方式。 响应式布局是根据设备屏幕宽度不同适当调整标签显示的布局&#xff0c;从而在每种设备屏幕宽度下呈现的界面是不同的 响应式布局 原理&#xff1a; 一个网站适配所有终端&#x…

【Redis总结-基础入门】

第一章 Redis基础 Redis总结-基础入门 第一章 Redis基础学习目标&#xff1a;1. Redis 简介1.1 NoSQL概念1.1.1 问题现象1.1.2 NoSQL的概念 1.2 Redis概念1.2.1 redis概念1.2.2 redis的应用场景 1.3 Redis 的下载与安装1.3.1 Redis 的下载与安装 1.4 Redis服务器启动1.4.1 Redi…

电脑常用快捷键大全(含Visual Studio快捷键操作)

目录 一、系统快捷键 二、常用系统命令 三、QQ快捷键 四、IE浏览器快捷键使用大全 五、Word 中的快捷键 六、Excel 中的快捷键 七、PowerPoint 中的快捷键 八、Visual Studio 九、Eclipse 十、Photoshop 十一、批处理指令 十二、安装操作系统 &#xff08;此快捷操作对我们程…

python爬虫用什么电脑好_【Python】【爬虫】最近想买电脑,用Python爬取京东评论做个参考...

最近想换电脑&#xff0c;又有点不确定买哪一款。所以决定爬取京东上电脑评论&#xff0c;做个参考&#xff0c;并把最终结果绘制成词云图。 一、先来看下三款电脑评价的词云图 1 用Python爬取京东自营ThinkPad T490s的评论绘制成词云图 2 用Python爬取京东自营MacBook Pro的…