Thursday 3 October 2013

How to ZIP files in SELENIUM with an Example

1. How to ZIP a file in SELENIUM

Steps to ZIP(Compare with Code and understand)
1. Create an File object for the SOURCE FOLDER, in which files are present.
2. Create another File object for the destination folder, for ZIP file.
3. Create a ZipOutputStream Object.
4. Create a BufferedInputStream Object.
5. Create a Byte Array Object of size 1000.
6. Create a String Array, and collect all files from SOURCE FOLDER as   
    LIST  using File Object for source folder.
7. Loop through String Array and for each element perform below action.
8. INSIDE FOR LOOP
   1. Get the SOURCE FOLDER path and append each file to the path
       with default size, and put it in BufferedInputStream Object.
   2. Through ZipOutputStream Object, make an ZIP Entry for each File.
   3. Through While Loop read the data bit by bit untill 1000 bytes from
       BufferedInputStream Object and write to ZipOutputStream Object.
      9. INSIDE WHILE LOOP
       1. Read the BufferedInputStream Object.
       2. Write to ZipOutputStream Object.
4. After writing make a Close Entry using ZipOutputStream Object.
5. Flush the data using ZipOutputStream Object.
6. Close the ZipOutputStream Object.
———————————————————————–
try
  {
    File inFolder=new File(“D:\\sel\\framework\\tests”);
    File outFolder=new File(“D:\\sel\\framework\\results\\results.zip”);
 
    ZipOutputStream out = new ZipOutputStream(new  BufferedOutputStream( 
                                                  new FileOutputStream(outFolder)));
   BufferedInputStream in = null;
   byte[] data  = new byte[1000];
   String files[] = inFolder.list();
 
  for (int i=0; i<files.length; i++)
  {
     in = new BufferedInputStream(new FileInputStream
                                               (inFolder.getPath() + “/” + files[i]), 1000);  
     out.putNextEntry(new ZipEntry(files[i])); 
     int count;
              while((count = in.read(data,0,1000)) != -1)
               {
                    out.write(data, 0, count);
               }
              out.closeEntry();
  }
 out.flush();
 out.close();
  }
 catch(Exception e)
 {
  e.printStackTrace();
  }

No comments:

Post a Comment

Angular JS Protractor Installation process - Tutorial Part 1

                     Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...