文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.
FileOperationUtils.java
package com.xnl.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;
import org.apache.commons.io.FileUtils;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
/**
* 文件操作工具类:
* 文件/目录的创建、删除、移动、复制、zip压缩与解压
* 文件操作过程中要注意是否有操作权限的问题,以免报错。
* commons-io-2.5.jar
* ant-1.6.5.jar
* @date 2017年3月1日
* @author xnl
*/
public class FileOperations { /*使用GBK编码避免压缩中文文件名乱码*/
private static final String CHINESE_CHARSET = "GBK";
/*文件读取缓冲区大小*/
private static final int CACHE_SIZE = 1024 * 50;
/**
* 压缩文件或文件夹为zip格式文件
* @param sourceFile 文件或文件夹
* @param zipFilePath 压缩文件输出路径(以.zip结尾)
* @throws Exception
*/
public static void zip(String sourceFile, String zipFilePath) throws Exception {
File sourcefile = new File(sourceFile);
if (!sourcefile.exists()) {
throw new Exception("指定的文件不存在!");
}
File zipFile = new File(zipFilePath);
if (!zipFile.getParentFile().exists()) { // 如果目标文件的父目录不存在,则创建父目录
new File(zipFile.getParentFile().getPath()).mkdirs();
}
OutputStream out = new FileOutputStream(zipFilePath);
BufferedOutputStream bos = new BufferedOutputStream(out);
ZipOutputStream zos = new ZipOutputStream(bos);
zos.setEncoding(CHINESE_CHARSET);// 解决中文文件名乱码问题
String basePath = null;
if (sourcefile.isDirectory()) {
basePath = sourcefile.getPath();
} else {
basePath = sourcefile.getParent();
}
zipFile(sourcefile, basePath, zos);
zos.closeEntry();
zos.close();
bos.close();
out.close();
}
/**
* 递归压缩文件
* @param parentFile
* @param basePath
* @param zos
* @throws Exception
*/
private static void zipFile(File parentFile, String basePath, ZipOutputStream zos) throws Exception {
File[] files = new File[0];
if (parentFile.isDirectory()) {
files = parentFile.listFiles();
} else {
files = new File[1];
files[0] = parentFile;
}
String pathName;
InputStream is;
BufferedInputStream bis;
byte[] cache = new byte[CACHE_SIZE];
for (File file : files) {
// TODO 判断文件是否正在读写中,如果是,则不压缩该文件。
if (!file.renameTo(file)) {
continue;
}
if (file.isDirectory()) {
pathName = file.getPath().substring(basePath.length() + 1) + File.separator;
zos.putNextEntry(new ZipEntry(pathName));
zipFile(file, basePath, zos);
} else {
pathName = file.getPath().substring(basePath.length() + 1);
is = new FileInputStream(file);
bis = new BufferedInputStream(is);
zos.putNextEntry(new ZipEntry(pathName));
int nRead = 0;
while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
zos.write(cache, 0, nRead);
}
bis.close();
is.close();
}
}
}
/**
* 功能:压缩多个文件成一个zip文件
* @param sourceFile:源文件列表
* @param targetZipFile:压缩后的文件
* @throws Exception
*/
public static void zipFiles(File[] sourceFiles, File targetZipFile) throws Exception {
byte[] buf = new byte[CACHE_SIZE];
ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(targetZipFile));
zos.setEncoding(CHINESE_CHARSET);// 解决中文文件名乱码问题
FileInputStream fis = null;
for (int i = 0; i < sourceFiles.length; i++) {
fis = new FileInputStream(sourceFiles[i]);
zos.putNextEntry(new ZipEntry(sourceFiles[i].getName()));
int len;
while ((len = fis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
if (zos != null) {
zos.closeEntry();
}
if (fis != null) {
fis.close();
}
}
if (zos != null) {
zos.close();
}
}
/**
* 解压指定的zip压缩包到目标目录
* 对应的是ant-1.6.5.jar
* @param sourceFile 指定的zip压缩包文件
* @param destDir 目标目录
* @throws Exception
*/
public static void unzip(String sourceFile, String destDir) throws Exception {
Project p = new Project();
Expand expand = new Expand();
expand.setProject(p);
expand.setSrc(new File(sourceFile));
expand.setOverwrite(false);
expand.setDest(new File(destDir));
expand.setEncoding(CHINESE_CHARSET);
expand.execute();
}
/**
* copy普通文件到目标文件夹下
* @param sourceFile 源文件路径
* @param targetDir 目标文件夹路径
* @throws Exception
*/
public static void copyFileToDir(String sourceFile, String targetDir) throws Exception {
File source = new File(sourceFile);
File target = new File(targetDir);
FileUtils.copyFileToDirectory(source, target);
}
/**
* copy普通文件到目标文件,可实现文件同目录改名
* @param sourceFile 普通文件
* @param targetFile 目标文件
* @throws Exception
*/
public static void copyFileToFile(String sourceFile, String targetFile) throws Exception {
File source = new File(sourceFile);
File target = new File(targetFile);
FileUtils.copyFile(source, target);
}
/**
* copy指定文件夹到目标文件夹
* @param sourceDir 指定原文件夹
* @param targetDir 目标文件夹
* @throws Exception
*/
public static void copyDirToDir(String sourceDir, String targetDir) throws Exception {
File source = new File(sourceDir);
File target = new File(targetDir);
FileUtils.copyDirectoryToDirectory(source, target);
}
/**
* 删除指定的文件夹(递归删除多级目录)
* 注意:不论文件夹是否为空都可以删除
* @param sourceDir 指定的文件夹路径
* @throws Exception
*/
public static void deleteDir(String sourceDir) throws Exception {
File directory = new File(sourceDir);
FileUtils.deleteDirectory(directory);
}
/**
* 删除文件或文件夹,
* 注意:不论文件夹是否为空都可以删除,请慎用
* @param sourceFilePath 指定的文件\文件夹路径
*/
public static void deleteFile(String sourceFilePath) {
File source = new File(sourceFilePath);
FileUtils.deleteQuietly(source);
}
/**
* 指定文件移动到目标文件,可以实现指定文件的同目录下改名,同时会删除原文件
* @param sourceFile 指定文件路径
* @param targetFile 目标文件路径
* @throws Exception
*/
public static void moveFileToFile(String sourceFile, String targetFile) throws Exception {
File source = new File(sourceFile);
File target = new File(targetFile);
FileUtils.moveFile(source, target);
}
/**
* 指定文件移动到目标目录下,同时会删除原文件
* @param sourceFile 指定文件
* @param targetDir 目标目录
* @throws Exception
*/
public static void moveFileToDir(String sourceFile, String targetDir) throws Exception {
File source = new File(sourceFile);
File target = new File(targetDir);
FileUtils.moveToDirectory(source, target, true);
}
/**
* 指定目录移动到目标目录,同时会删除原目录
* @param sourceDir 指定源目录
* @param targetDir 目标目录
* @throws Exception
*/
public static void moveDirToDir(String sourceDir, String targetDir) throws Exception {
File source = new File(sourceDir);
File target = new File(targetDir);
FileUtils.moveDirectoryToDirectory(source, target, true);
}
/**
* 创建单个普通文件(不能是目录)
* @param descFileName 文件名,包含路径
* @return 如果文件创建成功,则返回true,否则返回false
* @throws Exception
*/
public static boolean createFile(String descFileName) throws Exception {
File file = new File(descFileName);
if (file.exists()) { // 如果文件已经存在,则创建失败
return false;
}
if (descFileName.endsWith(File.separator)) { // 如果文件路径以'/'结尾,则是目录,不能创建目录
return false;
}
if (!file.getParentFile().exists()) { // 如果文件的父目录不存在,则需要创建
// 如果文件所在的目录不存在,则创建目录
if (!file.getParentFile().mkdirs()) { // 如果父目录创建失败,则文件创建也失败
return false;
}
}
if (file.createNewFile()) { // 文件创建成功
return true;
} else { // 文件创建失败
return false;
}
}
/**
* 创建目录
* @param descDirName 目录名,包含路径
* @return 如果目录创建成功,则返回true,否则返回false
*/
public static boolean createDirectory(String descDirName) {
String descDirNames = descDirName;
if (!descDirNames.endsWith(File.separator)) {
descDirNames = descDirNames + File.separator;
}
File descDir = new File(descDirNames);
if (descDir.exists()) { // 目录已经存在,则创建失败
return false;
}
if (descDir.mkdirs()) { // 创建成功
return true;
} else { // 创建失败
return false;
}
}
/**
* 压缩指定文件夹下符合匹配规则的文件到目标目录下
* @param regex 匹配规则
* @param sourceDir 源文件夹
* @param targetZipFile 目标zip文件的路径及名称
* @param isEnd 是否以regex匹配规则结尾,为true则表示按照扩展名过滤,false则表示按照文件名(除扩展名外)过滤
* @throws Exception
*/
public static void zip(String regex, String sourceDir, String targetZipFile, boolean isEnd) throws Exception {
File sourceDirFile = new File(sourceDir);
File targetZip = new File(targetZipFile);
if (!targetZip.getParentFile().exists()) {
new File(targetZip.getParent()).mkdirs();
}
File[] files = null;
if (!isEmpty(regex) && isEnd) {
// 源文件夹下符合过滤条件的文件,不包含子文件夹中的文件在内
files = sourceDirFile.listFiles(new ExtensionFilter(regex));
} else if (!isEmpty(regex) && !isEnd) {
// 源文件夹符合过滤条件的所有文件及文件夹的路径及名称,不包含子文件夹中的文件或文件夹在内
files = sourceDirFile.listFiles(new NameFilter(regex));
} else {
// 源文件夹下的所有的文件/文件夹的路径及名称,不包含子文件夹内的文件/文件夹
files = sourceDirFile.listFiles();
}
System.out.println(Arrays.toString(files));
if (files != null && files.length > 0) {
zipFiles(files, targetZip);
} else {
throw new Exception("不存在符合匹配规则的文件!");
}
}
private static boolean isEmpty(String str) {
if (str == null || "".equals(str)) {
return true;
}
return false;
}
}
/**文件名过滤器,只匹配符合规则的文件,不包括文件夹*/
class NameFilter implements FilenameFilter {
/*匹配规则*/
private String regex;
public NameFilter(String regex) {
this.regex = regex;
}
@Override
public boolean accept(File dir, String name) {
File f = new File(dir.getPath() + File.separator + name);
if (f.isDirectory()) {
return false;
}
if (name.contains(".")) {
name = name.substring(0, name.lastIndexOf(".")); // 将文件名的扩展名去掉
}
if (name.indexOf(regex) > -1) {
return true;
}
return false;
}
}
/**扩展名过滤器,只匹配符合规则的文件,不包括文件夹*/
class ExtensionFilter implements FileFilter {
/*匹配规则*/
private String regex;
public ExtensionFilter(String regex) {
this.regex = regex;
}
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return false;
}
String filename = pathname.getName();
boolean isMatched = filename.endsWith(regex);
if (pathname.isFile() && isMatched) {
return true;
}
return false;
}
}
文件操作工具类: 文件/目录的创建、删除、移动、复制、zip压缩与解压.的更多相关文章
- golang zip 压缩,解压(含目录文件)
每天学习一点go src. 今天学习了zip包的简单使用,实现了含目录的压缩与解压. 写了两个方法,实现了压缩.解压. package ziptest import ( "archive/z ...
- zip压缩与解压文件夹或文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- JAVA文件操作工具类(读、增、删除、复制)
使用JAVA的JFinal框架 1.上传文件模型类UploadFile /** * Copyright (c) 2011-2017, James Zhan 詹波 (jfinal@126.com). * ...
- C#使用ICSharpCode.SharpZipLib.dll进行文件的压缩与解压
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...
- 【转】iOS开发之压缩与解压文件
ziparchive是基于开源代码”MiniZip”的zip压缩与解压的Objective-C 的Class,使用起来非常的简单方法:从http://code.google.com/p/ziparch ...
- iOS开发之压缩与解压文件
ziparchive是基于开源代码”MiniZip”的zip压缩与解压的Objective-C 的Class,使用起来非常的简单 方法:从http://code.google.com/p/ziparc ...
- Java_压缩与解压工具类
转载请注明出处:http://blog.csdn.net/y22222ly/article/details/52201675 zip压缩,解压 zip压缩与解压主要依靠java api的两个类: Zi ...
- Android文件操作工具类(转)
Android文件操作工具类(转) 2014/4/3 18:13:35 孤独的旅行家 博客园 这个工具类包含Android应用开发最基本的几个文件操作方法,也是我第一次发博客与大家分享自己写的东 ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(4)-文件操作工具类FileOperationHelper
文件操作是非常通用的,注释都写在源代码中了,不多说~需要特别说明的是,任务的异步执行和IOperationProgressListener.拷贝和删除等操作,是比较费时的,采用了异步执行的方式~ An ...
随机推荐
- Java并发框架——AQS之怎样使用AQS构建同步器
AQS的设计思想是通过继承的方式提供一个模板让大家能够非常easy依据不同场景实现一个富有个性化的同步器.同步器的核心是要管理一个共享状态,通过对状态的控制即能够实现不同的锁机制. AQS的设计必须考 ...
- scrollHeight、clientHeight、offsetHeight、scrollTop等的定义以及图解
开发中经常遇到使用scrollHeight.scrollTop.clientHeight.offsetHeight等的情况,网上有众多关于这些属性的解释,但是并不全面和直观,现在将这些属性结合图例整理 ...
- iOS应用开发最佳实践:编写高质量的Objective-C代码
本文转载至 http://www.cocoachina.com/industry/20131129/7445.html 点标记语法 属性和幂等方法(多次调用和一次调用返回的结果相同)使用点标记语法访问 ...
- 总结OpenvSwitch的调试经验
相信相信不少人在开始用OpenvSwitch(以下简称OVS)或者修改源码的时候,都不知道如何去调试,根据我的学习历程以及从网上搜集的资料做一个汇总. 一. 个人经验 从网上找相关资料,熟悉Openv ...
- 【BZOJ4584】[Apio2016]赛艇 DP
[BZOJ4584][Apio2016]赛艇 Description 在首尔城中,汉江横贯东西.在汉江的北岸,从西向东星星点点地分布着个划艇学校,编号依次为到.每个学校都拥有若干艘划艇.同一所学校的所 ...
- 【BZOJ2259】[Oibh]新型计算机 最短路
[BZOJ2259][Oibh]新型计算机 Description Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计 ...
- [LintCode] 最后一个单词的长度
class Solution { public: /** * @param s A string * @return the length of last word */ int lengthOfLa ...
- 第5章 IDA Pro实验题
Question: 1.DLLMain的地址是什么? 2.使用import窗口并浏览到gethostbyname,导入函数定位到什么位置 3.有多少函数调用了gethostbyname? 4.将精力放 ...
- QStorageInfo获取磁盘信息(非常详细)
QStorageInfo类提供了系统当前挂载的存储和驱动器的相关信息,包括它们的空间,挂载点,标签名,文件系统名. 一般,我们可以使用特定的文件或目录来创建一个QStorageInfo类的对象,也可以 ...
- 编译安装cmake3
编译安装cmake3 ubuntu 14 的系统默认安装的是cmake2.7,apt-get upgrade之后也还是cmake2.7,而很多软件如今需要3及以上的版本来进行cmake编译(如caff ...