引入依赖
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.ant/ant -->
<dependency>
<groupId>org.apache.ant</groupId>
<artifactId>ant</artifactId>
<version>1.10.5</version>
</dependency>
tar.gz 压缩、解压缩
public static void compress(String sourceFolder, String tarGzPath) {
TarArchiveOutputStream tarOs = null;
try {
FileOutputStream fos = new FileOutputStream(tarGzPath);
GZIPOutputStream gos = new GZIPOutputStream(new BufferedOutputStream(fos));
tarOs = new TarArchiveOutputStream(gos);
tarOs.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
File sourceFile = new File(sourceFolder);
File[] sources = sourceFile.listFiles();
for (File oneFile : sources) {
addFilesToTarGZ(oneFile.getPath(), "", tarOs);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
tarOs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void addFilesToTarGZ(String sourcePath, String parent, TarArchiveOutputStream tarArchive) throws IOException {
File sourceFile = new File(sourcePath);
String fileName = parent.concat(sourceFile.getName());
tarArchive.putArchiveEntry(new TarArchiveEntry(sourceFile, fileName));
if (sourceFile.isFile()) {
FileInputStream fis = new FileInputStream(sourceFile);
BufferedInputStream bis = new BufferedInputStream(fis);
IOUtils.copy(bis, tarArchive);
tarArchive.closeArchiveEntry();
bis.close();
} else if (sourceFile.isDirectory()) {
tarArchive.closeArchiveEntry();
for (File f : sourceFile.listFiles()) {
addFilesToTarGZ(f.getAbsolutePath(), fileName + File.separator, tarArchive);
}
}
}
public static void changeFileName(String oldFileName,String newFileName)throws IOException{
File oldName = new File(oldFileName);
File newName = new File(newFileName);
if (newName.exists()) {
throw new java.io.IOException("file exists");
}
if(oldName.renameTo(newName)) {
System.out.println("已重命名");
} else {
System.out.println("Error");
}
}
public static void unTarGz(String sourceFile,String outputDir) throws IOException{
TarInputStream tarIn = null;
File file = new File(sourceFile);
try{
tarIn = new TarInputStream(new GZIPInputStream(
new BufferedInputStream(new FileInputStream(file))),
1024 * 2);
createDirectory(outputDir,null);
TarEntry entry = null;
while( (entry = tarIn.getNextEntry()) != null ){
if(entry.isDirectory()){
entry.getName();
createDirectory(outputDir,entry.getName());
}else{
File tmpFile = new File(outputDir + "/" + entry.getName());
createDirectory(tmpFile.getParent() + "/",null);
OutputStream out = null;
try{
out = new FileOutputStream(tmpFile);
int length = 0;
byte[] b = new byte[2048];
while((length = tarIn.read(b)) != -1){
out.write(b, 0, length);
}
}catch(IOException ex){
throw ex;
}finally{
if(out!=null)
out.close();
}
}
}
}catch(IOException ex){
throw new IOException("解压归档文件出现异常",ex);
} finally{
try{
if(tarIn != null){
tarIn.close();
}
}catch(IOException ex){
throw new IOException("关闭tarFile出现异常",ex);
}
}
}
public static void createDirectory(String outputDir,String subDir){
File file = new File(outputDir);
if(!(subDir == null || subDir.trim().equals(""))){
file = new File(outputDir + "/" + subDir);
}
if(!file.exists()){
if(!file.getParentFile().exists())
file.getParentFile().mkdirs();
file.mkdirs();
}
}