Chinaunix首页 | 论坛 | 博客
  • 博客访问: 335258
  • 博文数量: 117
  • 博客积分: 650
  • 博客等级: 中士
  • 技术积分: 738
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-21 13:07
文章分类

全部博文(117)

文章存档

2014年(2)

2013年(2)

2012年(112)

2010年(1)

分类: Java

2012-01-05 20:59:04

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;

import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;

public class UnZip {
 public static  void visitTARGZ(File targzFile) throws IOException {
  FileInputStream fileIn = null;
  BufferedInputStream bufIn = null;
  GZIPInputStream gzipIn = null;
  TarArchiveInputStream taris = null;
  try {
   fileIn = new FileInputStream(targzFile);
   bufIn = new BufferedInputStream(fileIn);
   gzipIn = new GZIPInputStream(bufIn); // first unzip the input file
             // stream.
   taris = new TarArchiveInputStream(gzipIn);

   TarArchiveEntry entry = null;
   while ((entry = taris.getNextTarEntry()) != null) {
    if (entry.isDirectory())
     continue;
    // configure(taris, ((TarArchiveEntry) entry).getFile());
    // //process every entry in this tar file.
    System.out.println(entry);
    File file = entry.getFile();
//    System.out.println(file.getName());
    System.out.println(entry.getSize());
//    FileInputStream fo = new FileInputStream(file);
    
    byte[] b = new byte[(int) entry.getSize()];
//    fo.read(b);
    taris.read(b);
    taris.read(b, 0, (int) entry.getSize());
    System.out.println(b.length);
    System.out.println(b);
   }
  } finally {
   taris.close();
   gzipIn.close();
   bufIn.close();
   fileIn.close();
  }
 }
 
 public static void main(String[] args) {
  try {
   visitTARGZ(new File("d://release_package.tar.gz"));
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

阅读(13337) | 评论(2) | 转发(0) |
给主人留下些什么吧!~~

horse2852012-03-20 09:59:32

估计使用的jar不同!

3980636842012-03-19 12:29:21

run:
Exception in thread "main" java.lang.NullPointerException
        at UnZip.visitTARGZ(UnZip.java:43)
        at UnZip.main(UnZip.java:55)
Java Result: 1
为什么会有异常,亲