top of page
Search

Mastering Zip File Management in SuiteScript

  • Jul 29, 2024
  • 3 min read

Updated: 6 days ago


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 Carolina Gorriarán, Solutions Architect



Interested in learning more?






FAQS

What is SuiteScript?

SuiteScript is NetSuite's JavaScript-based scripting framework that allows developers to customize, automate, and extend the platform's functionality, including integrations with external systems and APIs.

What is NetSuite's N/compress module?

The N/compress module is a native SuiteScript tool, introduced in the 2020.2 release, that enables file compression and decompression using gzip, as well as the creation of file archives in formats such as ZIP, TAR, and TGZ.

What is the limitation of the N/compress module when working with ZIP files?

The N/compress module can create and compress ZIP archives, but it does not allow developers to directly access or manipulate the individual files inside an existing ZIP, which limits its usefulness in certain integration scenarios.

What is JSZip-Sync and why is it used in SuiteScript?

JSZip-Sync is a third-party JavaScript library that enables the creation, reading, and editing of ZIP files in a straightforward way. In SuiteScript, it is used as an alternative to the N/compress module when access to the contents inside a ZIP file is required.

How is JSZip-Sync integrated into a SuiteScript script?

To use JSZip-Sync in SuiteScript, developers must download the jszip.min.js file from the npm site, upload it to the scripts folder in NetSuite's File Cabinet, and add it as a dependency in the script's define object.

When should you use N/compress vs. JSZip-Sync?

The N/compress module is best suited for compressing files or optimizing storage space in NetSuite's File Cabinet. JSZip-Sync is the better choice when you need to read, extract, or modify individual files within an existing ZIP archive.

What is NetSuite's File Cabinet?

The File Cabinet is NetSuite's built-in file storage repository where documents, scripts, images, and other files needed for the platform's operations and integrations can be stored and managed.

Why is ZIP file handling important in NetSuite integrations?

Many external APIs deliver their responses as CSV files compressed inside a ZIP archive. Knowing how to handle these files directly from SuiteScript allows developers to process that data without requiring manual intermediate steps.


bottom of page