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 是一个去除文件名中所有数 ...
随机推荐
- php 防止刷新重复下载文件
在超链接中增加随机数. <a href="./index.php?module=operation&action=download&url=D:\WW\WlequPho ...
- Linux 文件管理(C语言库函数三)
找到当前目录 char *getcwd(char * buf,size_t size) getcwd函数把当前工作目录的绝对路径名复制到buf中,size指示buf的大小 如果buf不够大,不能装下整 ...
- JPA(Java Persistence API)Java持久化API-介绍
JPA全称: Java Persistence API JPA的宗旨是为POJO提供持久化标准规范,能够脱离容器独立运行,很方便开发和测试.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关 ...
- 【独立开发人员er Cocos2d-x实战 013】Cocos2dx 网络编程实战之星座运势
学习cocos2d-x和cocos creator的圈子:cocos2d-x:436689827 cocos creator:124727696 本篇文章主要内容:jsoncpp的使用,Coco ...
- [ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)
Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael ...
- OC对象给分类加入属性
OC对象中不能给分类加入属性.可是在实际开发中.常常为了更好的性能须要给分类加入属性,那么 加入的属性不能有默认的成员变量.须要我们自己实现set和get方法,要用到执行时 例如以下: #import ...
- 基于Boost无锁队列实现的内存池
- StartCom免费ssl证书申请以及在Tomcat环境中的配置
提示:建议以下操作不使用谷歌浏览器(该网站的证书不识别...),可以看到我的截图中谷歌换成了ie(没装火狐)...建议该申请使用火狐 前面介绍了下自签名的ssl证书,虽然可以实现https协议访问,但 ...
- MySQL如何优化GROUP BY :松散索引扫描 VS 紧凑索引扫描
执行GROUP BY子句的最一般的方法:先扫描整个表,然后创建一个新的临时表,表中每个组的所有行应为连续的,最后使用该临时表来找到组 并应用聚集函数.在某些情况中,MySQL通过访问索引就可以得到结果 ...
- Python __setitem__()、__getitem__()、__delitem__()
转载:http://blog.csdn.net/xhw88398569/article/details/48690163 __xxxitem__:使用 [''] 的方式操作属性时被调用 __setit ...