2010-02-22 158 views
0

如何從使用JAVA庫的壓縮文件中提取數據?有沒有解壓縮庫,我得到的文件和操縱它?如何提取讀取壓縮文件?

+0

刪除標籤 「信息提取」:它有**什麼**做壓縮(見http://en.wikipedia.org/wiki/Information_extraction)。 – MaD70 2010-03-07 17:41:51

回答

5

您可以使用 「java.util.zip」 包。

請參閱this article by Sun

+0

其他強大的選項是TrueZip和7-Zip-JBinding – 2010-02-23 16:12:30

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); 
    } 
    } 
} 
+0

原始問題是詢問zip,而不是gzip。解壓zip文件的過程是不同的;請參閱NomNom答案中引用的文章。 – rob 2010-02-22 23:14:25

+0

我引用的包的鏈接適用於zip和gzip,對於gzip示例很抱歉。 – 2010-02-22 23:27:22

+1

我有點好笑,所有的消息都在「準備」消息的同一行打印「已打開」,「已轉移」和「已關閉」......當你實際上沒有做任何消息時,直到後來^ _^ – 2010-02-23 04:34:28