zip
zip
Stability: 2-Stable
The Zip module is used to process file compression and decompression, and supports encryption and compression.
The zip function comes from zip4j, you can read related documents for more information.
$zip.zipDir(dir, dest[, options])
dir
{string} The path of the folder to be compressed, if there are subfolders under the folder, they will be compressed togetherdest
{string} The path where the compressed package is storedoptions
{object} Optional parameters, option options include the following, for complete options, see Compression Options:password
{string} Encrypted and compressed password. If the password is set but the encryption method is not set, the encryption method defaults to AEScompressionMethod
{string} Compression method,COMP_STORE
means only packaging without compression,COMP_DEFLATE
means compression (default)encryptionMethod
{string} Encryption method,ENC_NO_ENCRYPTION
is no encryption (default),ENC_METHOD_STANDARD
is standard secret,ENC_METHOD_AES
is AES encryptioncompressionLevel
{number} Compression level, 0~9, 0 is no compression, 9 is the best compression (slower speed). In versions before 8.7, the default is 0 without compression; after 8.7, the value defaults to 5 (normal compression)aesKeyStrength
{string} AES encryption strength, includingAES_STRENGTH_128
,AES_STRENGTH_192
,AES_STRENGTH_256
readHiddenFiles
{boolean} Whether to include hidden files when compressing the folder. If true, hidden files will be included when adding the folder to the zipincludeRootFolder
{boolean} Set a flag to indicate whether the parent folder of the added file will be included in the ZIP. If true, the parent folder of the added file will be included in the ZIP. Default truerootFolderInZip
{string} Set the folder name of the first directory of the compressed package, if there is no first directory, create this first directory
Compress all files/folders in the folder and generate them to the target path dest
.
A simple compressed folder example.
// Need to compress the file path
let dir = "./zipExample2/";
// File storage path after compression
if (!$files.exists(dir)) {
// folder does not exist create folder
$files.create(dir);
}
// Create a file, you can put a few more files or folders to run to see the effect
$files.create($files.join(dir, "test.js"));
// File storage path after compression
let zipFile = $files.join(dir, "Unencrypted Compressed.zip");
// Delete the file with the same name
$files.remove(zipFile);
// Compress
$zip.zipDir(dir, zipFile);
log("Completion of compression, path of compressed package:" + zipFile);
Encryption and compression example
// Need to compress the file path
let dir = "./zipExample2/";
// File storage path after compression
if (!$files.exists(dir)) {
// folder does not exist create folder
$files.create(dir);
}
// Create a file, you can put a few more files or folders to run to see the effect
$files.create($files.join(dir, "test.js"));
// Compressed package storage path
let encryptedZipFile = $files.join(dir, "encrypted and compressed.zip");
$files.remove(encryptedZipFile); // delete the file with the same name
// Compress
$zip.zipDir(dir, encryptedZipFile, {
password: "CloudControl Pro" ,// Compressed password
compressionLevel: 5, // compression level
aesKeyStrength:'AES_STRENGTH_256', // encryption method
readHiddenFiles: false, // Hidden files are not added to the compressed package
includeRootFolder: false, //Do not add the parent directory of the folder into the compressed package
rootFolderInZip: "Test" // Add a parent directory for the compressed package
});
log("Encryption and compression is complete, compressed package path:" + encryptedZipFile);
$zip.zipFile(file, dest[, options])
file
{string} Single file path to be compressed.dest
{string} The path where the compressed package is stored- For
options
{object} options, please refer to Compression Options
Compress the single file file
to the path dest
.
let path = "./zipExample/test.js";
if (!$files.exists(path)) {
$files.create(path);
}
let zipFilePath = "./zipExample/test.zip";
$zip.zipFile(path, zipFilePath);
log("Single file compression completed, compressed package path:" + zipFilePath);
$zip.zipFiles(fileList, dest[, options])
fileList
{Array<string>} Array of multiple file paths to be compresseddest
{string} Compression target pathoptions
{object} options refer to compression options
Compress multiple files fileList
to the path dest
. Folders cannot be included in fileList
.
let dir = "./zipExample3/";
if (!$files.exists(dir)) {
$files.create(dir);
}
let fileList = ["file1.js", "file2.js", "file3.js"].map(p => $files.join(dir, p));
fileList.forEach(file => {
$files.create(file);
});
let zipMultiFile = $files.join(dir, "Multi-file compression.zip");
$files.remove(zipMultiFile); // delete the file with the same name
$zip.zipFiles(fileList, zipMultiFile); // Compress
log("Multi-file compression completed, compressed package path:" + zipMultiFile);
$zip.unzip(zipFile, dest[, options])
zipFile
{string} The path of the compressed package file to be decompresseddest
{string} The folder directory after decompressionoptions
{object} decompression options, optional, see decompression options
Unzip the zip file. If the folder dest
does not exist, create the folder and extract the contents into it; if dest
already exists, create a folder with the same name as the zipFile
file under dest
, and extract the contents to inside.
// Prepare an encrypted compressed package
let dir = "./zipExample2/";
if (!$files.exists(dir)) {
$files.create(dir);
}
// File storage path after compression
let encryptedZipFile = $files.join(dir, "encrypted and compressed.zip");
$files.remove(encryptedZipFile); // delete the file with the same name
// Compress
$zip.zipDir(dir, encryptedZipFile, {
password: "CloudControl Pro" // Compressed password
});
$zip.unzip(encryptedZipFile,'./zipExample5', {
password:'CloudControl Pro' // Unzip the password
});
log("The encrypted zip is successfully decompressed, the path of decompression:" + "./zipExample5/");
$zip.open(file)
file
{string} Compressed package file path- Return {ZipFile} ZipFile object
Open a zip file and return the ZipFile
object, which can be further zip operations.
ZipFile
The object returned by $zip.open()
is used to add or delete files in the compressed package, obtain file header information, and decompress.
ZipFile.getPath()
- Return {string} return path
Get the path of the current compressed package.
let path = "./test.zip";
let zipFile = $zip.open(path);
log(zipFile.getPath();
// Output path "./test.zip"
ZipFile.isValidZipFile()
- Return whether {boolean} is a valid compressed package
Get whether the current compressed package is a valid compressed package.
If the selected path is not a compressed package or the file does not exist, all return false
.
ZipFile.setPassword(password)
password
{string} set password
If the file is an encrypted compressed package, you must use this function to set the correct decompression password to decompress the encrypted file.
Trying to decompress the encrypted compressed package without setting a password will throw an exception. If the selected compressed package file does not exist, using this function will throw a ZipException
.
ZipFile.isEncrypted()
- Return {boolean} whether the compressed package is encrypted
Get whether the current compressed package is an encrypted compressed package.
ZipFile.addFile(file[, options])
file
{string} local file pathoptions
{object} Compression options, optional, see Compression Options
Add the specified file in the compressed package.
let zipFile = $zip.open("./app.apk");
zipFile.addFile(file);
ZipFile.addFiles(fileList, options)
fileList
{Array<string>} The array of local file paths to be addedoptions
{object} Compression options, optional, see Compression Options
Add multiple files in the compressed package.
ZipFile.addFolder(folder[, options])
folder
{string} folder pathoptions
{object} Compression options, optional, see Compression Options
In the compressed packageAdd the specified folder.
Be careful not to add the parent directory of the compressed package, otherwise it may cause endless adding loops!
A simple example of a backup script:
let zipFile = "/sdcard/scripts.zip";
//Specify the compressed file
let zip = $zip.open(zipFile);
log("Start backup, depends on the number of your scripts");
// Add the current folder to the compressed package
zip.addFolder("./");
log("Backup is complete, the compressed package path is:" + zipFile);
ZipFile.removeFile(file)
file
{string} The file to be deleted
Delete the specified file in the zip file. This method first finds the file header, and then deletes the file; if the file does not exist, this method throws an exception.
If the zip file is a split zip file, this method will throw an exception, because the zip specification does not allow to update the split zip archive.
let zipFile = $zip.open("./app.apk");
zipFile.removeFile("res/drawable/logo.png");
ZipFile.extractFile(file, dest[, options, newFileName])
file
{string} The file to be unzippeddest
{string} The destination path of the decompressed fileoptions
{Object} Decompression options, optional. See decompression optionsnewFileName
{string} The new name of the decompressed file, optional.
Extract specific files from the compressed package file to the target path. If the target path is invalid, this method will throw an exception.
ZipFile.extractAll(dest{, options})
dest
{string} Decompression pathoptions
{Object} Decompression options, optional. See decompression options
Unzip all files to the target path dest
.
let zip = $zip.open("./test.zip");
zip.addFiles(["./test.js", "./test.txt"]);
zip.extractAll("./test");
ZipFile.getFileHeader(file)
file
{string} The specified file path in the compressed package- Return {FileHeader}
Get the file header information of the specified file in the compressed package. The file header information includes the check value, whether it is encrypted, comments, etc.
ZipFile.getFileHeaders()
- Return {Array<[FileHeader](https://javadoc.io/doc/net.lingala.zip4j/zip4j/1.3.2/net/lingala/zip4j/model/FileHeader.html)>}
Get the file header information of all files in the compressed package. The file header information includes the check value, whether it is encrypted, comments, etc.
Compression options
options
{object} includes:
aesKeyStrength
{string} AES encryption strength, includingAES_STRENGTH_128
,AES_STRENGTH_192
,AES_STRENGTH_256
compressionLevel
{number} Compression level, 0~9, 0 is no compression, 9 is the best compression (slower speed). In versions before 8.7, the default is 0 without compression; after 8.7, the value defaults to 5 (normal compression)compressionMethod
{string} Compression method,COMP_STORE
means only packaging without compression,COMP_DEFLATE
means compression (default)defaultFolderPath
{string} When compressing or adding files, specify the default folder location of the file in the compressed packageencryptionMethod
{string} Encryption method,ENC_NO_ENCRYPTION
is no encryption (default),ENC_METHOD_STANDARD
is standard secret,ENC_METHOD_AES
is AES encryptionfileNameInZip
{string} When compressing or adding files, specify the path and name of the file in the compressed packageincludeRootFolder
{boolean} Set a flag to indicate whether the parent folder of the added file will be included in the ZIP. If true, the parent folder of the added file will be included in the ZIP. Default truepassword
{string} Encrypted and compressed password. If the password is set but the encryption method is not set, the encryption method defaults to AESreadHiddenFiles
{boolean} Whether to include hidden files when compressing the folder. If true, hidden files will be included when adding the folder to the ziprootFolderInZip
{string} Set the folder name of the first directory of the compressed package, if there is no first directory, create this first directory
Decompression options
options
{object} includes:
ignoreAttribute
{Array<string>} File attributes to be ignored when unzipped files are written to disk, including:alls
all attributesarchive
compressed package attributesdateTime
time- Whether
hidden
is hidden - Whether
readOnly
is read-only - Is
system
a system file