关于Java:如何提取读取的压缩文件?

How to extract read a zipped file?

如何使用JAVA库从压缩文件中提取数据?有没有可以解压缩的库,我可以获取文件并对其进行操作吗?


您可以使用" java.util.zip "包。

请参见Sun(现为Oracle)的这篇文章。


https://docs.oracle.com/javase/1.5.0/docs/api/java/util/zip/package-summary.html
http://www.roseindia.net/java/beginners/JavaUncompress.shtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);
    }
  }
}


常用压缩
http://commons.apache.org