skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Saturday, September 15, 2012

Zip File Extraction | Java Example

Posted by Admin at 12:52 AM – 0 comments
 

It will exrect the contents of the compressed zip file and write the contents in a seperate file.


package Alltest; // package name where u create this class

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class xtractZipFile {

   
     // Extracts a zip file
    
    public void extractZipFile() {
        
        try {
            String zipFileName = "C:/Documents and Settings/313915/Desktop/Hi/Parent Satelitte report-FS.zip";
// Path of your compressed file
            String extractedFileName = "C:/Documents and Settings/313915/Desktop/Hi/extracted.doc"; 
// path where you want the file to be written
            
            //Create input and output stream
            ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFileName));
            OutputStream os = new FileOutputStream(extractedFileName);
            
            ZipEntry ze;
            
            byte[] buffer = new byte[1024];
            int nrBytesRead;
            
            
            if ((ze = zis.getNextEntry()) != null) {
             System.out.println(ze.getName());
                while ((nrBytesRead = zis.read(buffer)) > 0) {
                 os.write(buffer, 0, nrBytesRead);
                }
            }
                    
            // close the streams
            os.close();
            zis.close();
            System.out.println("File Extracted successfully!!!");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    

    public static void main(String[] args) {
        new xtractZipFile().extractZipFile();
    }
    
}

Labels: Zip File

Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger