FileUtil.java

import java.io.*;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;/*** 文件操作* Created by heavenick on 2015/7/8.*/
public class FileUtil {public static void main(String[] args) throws IOException {copyFile("E:\\upload\\create\\1436144988371_JL33041594.xml","E:\\test\\upload");
//        deleteFile("E:\\test\\upload\\");}    /*** 移动 文件或者文件夹* @param oldPath* @param newPath* @throws IOException*/public static void moveTo(String oldPath,String newPath) throws IOException {copyFile(oldPath,newPath);deleteFile(oldPath);}/*** 删除 文件或者文件夹* @param filePath*/public static void deleteFile(String filePath){File file = new File(filePath);if (!file.exists()) {return;}if (file.isDirectory() ) {File[] list = file.listFiles();for (File f : list) {deleteFile(f.getAbsolutePath()) ;}}file.delete();}/*** 复制 文件或者文件夹* @param oldPath* @param newPath* @throws IOException*/public static void  copyFile(String oldPath ,String newPath ) throws IOException {System.out.println("copy file from [" + oldPath + "] to [" + newPath +"]");File oldFile = new File(oldPath) ;if  (oldFile.exists())  {if(oldFile.isDirectory()){ // 如果是文件夹File newPathDir = new File(newPath);newPathDir.mkdirs();File[] lists = oldFile.listFiles() ;if(lists != null && lists.length > 0 ){for (File file : lists) {copyFile(file.getAbsolutePath(), newPath.endsWith(File.separator) ? newPath + file.getName() : newPath + File.separator + file.getName()) ;}}}else {InputStream  inStream  =  new  FileInputStream(oldFile);  //读入原文件FileOutputStream  fs  =  new  FileOutputStream(newPath);write2Out(inStream ,fs) ;inStream.close();}}}/*** 重命名文件* @param file* @param name* @return*/public static File renameFile(File file , String name ){String fileName = file.getParent()  + File.separator + name ;File dest = new File(fileName);file.renameTo(dest) ;return dest ;}/*** 压缩多个文件。* @param zipFileName 压缩输出文件名* @param files 需要压缩的文件* @return* @throws Exception*/public static File createZip(String zipFileName, File... files) throws Exception {File outFile = new File(zipFileName) ;ZipOutputStream out = null;BufferedOutputStream bo = null;try {out = new ZipOutputStream(new FileOutputStream(outFile));bo = new BufferedOutputStream(out);for (File file : files) {zip(out, file, file.getName(), bo);}} catch (Exception e) {e.printStackTrace();}finally {try {bo.close();} finally {out.close(); // 输出流关闭}}return outFile;}/**** @param zipFileName 压缩输出文件名* @param inputFile 需要压缩的文件* @return* @throws Exception*/public static File createZip(String zipFileName, File inputFile) throws Exception {File outFile = new File(zipFileName) ;ZipOutputStream out = null;BufferedOutputStream bo = null;try {out = new ZipOutputStream(new FileOutputStream(outFile));bo = new BufferedOutputStream(out);zip(out, inputFile, inputFile.getName(), bo);} catch (Exception e) {e.printStackTrace();}finally {try {bo.close();} finally {out.close(); // 输出流关闭}}return outFile;}private static void zip(ZipOutputStream out, File f, String base,BufferedOutputStream bo) throws Exception { // 方法重载if (f.isDirectory()) {File[] fl = f.listFiles();if ( fl == null ||  fl.length == 0) {out.putNextEntry(new ZipEntry(base + "/")); // 创建创建一个空的文件夹}else{for (int i = 0; i < fl.length; i++) {zip(out, fl[i], base + "/" + fl[i].getName(), bo); // 递归遍历子文件夹}}} else {out.putNextEntry(new ZipEntry(base)); // 创建zip压缩进入 base 文件System.out.println(base);BufferedInputStream bi = new BufferedInputStream(new FileInputStream(f));try {write2Out(bi,out) ;} catch (IOException e) {//Ignore}finally {bi.close();// 输入流关闭}}}private static void write2Out(InputStream input , OutputStream out) throws IOException {byte[] b = new byte[1024];int c = 0 ;while ( (c = input.read(b)) != -1 ) {out.write(b,0,c);out.flush();}out.flush();}
}

转载于:https://www.cnblogs.com/qixidi/p/10213564.html

Java zip 压缩 文件夹删除,移动,重命名,复制相关推荐

  1. python怎么把一个文件夹内的文件复制到另外一个文件夹(进阶重命名复制)

