Translate

How to Zip your Test Automation Reports using Java

We can use this Class in Selenium to Zip your Test Automation Reports and mail them to anyone. We can use this for adding a file or a folder to the archive list. This Class is based on Java util & io Packages. In general we use Jenkins or some CI tool by adding plugins for auto mailing.



Here i am sharing the snippet.


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils
{

private List<String> fileList;
private static final String OUTPUT_ZIP_FILE = “D:\\ReportsFolder.zip”;

// SourceFolder path
private static final String SOURCE_FOLDER = “F:\\Workspace\\AutomationsReports”;

public ZipUtils()
{
fileList = new ArrayList<String>();
}

public static void main(String[] args)
{
ZipUtils appZip = new ZipUtils();
appZip.generateFileList(new File(SOURCE_FOLDER));
appZip.zipIt(OUTPUT_ZIP_FILE);
}

public void zipIt(String zipFile)
{
byte[] buffer = new byte[1024];
String source = “”;
FileOutputStream fos = null;
ZipOutputStream zos = null;
try
{
try
{
source = SOURCE_FOLDER.substring(SOURCE_FOLDER.lastIndexOf(“\\”) + 1, SOURCE_FOLDER.length());
}
catch (Exception e)
{
source = SOURCE_FOLDER;
}
fos = new FileOutputStream(zipFile);
zos = new ZipOutputStream(fos);

System.out.println(“Output to Zip : ” + zipFile);
FileInputStream in = null;

for (String file : this.fileList)
{
System.out.println(“File Added : ” + file);
ZipEntry ze = new ZipEntry(source + File.separator + file);
zos.putNextEntry(ze);
try
{
in = new FileInputStream(SOURCE_FOLDER + File.separator + file);
int len;
while ((len = in.read(buffer)) > 0)
{
zos.write(buffer, 0, len);
}
}
finally
{
in.close();
}
}

zos.closeEntry();
System.out.println(“Folder successfully compressed”);

}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{
zos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

public void generateFileList(File node)
{

// add file only
if (node.isFile())
{
fileList.add(generateZipEntry(node.toString()));

}

if (node.isDirectory())
{
String[] subNote = node.list();
for (String filename : subNote)
{
generateFileList(new File(node, filename));
}
}
}


private String generateZipEntry(String file)
{
return file.substring(SOURCE_FOLDER.length() + 1, file.length());
}
}

Simple, now check at the path you've mentioned. There will a zip file

No comments:

Post a Comment