把一个目录下的所有文件和文件夹打成.tar.gz包(从当前的目录开始)
本篇文章是:把一个目录下的所有文件和文件夹打包为一个 name.tar.gz文件包,文件名自己定义。 经过测试:可直接用来用,main方法都有了 注:这个方式找了好久也没找到,最后自己慢慢写的,好不容易写出来,保存到这里方便我自己后续学习也方便网络里的大家庭。
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.utils.IOUtils;
import java.io.*;
import java.util.zip.GZIPOutputStream;
public class TarGipFileUtil {
public static void main(String[] args) {
String sourceFilePath = "E:/aarm/static/ABX/ide-sdk";
String tarFilePath = "E:/aarm/static/ABX/ide-sdk-tarGz";
String tarFileName = "ide-sdk.tar.gz";
createTarFile(sourceFilePath, tarFilePath, "", tarFileName);
}
public static void createTarFile(String sourceFolder, String tarGzPath,String ignoreDir, String tarGzFileName) {
TarArchiveOutputStream tarOs = null;
try {
File tarGzFile = new File(tarGzPath);
File sourceFile = new File(sourceFolder);
if (!tarGzFile.exists()) {
tarGzFile.mkdirs();
}
if (!sourceFile.exists()) {
throw new FileNotFoundException("压缩的目录不存在。。。");
}
File tarFile = new File(tarGzFile + "/" + tarGzFileName);
FileOutputStream fos = new FileOutputStream(tarFile);
GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos));
tarOs = new TarArchiveOutputStream(gos);
tarOs.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
addFilesToTarGZ(sourceFile, "", tarOs,ignoreDir);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
tarOs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void addFilesToTarGZ(File file, String parent, TarArchiveOutputStream tarArchive,String ignoreDir)
throws IOException {
if(parent.startsWith(ignoreDir+ File.separator)) {
parent = parent.replace(ignoreDir+File.separator, File.separator);
}
String entryName = parent + file.getName();
if(!parent.equals("")) {
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();
if (files != null) {
for (File f : files) {
addFilesToTarGZ(new File(f.getAbsolutePath()), entryName + File.separator, tarArchive,ignoreDir);
}
}
}
}
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();
}
public static void copyDirectory(String sourcePathString,String targetPathString){
if(!new File(sourcePathString).canRead()){
System.out.println("源文件夹" + sourcePathString + "不可读,无法复制!");
}else{
(new File(targetPathString)).mkdirs();
System.out.println("开始复制文件夹" + sourcePathString + "到" + targetPathString);
File[] files = new File(sourcePathString).listFiles();
for (File file : files) {
if (file.isFile()) {
copyFile(new File(sourcePathString + File.separator + file.getName()), new File(targetPathString + File.separator + file.getName()));
} else if (file.isDirectory()) {
copyDirectory(sourcePathString + File.separator + file.getName(), targetPathString + File.separator + file.getName());
}
}
System.out.println("复制文件夹" + sourcePathString + "到" + targetPathString + "结束");
}
}
public static void copyFile(File sourceFile,File targetFile){
if(!sourceFile.canRead()){
System.out.println("源文件" + sourceFile.getAbsolutePath() + "不可读,无法复制!");
}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(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();
}
}
}
}
}
|