Service Virtualization

  • 1.  Is there a way to extract files from a zip file by using Devtest

    Posted Oct 05, 2016 02:42 PM

    Hi,

    I am trying to access a zip file in some location e.g. {LISA_RELATIVE_PROJECT_PATH}/Data and extract the contents. Can this done using any step in Devtest?

    Thanks!



  • 2.  Re: Is there a way to extract files from a zip file by using Devtest

    Broadcom Employee
    Posted Oct 05, 2016 03:28 PM

    What version of DevTest are you working with?

    There are a couple of options to work with zip files:

       java.util.zip.ZipFile available with Core Java or net.lingala.zip4j.core.ZipFile available in  zip4j-<version>.jar & in your DevTestHome/lib/shared folder.

    You can create a JSR 223 step to instantiate a suitable class from above & extract the contents.



  • 3.  Re: Is there a way to extract files from a zip file by using Devtest

    Posted Oct 06, 2016 12:02 PM

    Thanks Prem for responding back. I am using Devtest 9.5.1 and do see that jar in lib/shared. But I am unable to figure out which method I may use to get this done. Do you have any sample script performing this?



  • 4.  Re: Is there a way to extract files from a zip file by using Devtest
    Best Answer

    Posted Oct 20, 2016 12:42 AM

    Hi @Rahul_THD,

     

    There's some sample of Java code from Oracle:

    Compressing and Decompressing Data Using Java APIs 

     

    I have a snippet of Beanshell script that works using java.util.zip package. Take note that the "C:/Temp" and the test.zip file must exist in order for the script to work.

     

    import java.io.BufferedInputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;

    filename = "{{LISA_RELATIVE_PROJ_ROOT}}/Data/test.zip";
    outputDir = "C:/Temp";
    FileInputStream fis = new FileInputStream(filename);
    ZipInputStream zin = new ZipInputStream(new BufferedInputStream(fis));

    ZipEntry entry;
    while((entry = zin.getNextEntry()) != null) {
    _logger.info(String.format("Entry: %s len %d added %TD", entry.getName(),

        entry.getSize(), new Date(entry.getTime())));
    String outpath = outputDir + File.separator + entry.getName();
    FileOutputStream fos = new FileOutputStream(outpath);
    return true;
    len = 0;
    while((len = zin.read(buffer)) > 0) {
    fos.write(buffer, 0, len);
    }
    }
    fos.close();