您的位置:首页 > 其它

ZipHelper压缩工具类

2015-07-09 00:00 155 查看
摘要: 解压tar.gz和zip文件

public final class ZipHelper {
private static final Logger logger = LoggerFactory.getLogger(ZipHelper.class);
private ZipHelper() {}

/**
* 方法说明:<br>
* 解压<tt>tar.gz</tt>类型文件并写入<tt>outputDir</tt>对应的目录
*
* @param zipFileName tar.gz格式的压缩文件
* @param outputDir 输入文件目录
* @throws Exception
*/
public static void unZipTargz(String zipFileName, String outputDir) throws Exception {
ArchiveInputStream in = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
byte[] b = null;
File file = null;
char last = outputDir.charAt(outputDir.length()-1);
String dir = last == '/' || last == '\\' ? outputDir : outputDir+'/';
try {
GZIPInputStream gis = new GZIPInputStream(new BufferedInputStream(new FileInputStream(zipFileName)));
in = new ArchiveStreamFactory().createArchiveInputStream("tar", gis);
TarArchiveEntry entry = null;
while (null != (entry = (TarArchiveEntry) in.getNextEntry())) {
String name = entry.getName();
file = new File(dir+name);
if(name.charAt(name.length()-1) == '/') {
if (!file.exists()) {
try{
file.mkdirs();
}catch(SecurityException e) {
throw new IOException("[ It doesn't own the create directory rights. ]");
}
}
} else {
try{
file.createNewFile();
}catch(SecurityException e) {
throw new IOException("[ It doesn't own the create directory rights. ]");
}
try {
bis = new BufferedInputStream(in);
bos = new BufferedOutputStream(new FileOutputStream(file));
b = new byte[bis.available()];
bis.read(b);
bos.write(b);
bos.flush();
} finally {
try {
if(null != bos) { bos.close(); }
} catch (IOException e) {
}
}
}
}
} finally {
try {
if(null != bis) { bis.close(); }
if(null != in) { in.close(); }
} catch (IOException e) {
}
}
}

/**
* 方法说明:<br>
* 解压<tt>zip</tt>类型文件并写入<tt>outputDir</tt>对应的目录
*
* @param zipFileName zip格式的压缩文件
* @param outputDir 输入文件目录
* @throws Exception
*/
public static void unZip(String zipFileName, String outputDir) throws Exception {
File zipFile = new File(zipFileName);
File pathFile = new File(outputDir);
if(!pathFile.exists()){
try{
pathFile.mkdirs();
}catch(SecurityException e) {
throw new IOException("[ It doesn't own the create directory rights. ]");
}
}
char last = outputDir.charAt(outputDir.length()-1);
String dir = last == '/' || last == '\\' ? outputDir : outputDir+'/';

InputStream in = null;
OutputStream out = null;
ZipFile zip =  null;
try{
zip = new ZipFile(zipFile);
for(Enumeration<ZipArchiveEntry> entries = zip.getEntries();entries.hasMoreElements();){
ZipArchiveEntry entry = (ZipArchiveEntry)entries.nextElement();
String zipEntryName = entry.getName();
in = zip.getInputStream(entry);
String outPath = dir+zipEntryName;
//判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if(!file.exists()){
try{
file.mkdirs();
} catch (SecurityException e) {
throw new IOException("[ It doesn't own the create directory rights. ]");
}
}
//判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if(new File(outPath).isDirectory()){
continue;
}

out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while((len=in.read(buf1))>0){
out.write(buf1,0,len);
}

}
}finally{
try {
if(null != in) { in.close(); }
if(null != out) { out.close(); }
if(null != zip){zip.close();}
}catch(Exception ex){
logger.info("正常释放资源");
}
}
}

public static boolean isPics(String filename)
{
boolean flag = false;

if(filename.endsWith(".jpg") || filename.endsWith(".gif")  || filename.endsWith(".bmp") || filename.endsWith(".png"))
flag = true;

return flag;
}

public static void main(String[] args){
String filePath = "D:\\work\\acquirer\\recon\\20130904\\b2c\\UPOP.zip";
String outputDir = "D:\\work\\acquirer\\recon\\20130904\\b2c\\UPOP";//解压到文件
try {
ZipHelper.unZip(filePath, outputDir);
} catch (Exception e) {
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: