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) { } } }
相关文章推荐
- Quick3.3, CocosIDEV1.2.0调试lua代码
- 企业级移动互联并非把应用搬到移动端这么简单
- C#webBrowser使用代理服务器的方法winform
- 中小企业普遍存在的内部沟通现状
- Percona Xtrabackup快速备份MySQL
- 使用autotools自动生成Makefile并在此之上使用dh-make生成可发布的deb程序包
- 图灵机器人
- 基于Zookeeper的分布式共享锁
- spring web 4.1处理json
- springmvc中@ResponseBody和@RequestBody的使用
- servlet线程安全问题
- 基础学习的重要性
- python
- js
- python
- python
- python 3.x和2.x区别
- 一个程序员对互联网的思考
- XML和JavaBean之间的互转---XStream
- VIEWGOOD(远古)建立区域性教育资源公共平台的解决方案