NetSuite的gzip解压缩在接口开发中的应用

NetSuite的gzip解压缩在接口开发中的应用, 常见于接口开发中大数据量的传输, 用gzip压缩和解压缩功能提供传输效率

有什么用

可以看到NetSuite的gzip解压缩在接口开发中的应用

接口改造

接口优化

怎么用

两端的开发人员协调, 将不同系统中数据传输, 在接口端 和 主发送端口 将数据进行解压缩和压缩工作,

从而达到: 提高数据传输速率, 减少数据传输量, 另个角度加密数据; 对大量数据传输的情形有显著的效果提升作用

相关内容

NetSuite实现方法

NetSuite的SuiteScript在2023年加入的 N/compress Module模块, 实现了服务端对数据进行压缩和解压缩的buildin支持.

NetSuite发送往第三方系统

将NS中的数据/报表, 通过JSON组织成string字符串的类型, 然后compress.gzip进行压缩(压缩级别可选: 1-9),

最后通过payload把数据post到第三方系统的接口节点上. NS端实例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
....
var objTxtFile = file.create({
fileType: 'PLAINTEXT',
name: 'file.txt',
encoding: file.Encoding.UTF8,
contents: JSON.stringify(arrInvUpdateData)
});
var gzippedFile = compress.gzip({
file: objTxtFile,
level: 4
});
var strGzipText = gzippedFile.getContents();

let objBody = {
"messageId": new Date().getTime().toString(),
"route": "goodsInventoryUpdate",
"payload": strGzipText
};

...

第三方系统发到NetSuite接口

从第三方系统发送到NetSuite的接口; RESTlet; NetSuite端对接收的数据进行解压缩; 然后处理相应的业务需求

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
var objTxtFile = file.create({
fileType: file.Type.GZIP,
name: 'zipped.txt.gz',
encoding: file.Encoding.UTF8,
folder: -15,
contents: strBodyZipped
});

var guzippedFile = compress.gunzip({
file: objTxtFile
});

var strGuzipText = guzippedFile.getContents();
strGuzipText = strGuzipText.toString().replaceAll('\\', '');
strGuzipText = strGuzipText.substr(1, strGuzipText.length-2);

requestBodyOrg.body = JSON.parse(strGuzipText);

需要注意的点有:

  1. Netuite的compress.gunzip接收的参数是文件的类型
  2. compress.gunzip接收的文件类型需要必须是.gz, GZIP的文件类型: file.Type.GZIP
    1. 这点很重要, 虽然内部是相同的内容, 如果是txt文件, NetSuite会解压缩报错
  3. 解压缩以后的字符串, 是第三方系统处理过的, 需要进行一些清洗清洁工作, 比如 \ 和多余的 “”

其他第三方gzip解决方案

多文件的压缩与解压缩

使用 JSZip-Sync

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

使用步骤:

  • 下载 jszip.min.js file; the npm site.
  • 上传到NetSuite script 文件夹
  • 添加模块定义 define object (‘./jszip.min.js’)

举例解压缩一个zip文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
....
const getZipContents = (fileId) => {
try {
if(fileId){
/** zip文件已存放于filecabinet中 */
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;
/** 压缩包中遍历每个文件 */
Object.keys(zip.files).forEach(fileName => {

let strFileContent = zip.file(fileName).async("string");.

arrUnzippedFiles.push({
file_name: fileName,
file_contents: strFileContent._result
})
})
})
return arrUnzippedFiles;
}
} catch(error){
log.error ('解压缩失败文件id ' + intZipFileId, error)
}
}
....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
....

const zippedFile = file.load({id: fileId});
const zippedFileContent = zippedFile.getContents();
const unzippedFiles = [];

const ZipInstance = new jszip();

ZipInstance.sync(function () {
ZipInstance.loadAsync(zippedFileContent, {base64: true}).then(function (zip) {
Object.keys(zip.files).forEach(function(filename){
const file = zip.file(filename).async("string");

unzippedFiles.push({
name: filename,
content: file._result,
size: file._result.length
})

});
})
});
....

个性化需求沟通 扫客服加V加群: