java压缩文件生成XXX.tar.gz压缩包

生成XXX.tar.gz压缩文件有两种方式,可以先打包后压缩,还可以打包压缩同时进行。

所用到的jar包见附件,若用maven,pom.xml文件内容如下:


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.it.bill</groupId>
<artifactId>bill_project.gi</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<name>bill_project.gi</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.8</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.5</version>
</dependency>
</dependencies>
</project>






package com.bill;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveOutputStream;
import org.apache.commons.compress.compressors.CompressorException;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.CompressorStreamFactory;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.commons.io.FileUtils;

public class TestCompressSnapshotFiles {

public static void main(String[] args) {
try {
String sourceFileDirPath = "C:/opt/webhost/pn/testCompressSnapshotXmlFiles/tempFileDir/";
File sourceFileDir = new File(sourceFileDirPath);

if (sourceFileDir.isDirectory()) {
File[] sourceFileArr = sourceFileDir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".xml");
}
});

/**
* the first way, packaging before they are compressed
*/
String targetFilePath = "C:/opt/webhost/pn/testCompressSnapshotXmlFiles/firstCompressedPackage.tar";
File targetFile = new File(targetFilePath);
File tarFile = packSnapshotXmlFiles(sourceFileArr, targetFile);
compressSnapshotXmlFiles(tarFile);

/**
* the second way, packaging and compression at the same time
*/
targetFilePath = "C:/opt/webhost/pn/testCompressSnapshotXmlFiles/secondCompressedPackage.tar.gz";
targetFile = new File(targetFilePath);
packAndCompressSnapshotXmlFiles(sourceFileArr, targetFile);
} else {
System.err.println("This is not folder: " + sourceFileDirPath);
}

System.out.println("----- Compress snapshot xml files end. ----");
} catch (Exception e) {
e.printStackTrace();
}
}

private static void packAndCompressSnapshotXmlFiles(File[] sourceFileArr,
File targetFile) throws IOException, CompressorException {
FileOutputStream fileOutputStream = null;
CompressorOutputStream gzippedOut = null;
TarArchiveOutputStream taos = null;

try {
fileOutputStream = FileUtils.openOutputStream(targetFile);

gzippedOut = new CompressorStreamFactory().createCompressorOutputStream(
CompressorStreamFactory.GZIP, fileOutputStream);

taos = new TarArchiveOutputStream(gzippedOut);
for (File perFile : sourceFileArr) {
System.out.println(
"current file name: " + perFile.getName() + ", length: "
+ perFile.length() / 1024);
TarArchiveEntry tae = new TarArchiveEntry(perFile, perFile.getName());
taos.putArchiveEntry(tae);
taos.write(FileUtils.readFileToByteArray(perFile));
taos.closeArchiveEntry();
}
} catch (IOException ioe) {
throw ioe;
} catch (CompressorException ce) {
throw ce;
} finally {
if (taos != null) {
try {
taos.close();
} catch (IOException e) {
}
}

if (gzippedOut != null) {
try {
gzippedOut.close();
} catch (IOException e) {
}
}
}

}

private static void compressSnapshotXmlFiles(File tarFile) throws IOException {
File target = new File(tarFile.getAbsolutePath() + ".gz");
FileInputStream fis = null;
GZIPOutputStream gzipOs = null;

try {
fis = new FileInputStream(tarFile);

gzipOs = new GZIPOutputStream(new FileOutputStream(target));

/*byte[] buffer = new byte[1024];
int n = -1;
while((n = fis.read(buffer)) != -1) {
gzipOs.write(buffer, 0, n);
}*/
IOUtils.copy(fis, gzipOs);

// delete the template tar file
tarFile.deleteOnExit();
} catch (FileNotFoundException fnfe) {
System.err.println("file not found: " + tarFile.getAbsolutePath());
throw fnfe;
} catch (IOException ioe) {
System.err.println("create gizp output stream fail");
throw ioe;
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}

if (gzipOs != null) {
try {
gzipOs.close();
} catch (IOException e) {
}
}
}


}

private static File packSnapshotXmlFiles(File[] sourceFileArr, File targetFile) throws IOException {
FileOutputStream fos = null;
TarArchiveOutputStream taos = null;
try {
fos = new FileOutputStream(targetFile);

taos = new TarArchiveOutputStream(fos);

for (File perFile : sourceFileArr) {
System.out.println("current file name: " + perFile.getName() + ", length: " + perFile.length()/1024);
taos.putArchiveEntry(new TarArchiveEntry(perFile, perFile.getName()));
IOUtils.copy(new FileInputStream(perFile), taos);
//taos.write(FileUtils.readFileToByteArray(perFile));
taos.closeArchiveEntry();
}

//This line is very import, otherwise the compressed file can't successfully decompressed
taos.finish();
} catch (FileNotFoundException fnfe) {
System.err.println("File not found: " + targetFile.getAbsolutePath());
throw fnfe;
} catch (IOException ioe) {
System.err.println("put archive entry fail.");
throw ioe;
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}

if (taos != null) {
try {
taos.close();
} catch (IOException e) {
}
}
}

return targetFile;
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值