文件下载、解压zip文件、移动文件夹、删除文件

news/2024/10/25 10:33:31/

由于IO流不熟悉带来的灾难,导致用的时候不知道用哪个流,必须记一下

1、文件下载

    /**** @param downloadPath 下载地址* @param fileName 保存文件名* @param savePath 保存路径* @return*/private  void download(String downloadPath,String fileName,String savePath) {System.out.println("正在下载"+fileName+"===============================");try {URL url = new URL(downloadPath);// 建立连接对象HttpURLConnection connection = (HttpURLConnection) url.openConnection();// 设置 User-Agent 避免被拦截connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)");InputStream inputStream = connection.getInputStream();BufferedInputStream bins = new BufferedInputStream(inputStream);File file = new File(savePath);if (!file.exists()) {file.mkdirs();}File res = new File(file + File.separator + fileName);OutputStream outputStream = new FileOutputStream(res);BufferedOutputStream bouts = new BufferedOutputStream(outputStream);int bytesRead = 0;byte[] buffer = new byte[2048];while ((bytesRead = bins.read(buffer,0,2048)) != -1) {bouts.write(buffer,0,bytesRead);}bouts.flush();bouts.close();bouts.close();inputStream.close();outputStream.close();log.info(fileName + "文件下载成功!");} catch (Exception e) {e.printStackTrace();throw new BusinessException(ResultCodeEnum.ERROR.getCode(),fileName + "文件下载失败!");}}

2、解压下载的压缩包zip文件

    /*** 将指定的zip文件解压到指定目录下* @param filePath 压缩包所在路径* @param zipFileName 压缩包名字* @param targetDirName 解压路径* @throws IOException*/public void upzipFile(String filePath,String zipFileName, String targetDirName) {String zipName = filePath+File.separator+zipFileName+".zip";System.out.println("文件:{"+zipName+"}. 解压路径:{"+targetDirName+"}. 解压开始.");long start = System.currentTimeMillis();if (!targetDirName.endsWith(File.separator)) {targetDirName += File.separator;}try {// 根据zip文件创建ZipFile对象,此类的作用是从zip文件读取条目
//            ZipFile zip = new ZipFile(zipFile,Charset.forName("GBK"));//解决中文文件夹乱码ZipFile zipFile = new ZipFile(zipName, Charset.forName("GBK"));ZipEntry zn = null;String entryName = null;String targetFileName = null;byte[] buffer = new byte[4096];int bytes_read;Enumeration entrys = zipFile.entries();         // 获取ZIP文件里所有的文件条目的名字while (entrys.hasMoreElements()) {              // 循环遍历所有的文件条目的名字zn = (ZipEntry) entrys.nextElement();entryName = zn.getName();               // 获得每一条文件的名字targetFileName =  targetDirName + zipFileName + File.separator + entryName;if (zn.isDirectory()) {new File(targetFileName).mkdirs();      // 如果zn是一个目录,则创建目录continue;} else {new File(targetFileName).getParentFile().mkdirs();// 如果zn是文件,则创建父目录}File targetFile = new File(targetFileName); // 否则创建文件System.err.println("正在创建文件:" + targetFile.getAbsolutePath());FileOutputStream os = new FileOutputStream(targetFile);// 打开文件输出流InputStream is = zipFile.getInputStream(zn);    // 从ZipFile对象中打开entry的输入流while ((bytes_read = is.read(buffer)) != -1) {os.write(buffer, 0, bytes_read);}os.close();                             // 关闭流is.close();}System.out.println("文件:{"+zipName+"}. 解压路径:{"+targetDirName+"}. 解压完成.耗时:{"+(System.currentTimeMillis()-start)+"}ms.");} catch (IOException err) {System.out.println("文件:{"+zipName+"}. 解压路径:{"+targetDirName+"}. 解压结束.耗时:{"+(System.currentTimeMillis()-start)+"}ms.");throw new BusinessException(ResultCodeEnum.ERROR.getCode(),"解压失败!");}}

3、复制移动整个文件夹

    /*** 复制整个文件夹内容到目录** @param oldPath String 原文件路径* @param newPath String 复制后路径*/public void copyFolder(String oldPath, String newPath) {try {(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹File a = new File(oldPath);String[] file = a.list();File temp = null;for (int i = 0; i < file.length; i++) {if (oldPath.endsWith(File.separator)) {temp = new File(oldPath + file[i]);} else {temp = new File(oldPath + File.separator + file[i]);}if (temp.isFile()) {FileInputStream ins = new FileInputStream(temp);BufferedInputStream bins = new BufferedInputStream(ins);FileOutputStream outs = new FileOutputStream(newPath + File.separator +(temp.getName()).toString());BufferedOutputStream bouts = new BufferedOutputStream(outs);int bytesRead = 0;byte[] buffer = new byte[8192];// 开始向网络传输文件流while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {bouts.write(buffer, 0, bytesRead);}bouts.flush();// 刷新管道ins.close();bins.close();outs.close();bouts.close();}if (temp.isDirectory()) {//如果是子文件夹copyFolder(oldPath + File.separator + file[i], newPath + File.separator + file[i]);}}} catch (Exception e) {System.out.println("复制整个文件夹内容操作出错!");e.printStackTrace();throw new BusinessException(ResultCodeEnum.ERROR);}}

4、删除文件

    /*** 删除文件夹的内容* @param file*/private void deleteFile(File file) {/*** File[] listFiles()*  返回一个抽象路径名数组,这些路径名表示此抽象路径名表示的目录中的文件。*/File[] files = file.listFiles();if (files!=null){//如果包含文件进行删除操作for (int i = 0; i <files.length ; i++) {if (files[i].isFile()){//删除子文件files[i].delete();}else if (files[i].isDirectory()){//通过递归的方法找到子目录的文件deleteFile(files[i]);}files[i].delete();//删除子目录}}}/*** 删除单个文件* @param filePath 文件路径*/private void deleteFile(String filePath) {File deleteFile = new File(filePath);deleteFile.delete();}

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

相关文章

好压 自解压文件解压完成后创建桌面快捷方式并启动

首先右键点击想要压缩的目标文件夹&#xff0c;右键点击“添加到压缩文件”&#xff0c;在弹出的窗口中选择7z格式&#xff0c;然后勾选创建自解压格式&#xff0c;然后点击自解压选项。 进入自解压选项后&#xff0c;点击解压标签&#xff0c;在解压后运行那一栏输入运行路径&…

Android压缩包下载解压

使用说明&#xff1a;在需要处理大文件下载&#xff0c;为了节省流量就在后台做成了压缩包&#xff0c;app端联网下载到本地解压使用&#xff1b; 使用步骤&#xff1a; 1 联网下载 在下使用的是okGo&#xff0c;此框架比Retrufit还简单&#xff0c;为什么不用呢。 okGo下载文…

压缩包密码忘记了,如何解压文件?

想要保护文件的内容&#xff0c;我们可以通过对文件进行压缩并且设置密码来完成&#xff0c;因为加密之后的压缩包&#xff0c;需要输入压缩包密码才能顺利解压文件出来。当别人不知道密码的情况下&#xff0c;是无法解压出压缩包的文件的。 温馨提示给大家&#xff1a;给压缩包…

JavaScript在线解压 ZIP 文件 JavaScript 怎样在线解压 ZIP,jszip实现解压压缩包,并下载压缩包内文件

JSzip解压压缩包、下载压缩包内文件 废话不多说&#xff0c;直接上代码 1、所需物料准备 1、jszip.min.js 仓库地址&#xff1a;https://github.com/Stuk/jszip CDN地址&#xff1a;https://cdn.bootcdn.net/ajax/libs/…

MySQL压缩包下载及解压安装

windows 64位操作系统解压版mysql8.x的下载及安装配置及修改初始密码 MySql 8.X(压缩包&#xff09;安装与配置 1&#xff0c;下载压缩包 官网下载压缩包&#xff0c;https://dev.mysql.com/downloads/mysql/,如果需要可执行安装&#xff0c;亦可转到下载&#xff1a;https:…

.tar.lz压缩包解压

压缩包&#xff1a;gmp-6.1.2tar.lz 尝试1&#xff1a; lzip -d gmp-6.1.2.tar.lz 结果&#xff1a; The program ‘lzip’ can be found in the following packages: * clzip * lunzip * lzd * lzip * lziprecover * minilzip * pdlzip * plzip Try: sudo apt in…

erphpdown9.64插件加Modown1.9模板主题下载

Modown是模板兔基于Erphpdown wordpress下载插件开发的一款全新的针对收费付费下载资源的WordPress主题&#xff0c;一款为erphpdown而生的wp主题。此主题下载包里不包含erphpdown插件&#xff0c;主题无域名限制。 此主题需要Erphpdown 9.2版本以上&#xff01;模板只针对付费…

全网首发Modown主题8.31开心版

主题特点&#xff1a; 自适应响应式设计&#xff0c;兼容主流浏览器 网格样式、瀑布流样式、博客样式任意切换 内置SEO优化 自带与主题UI完美兼容搭配的erphpdown前端用户中心页面 收费付费下载资源、付费阅读内容、付费观看视频、付费下载音乐&#xff0c;上传文件兼容第三方云…