top of page
Search

Mastering Zip File Management in SuiteScript


Oracle NetSuite Mastering Zip File Management in SuiteScript

Developing integrations within NetSuite often presents challenges. Recently, I encountered a significant hurdle while attempting to process an API response: all data was contained within a CSV file compressed in a zip archive and was inaccessible using NetSuite's N/compress SuiteScript module. In this article, we will review the capabilities of the N/compress module and explore a third-party library that will come in handy in similar circumstances.


Content:

N/Compress module

The N/compress module was incorporated in the 2020.2 release and has three different methods:

  • compress.gzip(options) Enables compression using customizable levels (0 to 9).

  • compress.gunzip(options) Facilitates decompression of gzip-compressed files.

  • compress.createArchiver() Allows creation of file archives (supports formats like CPIO, TAR, TBZ2, TGZ, and ZIP)


This module can be useful when working with an API that requires a zip file to be uploaded, or when trying to maximize space efficiency in your file cabinet. However, if we need to work with the files in an archive, we’ll need to use a different library.


Using JSZip-Sync in SuiteScript

JSZip-Sync is a Javascript library created for creating, reading and editing .zip files in a simple way. 


To use this library in suitescript, follow these steps

  • Download the jszip.min.js file from the npm site.

  • Upload the file in your script folder

  • Add the module in your define object ('./jszip.min.js')

An example of a function that decompress an existing zip file

const getZipContents = (fileId) => {
    try {
        if(fileId){
            /** First, load the zip file and get the file contents */
            let objZippedFile = file.load({
                id: fileId
            })
            let strZippedFileContents = objZippedFile.getContents()
            let arrUnzippedFiles = [];

            var new_zip = new JSZip();

            new_zip.sync(function () {
                let zip = new_zip.loadAsync(strZippedFileContents, {base64:true})._result;
                /** Iterate through all the files in the archive */
                Object.keys(zip.files).forEach(fileName => {
                    /** Check the documentation for a complete list of the different result types for this async function */
                    let strFileContent = zip.file(fileName).async("string");.

                    arrUnzippedFiles.push({
                        file_name: fileName,
                        file_contents: strFileContent._result
                    })
                })
            })
            return arrUnzippedFiles;
        }
  } catch(error){
       log.error ('Unable to unzip file id ' + intZipFileId, error)
  }
}

Conclusion

While the N/compress module in NetSuite excels in file compression and archiving, its limitations in directly accessing and manipulating files within zip archives can present challenges for specific integration needs. In contrast, integrating the JSZip-Sync library offers an effective alternative, simplifying the creation, extraction, and modification of zip files within SuiteScript environments. Both modules provide distinct advantages depending on developers' requirements. Whether choosing N/compress or JSZip-Sync, developers can improve their applications' ability to handle zip files efficiently, leading to smoother data management and integration processes. This flexibility allows us to tailor our solutions to specific integration challenges effectively.


By Caro G.

bottom of page