繁体   English   中英

如何使用 java 解压缩压缩(zip)文件

[英]How do i decompress a compressed(zip) file using java

我们如何解压缩压缩文件(.zip)。请告诉我一种解压缩文件的方法

解决方案 - 使用 BufferedInputStream 并将其交给 ZipInputStream 以读取压缩文件

FileInputStream fis;            
byte[] buffer = new byte[1024];                                                   
try {                                                                         
fis = new FileInputStream(zipFilePath);                            
ZipInputStream zis = new ZipInputStream(fis);                                     
ZipEntry ze = zis.getNextEntry();                                               
while(ze != null){                                                    
String fileName = ze.getName();                                                       
File newFile = new File(destDir + File.separator + fileName);
System.out.println("Unzipping to "+newFile.getAbsolutePath());

很简单,您可以这样做:

    import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.InflaterInputStream;

//Uncompressing a file using an InflaterInputStream
class unzip
{
    public static void main(String[] args) throws IOException {
        //assign Input File : file2 to FileInputStream for reading data
        FileInputStream fis=new FileInputStream("file2");

        //assign output file: file3 to FileOutputStream for reading the data 
    FileOutputStream fos=new FileOutputStream("file3");
    
    //assign inflaterInputStream to FileInputStream for uncompressing the data
    InflaterInputStream iis=new InflaterInputStream(fis);
    
    //read data from inflaterInputStream and write it into FileOutputStream
    int data;
    while((data=iis.read())!=-1)
    {
        fos.write(data);
    }
    
    //close the files
    fos.close();
    iis.close();
    
}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM