Sidebar

How do I extract from a zip file?

0 votes
2.2K views
asked Aug 3, 2016 by marc-m-9513 (570 points)
I want to take the zip file  and unzip and parse it to send the content data to another destination?

2 Answers

+1 vote
 
Best answer

Here's a generic handling of an entire zip file. It processes each file at the byte level, so you can get at text files, as well binary (images, PDF's, etc.) files. This code unzips the entire zip file, creating and including any subdirectories it encounters along the way.

  • The zipFilePath is the zip file pathname as a string.
  • The unzipPath is the unzip pathname as a string.
function unzipFile(zipFilePath, unzipPath) {
   var zipFile = null;
   
   try {
      zipFile = new java.util.zip.ZipFile(zipFilePath);
      var entries = zipFile.entries();
      var bytes = new Packages.java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 1024);
      
      while (entries.hasMoreElements()) {
         var zipEntry = new java.util.zip.ZipEntry(entries.nextElement());
         var fileFromZipEntry = new java.io.File(unzipPath + java.io.File.separator + zipEntry.getName());
         
         //zipEntry reports / as separator, even on Windows, so we're not using java.io.File.separator in the following test
         if (StringUtils.endsWith(zipEntry.getName(), '/')) {
            fileFromZipEntry.mkdirs();
            continue;
         } else {
            var inputStream = null;
            var outputStream = null;
            var length = -1;
            
            try {
               inputStream = zipFile.getInputStream(zipEntry);
               outputStream = new java.io.FileOutputStream(fileFromZipEntry);
               
               while ((length = inputStream.read(bytes)) >= 0) {
                  outputStream.write(bytes, 0, length);
               }
            } finally {
               if (inputStream !== null) {
                  inputStream.close();
                  inputStream = null;
               }
               
               if (outputStream !== null) {
                  outputStream.close();
                  outputStream = null;
               }
            }
         }
      }
   } catch (err) {
      qie.error('Failed to unzip file \'' + zipFilePath + '\': ' + err);
   } finally {
      if (zipFile !== null) {
         zipFile.close();
         zipFile = null;
      }
   }
}

 

A sample call to this function would look like:

unzipFile('c:\\temp\\compressed\\myZipFile.zip', 'c:\\temp\\decompressed\\myZipFile');

 

answered Apr 10, 2018 by david-f-5427 (1,600 points)
selected Apr 10, 2018 by brandon-w-8204
0 votes

Here is an example of extracting all files that end in xml from a zip source file.

// convert the source bytes to a ByteArrayInputStream
var byteInputStream = new java.io.ByteArrayInputStream(source.getBytes());

​// use the ByteArrayInputStream to create a new ZipInputStream
var inputStream = new java.util.zip.ZipInputStream(byteInputStream);

​// cycle the objects inside of the ZipInputStream
var zipEntry = inputStream.getNextEntry();
while (zipEntry !== null) {
  // if the fileName ends in xml, then we will process this file
  if (StringUtils.endsWithIgnoreCase(zipEntry.getName(), 'xml')) {

​    // read the zipEntry into a "UTF-8" encoded string
    var xmlDocument = org.apache.commons.io.IOUtils.toString(inputStream, "UTF-8");

    // NOTE: to read the contents into a ByteArray instead of a string (in case you are extracting an image)
​    // you can do the followin
​    // var fileBytes = org.apache.commons.io.IOUtils.toByteArray(inputStream);

​    // parse the XML string into a new XML Message model
    var xmlMessage = qie.parseXMLString(xmlDocument, "UTF-8");

​    // todo: do something with this message model
    // qie.spawnNewMessage(xmlMessage); OR qie.addInboundMessage(xmlMessage.toString(), zipEntry.getName());
  }

​  // get next file from the source zip file
  zipEntry = inputStream.getNextEntry();
}

answered Nov 7, 2016 by ben-s-7515 (12,640 points)
...