Unzip the Zip folder using Selenium

package com.test;

import com.aventstack.extentreports.Status;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class UnZip {

    private static WebDriver driver;

    public static String ZipFunction(String Zipfilename) throws Exception
    {
        String home = System.getProperty("user.home");
        File file = new File(Absolute path of the output of extracted zip file); //Creating a output folder 
        boolean fileexists = false;
        if (!file.exists()) {
            fileexists = file.mkdirs();
        }
        if (fileexists)
            System.out.println("Directory successfully created");
        else
            System.out.println("Failed to create directory");
        
        File zipFile = new File("Absolute path of input Zip file");
        ZipInputStream zis = new ZipInputStream(new FileInputStream(zipFile));
        ZipEntry ze = zis.getNextEntry();
        String XMLFileName = ze.getName().toString();
        while (ze != null) {
        //String entryName = ze.getName();
        System.out.print("Extracting " + XMLFileName + " -> " + outputFolder + File.separator + XMLFileName + "...");
        File f = new File(outputFolder + File.separator + XMLFileName);
        //create all folder needed to store in correct relative path.
        f.getParentFile().mkdirs();
        FileOutputStream fos = new FileOutputStream(f);
        int len;
        byte buffer[] = new byte[1024];
        while ((len = zis.read(buffer)) > 0) 
        {
         fos.write(buffer, 0, len);
        }
        fos.close();
        System.out.println("OK!");
        ze = zis.getNextEntry();
        }
        zis.closeEntry();
        zis.close();
        String XMLFile = outputFolder+File.separator+XMLFileName;
        return XMLFile;      
    }
    
    /*public static String XMLFile(String value) throws Exception
    {
        if(value != null){
            System.out.println("-------------------------------------");
            System.out.println(value);
            System.out.println("-------------------------------------");
            return value;
        } else {
            return null;
        }
       
    }*/
}
Advertisements

One thought on “Unzip the Zip folder using Selenium

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s