Gzip压缩和解压文件工具类

来源:互联网 发布:汤普森 防守 知乎 编辑:程序博客网 时间:2024/05/17 20:15
package com.bsteel.marketcondition.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;


import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.apache.tools.tar.TarOutputStream;


/**
 * 用于压缩和解压的工具类
 * 
 */
public class GZipUtil
{
/**
* 解压.tar.gz文件

* @param filePathAndName
*            被解压的文件路径+文件名,例如D:\\orcalelog\\newexchange\\weiGang\\upload\\bsteel.tar.gz

* @param unTargzPath
*            解压后存放的路径,例如D:\\orcalelog\\newexchange\\weiGang\\upload
* @throws IOException
* @throws FileNotFoundException
*/
public static void unTargzFile(String filePathAndName, String unTargzPath) throws IOException, FileNotFoundException
{
// 解压路径
File unTargzFile = new File(unTargzPath);
// 被解压文件
File targzFile = new File(filePathAndName);


// 不存在目录则创建解压目录
if (!unTargzFile.isDirectory())
{
unTargzFile.mkdirs();
}


// 建立tarzg压缩文件输入流
TarInputStream tarIn = new TarInputStream(new GZIPInputStream(new FileInputStream(targzFile)));
TarEntry entry = null;


// 遍历需要压缩的文件
while ((entry = tarIn.getNextEntry()) != null)
{
// 解压后的文件路径+文件名
File tmpFile = new File(unTargzPath + File.separator + entry.getName());


OutputStream out = new FileOutputStream(tmpFile);


int length = 0;


byte[] b = new byte[1024];


// 解压文件
while ((length = tarIn.read(b)) != -1)
{
out.write(b, 0, length);
}


out.flush();
out.close();
}
tarIn.close();
}


/**
* 压缩.tar.gz文件

* @param filePath
*            需要要锁的文件路径,例如D:\\orcalelog\\newexchange\\weiGang\\upload

* @param targetPathAndName
*            压缩后存放的路径+文件名,例如D:\\orcalelog\\newexchange\\weiGang\\upload\\bsteel.tar.gz
* @throws IOException
* @throws FileNotFoundException
*/
public static void targzFile(String filePath, String targePathAndName) throws IOException, FileNotFoundException
{
// 压缩的结果文件名不符合标准
if(!targePathAndName.endsWith(".tar.gz"))
return;


// 压缩tar.gz文件需要做两个步骤
// 1. 将所需要的压缩文件先打包成.tar文件
// 2. 将步骤1生成的.tar文件压缩成.gz文件

// 获得压缩的.tar文件名
// 例如:输出为D:\\bsteel.tar.gz则输出为D:\\bsteel.tar
String tarPathAndName = targePathAndName.substring(0, targePathAndName.lastIndexOf("."));
File tarFile = new File(tarPathAndName);
// 定义缓存单位为1024
int BUFFER = 1024;
// 长度
int len;
// 缓存
byte data[] = new byte[BUFFER];
// 步骤1:打包成.tar文件
TarOutputStream tos = new TarOutputStream(new FileOutputStream(tarPathAndName));
// 获得需要压缩文件夹下面所有文件
File files[] = new File(filePath).listFiles();

// 遍历输入文件夹下所有文件
for (File t : files)
{
// ant会自己先创建XX.tar文件,所以你需要的.tar文件也会被打包进去,此文件的文件大小为0
if(tarFile.getName().equals(t.getName()))
continue;

TarEntry te = new TarEntry(t);
te.setName(t.getName());
tos.putNextEntry(te);
// 输入流
InputStream inputStream = new FileInputStream(t);

// 打包成.tar文件
while ((len = inputStream.read(data)) != -1)
{
tos.write(data, 0, len);
}
tos.closeEntry();
inputStream.close();
}
// 关闭流
tos.finish();
tos.flush();
tos.close();


// 步骤2:压缩成GZ文件
GZIPOutputStream gos = new GZIPOutputStream(new FileOutputStream(targePathAndName));
InputStream is = new FileInputStream(tarPathAndName);

// 开始压缩
while ((len = is.read(data, 0, BUFFER)) != -1)
{
gos.write(data, 0, len);
}

// 关闭流
gos.finish();
gos.flush();
gos.close();
is.close();


// 删除临时的.tar文件
tarFile.delete();
}
}
原创粉丝点击