-3

How can we decompress a compressed file(.zip).Please tell me a way to decompress a file

Saran Raj
  • 86
  • 4
  • 1
    Have you tried to read javadoc for java.util.zip package? https://docs.oracle.com/javase/8/docs/api/java/util/zip/package-summary.html – MNEMO May 25 '22 at 01:40
  • Possibly related: [How to extract specific file in a zip file in java](https://stackoverflow.com/q/32179094), [What is a good Java library to zip/unzip files?](https://stackoverflow.com/q/9324933), https://mkyong.com/java/how-to-decompress-files-from-a-zip-file/ – Pshemo May 25 '22 at 01:42

2 Answers2

0

Solution - Using BufferedInputStream and give it to the ZipInputStream to read the zipped file

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());
Saran Raj
  • 86
  • 4
-1

It is simple you can do it like this:

    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();
    
}

}

fasf
  • 1