JAVA压缩文件、解压文件、删除文件
2016-03-01 19:29
495 查看
主要要用到zip的文件输入输出流、对文件是目录还是非目录的判断,以及对文件的遍历
压缩
解压
删除整个文件夹
4000
压缩
public File zip(String filePath) { File target = null; File source = new File(filePath); if (source.exists()) { // 压缩文件名=源文件名.zip //这里可以对目标文件路径进行设置 String zipName = source.getName() + ".zip"; target = new File(source.getParent(), zipName); if (target.exists()) { target.delete(); // 删除旧的文件 } FileOutputStream fos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(target); zos = new ZipOutputStream(new BufferedOutputStream(fos)); // 添加对应的文件Entry File[] files = source.listFiles(); for(int i = 0 ; i < files.length ;i++){ addEntry("", files[i], zos); } zos.close(); fos.close(); } catch (IOException e) { throw new RuntimeException(e); } } return target; } private static void addEntry(String base, File source, ZipOutputStream zos) throws IOException { // 按目录分级,形如:/aaa/bbb.txt String entry = base + source.getName(); if (source.isDirectory()) { for (File file : source.listFiles()) { // 递归列出目录下的所有文件,添加文件Entry addEntry(entry + "/", file, zos); } } else { FileInputStream fis = null; BufferedInputStream bis = null; try { byte[] buffer = new byte[1024 * 10]; fis = new FileInputStream(source); bis = new BufferedInputStream(fis, buffer.length); int read = 0; zos.putNextEntry(new ZipEntry(entry)); while ((read = bis.read(buffer, 0, buffer.length)) != -1) { zos.write(buffer, 0, read); } zos.closeEntry(); } finally { bis.close(); fis.close(); } } }
解压
public void UnZip(String zipFileName , String armFilePath){ //解压文件,zipFileName为压缩文件路径,armFilePath为解压文件夹路径 try { ZipInputStream zis = new ZipInputStream(new FileInputStream(new File(zipFileName))); BufferedInputStream bis = new BufferedInputStream(zis); ZipEntry zipEntry; while((zipEntry = zis.getNextEntry()) != null && !zipEntry.isDirectory()){ File nowFileOut = new File(armFilePath,zipEntry.getName()); if(!nowFileOut.exists()){ (new File(nowFileOut.getParent())).mkdirs(); } BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(nowFileOut)); byte[] buffer = new byte[1024 * 10]; int read = 0; while((read = bis.read(buffer,0,buffer.length)) != -1){ bos.write(buffer,0,read); } bos.close(); } bis.close(); zis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
删除整个文件夹
private 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(); }
4000
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树