       需求:把一个文件夹内(包含子文件夹)的所有文件复制到另一个文件夹下 #coding=utf-8 import os import shutilold_path = r'F:\1' # 要复制的文 ...

  2. 电脑文件夹怎么批量重命名

    不管你做什么工作,不管你能力如何,只要你有爱岗敬业的精神,才能把工作做好.那么你面对电脑文件夹是怎么管理的呢?你是否会定期对电脑文件夹进行批量重命名整理?其实对电脑文件夹的整理也是我们工作中很重要的一 ...

  3. 使用re进行文件夹的批量重命名

    使用re进行文件夹的批量重命名 import os import re filepath = "E:\面授基础高等数学-汤家凤" # 文件夹路径 #对目录下的文件进行遍历 for ...

  4. Java Zip解压缩文件夹工具类 ----ZipUtils

    在项目中如果遇到解压缩 文件的话,可以直接使用这个工具类进行操作.不多说,直接上代码: 1. maven 依赖: 可能会有多余的,没有做处理 <build><plugins>& ...

  5. java zip压缩文件

    java 生成zip压缩文件 1.zip压缩文件 文章目录 java 生成zip压缩文件 一.引入jar包 二.工具类 1.有密码压缩 2.无密码压缩 3. 解压方法 三.示例 3.1.压缩前 3.2 ...

  6. 怎么对文件夹进行随机重命名?怎么批量给多个文件夹生成随机名称?

    概要:之前我们介绍过对文件名称进行随机重命名,今天给大家带来的是批量重命名文件夹的功能.这个功能在某些场景下也是非常有用的,那怎么批量给多个文件夹生成随机名称呢?我们可以一起看看下具体的操作! 效果预 ...

  7. php文件夹操作:读取文件夹、批量重命名文件

    以读取从网上下载的评书解压后的文件夹为例,并批量重命名 文件夹内容如下,现在使用php读取这个文件夹里面的文件,并批量重命名为"隋唐演义+序号.mp3"的格式: <?php ...

  8. java加密文件夹_使用java.util.zip压缩文件夹,支持加密,增加描述

    导读热词 下面是编程之家 jb51.cc 通过网络收集整理的代码片段. 编程之家小编现在分享给大家,也给大家做个参考. import java.io.File; import java.io.File ...

  9. Linux: cp 复制文件、文件夹到文件夹、mv重命名

    cp命令的各选项含义如下 -a 该选项通常在拷贝目录时使用.它保留链接.文件属性,并递归地拷贝目录,其作用等于dpR选项的组合. -d 拷贝时保留链接. -f 删除已经存在的目标文件而不提示. -i ...

最新文章

  1. yolov3(二:车牌识别)
  2. Python3 集合set
  3. 【转】从源码分析Handler的postDelayed为什么可以延时?
  4. 连续赋值与求值顺序var a = {n:1};a.x = a = {n:2}; alert(a.x);
  5. 用cookie实现叶卡的记忆功能
  6. C语言 单链表查找出倒数第,查找单链表倒数第k个元素
  7. 采用成员函数和友元函数计算给定两个坐标点之间的距离
  8. 关于并发数与在线数的概念
  9. Linux FTP服务安装与账号设置
  10. 验证 decimal 和 数字
  11. webmin升级php,Centos linux下webmin安装及配置
  12. pytorch显存管理
  13. c#先进行uri解码_JavaScript、C# URL编码、解码总结
  14. rman异机恢复数据库
  15. Unix操作系统常用命令
  16. 嵌入式软件工程师面试遇到的经典题目
  17. Chinese NER Using Lattice LSTM 论文解读
  18. 用python写出各种三角形
  19. shiro漏洞原理以及检测key值原理
  20. Android 异步进程

热门文章

  1. 从Weex到Web,性能逆势如何破局?
  2. 双面黄琳:世界顶级女黑客,两个孩子的迟钝妈妈
  3. 如何解决大规模机器学习的三大痛点?
  4. linux 脚本$字符,一文看懂shell脚本中$0 $1 $# $@ $* $? $$ 的各种符号意义
  5. 第五章 Mininet常用命令参数介绍
  6. 在linux下安装db2全过程
  7. JavaWeb第四讲 会话跟踪技术HttpSession、Cookie、url、隐藏表单域
  8. sql server2008系统表详细说明sys.开头的表
  9. 数据结构:线段树及ST算法比较
  10. Orchard源码分析(4):Orchard.Environment.OrchardStarter类