如何提取读取压缩文件?

时间:2010-02-22 22:58:04

标签: java zip

如何使用JAVA库从压缩文件中提取数据?是否有一个解压缩的库,我得到文件并操纵它?

3 个答案:

答案 0 :(得分:5)

您可以使用“java.util.zip”包。

请参阅this article by Sun

答案 1 :(得分:2)

http://java.sun.com/j2se/1.4.2/docs/api/java/util/zip/package-summary.html http://www.roseindia.net/java/beginners/JavaUncompress.shtml

import java.util.zip;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class JavaUncompress{
public static void main(String args[]){
try{
  //To Uncompress GZip File Contents we need to open the gzip file.....
  if(args.length<=0){
    System.out.println("Please enter the valid file name");
  }
  else{
    String inFilename = args[0];
    System.out.println("Opening the gzip file.......................... :  opened");


    ZipInputStream zipInputStream = null;
    FileInputStream fileInputStream = null;
    zipInputStream = new ZipInputStream(new 

FileInputStream(inFilename));
    System.out.println("Opening the output file............ : opened");
    String outFilename = inFilename +".pdf";
    OutputStream out = new FileOutputStream(outFilename);
    System.out.println("Transferring bytes from the 

compressed file to the output file........:
     Transfer successful");
    byte[] buf = new byte[1024];  //size can be 

//changed according to programmer's need.
    int len;
    while ((len = zipInputStream.read(buf)) > 0) {
      out.write(buf, 0, len);
    }
    System.out.println("The file and stream is ......closing.......... : closed"); 
    zipInputStream.close();
    out.close();
      }
    }
    catch(IOException e){
  System.out.println("Exception has been thrown" + e);
    }
  }
}

答案 2 :(得分:1)

Commons Compress http://commons.apache.org