2010-02-22 117 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