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. Servlet 客户端 HTTP 请求

    当浏览器请求网页时,它会向 Web 服务器发送特定信息,这些信息不能被直接读取,因为这些信息是作为 HTTP 请求的头的一部分进行传输的.您可以查看 HTTP 协议 了解更多相关信息. 以下是来自于浏 ...

  2. windows中控制台窗口和普通窗口有什么区别?

    1. 窗口都是windows标准窗口,有窗口句柄,但是console window没有消息循环,直接从缓冲区读数据,显示数据. windows中普通窗口都有自己的窗口过程, 我可以使用SetWindo ...

  3. android studio win7开发环境

    java 开发环境 这里使用jdk1.7版本,从官网上下载. 点击.正常的window软件的安装方式,一直下一步即可. 环境变量的设置: 在系统属性中,对需要的环境变量进行设置: JAVA_HOME设 ...

  4. json responseJson

    private void doResoponseJson(HttpServletResponse resp,String jsonString){ Trace.logError(Trace.COMPO ...

  5. Unity3D学习笔记——NGUI之Property Binding

    Property Binding:用于绑定两个组件,然后可以将一个组件的信息发送给另一个组件. 效果图如下: 一:使用步骤 1.建立一个Sprite 2.建立一个Label 3.为Sprite添加Pr ...

  6. CFindReplaceDialog学习

    The CFindReplaceDialog class allows you to implement standard string Find/Replace dialog boxes in yo ...

  7. 进程 vs. 线程

    我们介绍了多进程和多线程,这是实现多任务最常用的两种方式.现在,我们来讨论一下这两种方式的优缺点. 首先,要实现多任务,通常我们会设计Master-Worker模式,Master负责分配任务,Work ...

  8. dva解读1

    1.首先定义一个app对象实现dva const app = dva({ history: createHistory(), }); // 2. Plugins app.use(createLoadi ...

  9. php后门管理工具weevely

    weevely是一款php后门管理工具,使用http头进行指令传输,功能强大.不过只支持php. weevely生成的服务器端php代码是经过了base64编码的,所以可以骗过主流的杀毒软件和IDS, ...

  10. 【BZOJ3930】[CQOI2015]选数 莫比乌斯反演

    [BZOJ3930][CQOI2015]选数 Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律 ...