Skip to content

zip

Use the zip Compress and decompress the ZIP package.

Compressed

zipProvides a relatively simple compression interface. You only need to store the file name and file content into an object interface and then pass in.zip.compression()In, the binary buffer object is returned.

ts
import * as zip from 'zip';
import * as buffer from 'buffer';

const buf = zip.compression({
  filename: 'content',
});
buf.toString(buffer.Encoding.Base64); //Returns a base64 character string.

To ensure file security, you can compress only the files with the name extension.

Decompress

zipProvides an interface for reading .zip packages. This interface is an interface for reading streaming data. It can read some files instead of all files.

An example of reading the ZIP package by traversing Base64 is as follows:

ts
import * as zip from 'zip';
import * as buffer from 'buffer';

//Decode binary from base64 string and build zip reader
const base64Str = '...';
const reader = zip.readZip(buffer.from(base64Str, buffer.Encoding.Base64));
//Traverse the path of the ZIP package and search for a file based on the conditions.
const file = reader.listFiles().find(f => /*File found for a condition*/);
if (!file) {
    //Logic for processing files that cannot be found
}
//Obtain the file object and read it.
const bytes = file.readData();
//Convert byte arrays into buffers and character strings.
buffer.fromBytes(bytes).toString();

You can also read the following information directly through the path:

ts
import * as zip from 'zip';
import * as buffer from 'buffer';

//Decode binary from base64 string and build zip reader
const base64Str = '...';
const reader = zip.readZip(buffer.from(base64Str, buffer.Encoding.Base64));
//Reads the decompressed file content from the file name.
const bytes = reader.readData('filename');
//Converts a binary array into a character string.
buffer.fromBytes(bytes).toString(); //Convert byte arrays into buffers and character strings.

The platform limits the size of a single decompressed file. (You can set system parameters to limit the number. For details, contact the O&M administrator.)

For details about how to process uploaded files, see.File upload Documentation.