public class FileUtil {
/**
*
* <p>Method :createTarFile
* <p>Description : 打包文件 .tar.gz
*
* @param sourceFolder
* @param tarGzPath
* @param ignoreDir
*/
public static void createTarFile(String sourceFolder, String tarGzPath,String ignoreDir) {
TarArchiveOutputStream tarOs = null;
try {
// 创建一个 FileOutputStream 到输出文件(.tar.gz)
FileOutputStream fos = new FileOutputStream(tarGzPath);
// // 创建一个 GZIPOutputStream,用来包装 FileOutputStream 对象
GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos));
// 创建一个 TarArchiveOutputStream,用来包装 GZIPOutputStream 对象
tarOs = new TarArchiveOutputStream(gos);
// 若不设置此模式,当文件名超过 100 个字节时会抛出异常,异常大致如下:
// is too long ( > 100 bytes)
// 具体可参考官方文档:http://commons.apache.org/proper/commons-compress/tar.html#Long_File_Names
tarOs.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
addFilesToTarGZ(sourceFolder, "", tarOs,ignoreDir);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
tarOs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void addFilesToTarGZ(String filePath, String parent, TarArchiveOutputStream tarArchive,String ignoreDir)
throws IOException {
File file = new File(filePath);
//不要再包一层最上面的目录
if(parent.startsWith(ignoreDir+File.separator)) {
parent = parent.replace(ignoreDir+File.separator, File.separator);
}
String entryName = parent + file.getName();
if(!parent.equals("")) {
// 添加 tar ArchiveEntry
tarArchive.putArchiveEntry(new TarArchiveEntry(file, entryName));
}
if (file.isFile()) {
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
// 写入文件
IOUtils.copy(bis, tarArchive);
tarArchive.closeArchiveEntry();
bis.close();
} else if (file.isDirectory()) {
// 因为是个文件夹,无需写入内容,关闭即可
if(!parent.equals("")) {
tarArchive.closeArchiveEntry();
}
File[] files = file.listFiles();
// 读取文件夹下所有文件
for (File f : file.listFiles()) {
// 递归
addFilesToTarGZ(f.getAbsolutePath(), entryName + File.separator, tarArchive,ignoreDir);
}
}
}
/**
* 递归删除目录下的所有文件及子目录下所有文件
* @param dir 将要删除的文件目录
* @return boolean Returns "true" if all deletions were successful.
* If a deletion fails, the method stops attempting to
* delete and returns "false".
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
//递归删除目录中的子目录下
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
/**
*
* <p>Method :copyDirectory
* <p>Description : 复制文件夹
*
* @param sourcePathString
* @param targetPathString
*/
public static void copyDirectory(String sourcePathString,String targetPathString){
if(!new File(sourcePathString).canRead()){
System.out.println("源文件夹" + sourcePathString + "不可读,无法复制!");
return;
}else{
(new File(targetPathString)).mkdirs();
System.out.println("开始复制文件夹" + sourcePathString + "到" + targetPathString);
File[] files = new File(sourcePathString).listFiles();
for(int i = 0; i < files.length; i++){
if(files[i].isFile()){
copyFile(new File(sourcePathString + File.separator + files[i].getName()),new File(targetPathString + File.separator + files[i].getName()));
}else if(files[i].isDirectory()){
copyDirectory(sourcePathString + File.separator + files[i].getName(),targetPathString + File.separator + files[i].getName());
}
}
System.out.println("复制文件夹" + sourcePathString + "到" + targetPathString + "结束");
}
}
/**
*
* <p>Method :copyFile
* <p>Description : 复制文件
*
* @param sourceFile
* @param targetFile
*/
public static void copyFile(File sourceFile,File targetFile){
if(!sourceFile.canRead()){
System.out.println("源文件" + sourceFile.getAbsolutePath() + "不可读,无法复制!");
return;
}else{
System.out.println("开始复制文件" + sourceFile.getAbsolutePath() + "到" + targetFile.getAbsolutePath());
FileInputStream fis = null;
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try{
fis = new FileInputStream(sourceFile);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(targetFile);
bos = new BufferedOutputStream(fos);
int len = 0;
while((len = bis.read()) != -1){
bos.write(len);
}
bos.flush();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
try{
if(fis != null){
fis.close();
}
if(bis != null){
bis.close();
}
if(fos != null){
fos.close();
}
if(bos != null){
bos.close();
}
System.out.println("文件" + sourceFile.getAbsolutePath() + "复制到" + targetFile.getAbsolutePath() + "完成");
}catch(IOException e){
e.printStackTrace();
}
}
}
}
}