Java zip 压缩 文件夹删除,移动,重命名,复制
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 压缩 文件夹删除,移动,重命名,复制的更多相关文章
- java ZIP压缩文件
问题描述: 使用java ZIP压缩文件和目录 问题解决: (1)单个文件压缩 注: 以上是实现单个文件写入压缩包的代码,注意其中主要是在ZipOutStream流对象中创建Z ...
- python之对指定目录文件夹的批量重命名
python之对指定目录文件夹的批量重命名 import os,shutil,string dir = "/Users/lee0oo0/Documents/python/test" ...
- Python 写了一个批量生成文件夹和批量重命名的工具
Python 写了一个批量生成文件夹和批量重命名的工具 目录 Python 写了一个批量生成文件夹和批量重命名的工具 演示 功能 1. 可以读取excel内容,使用excel单元格内容进行新建文件夹, ...
- java zip 压缩文件
zip压缩:ZipOutputStream.ZipFile.ZipInputStream 三个类的作用 一段 java zip 压缩的代码: File dir = new File("C ...
- java zip压缩文件和文件夹
public class FileUtil { /** * 压缩文件-File * @param out zip流 * @param srcFiles 要压缩的文件 * @param path 相对路 ...
- ZIP压缩文件夹中上个月的文件,并将备份文件拷贝到服务器
遍历文件夹的子文件夹下的所有文件,将上个月的文件集中到一起,然互压缩,并copy到服务器的映射磁盘. static void Main(string[] args) { //原始文件存放的位置 Dir ...
- zip 压缩文件夹
import java.io.*; import java.util.zip.*; /** * @author Dana·Li * <p> * 程序实现了ZIP压缩[compression ...
- 使用zip压缩文件夹方法
最近使用MapGis对.MPJ工程文件文件裁剪后,要对裁剪后的图形文件.ML,.MT,.MP,.MPJ文件打包,在网上找到7zip,Zlib的库,虽然都有源码,但是Zlib库中的使用没找到文件压缩的函 ...
- Python 入门学习(贰)文件/文件夹正则表达式批量重命名工具
基于 Udacity 的 Python 入门课程 Programming Foundations with Python 基于 Python 2.7 思路 Project 2 是一个去除文件名中所有数 ...
随机推荐
- 如何给Eclipse添加一个JDK或JRE
第一: 第二: 第三: 第四:
- posix正则表达式说明
转载自:http://baiy.cn/utils/_regex_doc/index.htm 正则表达式说明 简介 大体来讲,正则表达式的文法分为3种标准:BRE.ERE 和 ARE.其中 BER 和 ...
- python 中NumPy和Pandas工具包中的函数使用笔记(方便自己查找)
二.常用库 1.NumPy NumPy是高性能科学计算和数据分析的基础包.部分功能如下: ndarray, 具有矢量算术运算和复杂广播能力的快速且节省空间的多维数组. 用于对整组数据进行快速运算的标准 ...
- MySQL中enum类型数据,要传入字符串
问题来源:公司业务,某张表中一个字段定义为: enum('0','1','2','3','4','5','6','7','8','9','10') NOT NULL DEFAULT '0' 某天跑脚本 ...
- iOS-ASIHTTPRequest框架学习
本文转载至 http://www.cnblogs.com/A-Long-Way-Chris/p/3539679.html 前段时间在公司的产品中支持了够快网盘,用于云盘存储. 在这个过程中,学习到了很 ...
- Android WebView-应用内嵌入浏览器
移动应用开发,web app.Native app的讨论已经很久了,纯粹的web app还很少,多少能见到Native + web混合的app,混合的app是在Native app中写一个浏览器加载 ...
- [Spring MVC]学习笔记--DispatcherServlet
在上一篇我们介绍了Servlet,这一篇主要来看一下MVC中用到的DispatcherServlet(继承自HttpServlet). 1. DispatcherServlet在web.xml中被声明 ...
- SharePoint服务器端对象模型 之 访问文件和文件夹(Part 1)
本节中所阐述的内容,主要适用于SharePoint文档库中的文件和文件夹,以及列表中的文件夹.系统中的其他文件(如_layouts中的文件.配置文件.程序文件等)不在本章节的讨论范围之内. (一) ...
- C语言-数组篇
C语言数组 一.数组的概念 用来存储一组数据的构造数据类型 特点:只能存放一种类型的数据,如全部是int型或者全部是char型,数组里的数据成为元素. 二.数组的定义 格式: 类型 数组名[元素个数] ...
- es 中 for in for of
arr=[11,22,33,44,55,66,77,88]for (const i in arr){ console.log(i) if (i===4){ console.log(arr[i]) }} ...