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();
}
}

Java zip 压缩 文件夹删除,移动,重命名,复制的更多相关文章

  1. java ZIP压缩文件

    问题描述:     使用java ZIP压缩文件和目录 问题解决:     (1)单个文件压缩 注:     以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...

  2. python之对指定目录文件夹的批量重命名

    python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...

  3. Python 写了一个批量生成文件夹和批量重命名的工具

    Python 写了一个批量生成文件夹和批量重命名的工具 目录 Python 写了一个批量生成文件夹和批量重命名的工具 演示 功能 1. 可以读取excel内容,使用excel单元格内容进行新建文件夹, ...

  4. java zip 压缩文件

    zip压缩:ZipOutputStream.ZipFile.ZipInputStream 三个类的作用 一段 java  zip  压缩的代码: File dir = new File("C ...

  5. java zip压缩文件和文件夹

    public class FileUtil { /** * 压缩文件-File * @param out zip流 * @param srcFiles 要压缩的文件 * @param path 相对路 ...

  6. ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器

    遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并copy到服务器的映射磁盘. static void Main(string[] args) { //原始文件存放的位置 Dir ...

  7. zip 压缩文件夹

    import java.io.*; import java.util.zip.*; /** * @author Dana·Li * <p> * 程序实现了ZIP压缩[compression ...

  8. 使用zip压缩文件夹方法

    最近使用MapGis对.MPJ工程文件文件裁剪后,要对裁剪后的图形文件.ML,.MT,.MP,.MPJ文件打包,在网上找到7zip,Zlib的库,虽然都有源码,但是Zlib库中的使用没找到文件压缩的函 ...

  9. Python 入门学习(贰)文件/文件夹正则表达式批量重命名工具

    基于 Udacity 的 Python 入门课程 Programming Foundations with Python 基于 Python 2.7 思路 Project 2 是一个去除文件名中所有数 ...

随机推荐

  1. genymotion启动报错

    启动genymotion时提示 网络配置有问题,经检查是网卡virtualBox配置的问题,把ip设为自动获取即可 重新启动就可以了

  2. diamond源码阅读-diamond-client

    读取数据 DiamondManager manager = new DefaultDiamondManager("DEFAULT_GROUP", "zml", ...

  3. weixin oauth 授权

    1. 先了解下请求授权页面的构造方式:   https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_u ...

  4. C#调用python脚本

    因项目需要,需要使用C#控制台程序执行python脚本,查询各种资料后可以成功调用了,记录一下,以备后面遗忘. 只尝试了两种调用方式,第一种只适用于python脚本中不包含第三方模块的情况,第二种针对 ...

  5. point-position2修改版

    说明: 在共面直线测试中,由于计算误差等原因,共面条件判断不准,但计算结果依然正确. // point-position2.cpp : 定义控制台应用程序的入口点. #include "st ...

  6. Jmeter中中文乱码

    jmeter-察看结果树-响应数据中的中文显示乱码 jmeter\bin\jmeter.properties 默认编码为:ISO-8859-1# The encoding to be used if ...

  7. MySQL-库的操作

    05-库的操作   本节重点: 掌握库的增删改查   一.系统数据库 执行如下命令,查看系统库 show databases; nformation_schema: 虚拟库,不占用磁盘空间,存储的是数 ...

  8. 3N Numbers

    D - 3N Numbers Time limit : 2sec / Memory limit : 256MB Score : 500 points Problem Statement Let N b ...

  9. Man-in-the-middle attack

    w https://en.wikipedia.org/wiki/Man-in-the-middle_attack https://zh.wikipedia.org/wiki/中间人攻击 需要通过一个安 ...

  10. 【转】IBM PowerVM虚拟化技术笔记

    1. 从CPU虚拟化的角度, 分区(partition)可以分成两大类:Micro-partition和Dedicated-procesor.前者可以将物理处理器以0.01的 粒度分配给微分区,分区创 ...