new mZip() → {Object}
Example
import wz from 'w-zip'
let fpUnzip = './test/outputZip'
let fpUnzipExtract = fpUnzip + '/extract'
let fpSrc1 = './test/input/file1(中文).txt'
let fpZip1 = fpUnzip + '/test1.zip'
let fpSrc2 = './test/input/folder1'
let fpZip2 = fpUnzip + '/test2.zip'
let fpZip2PW = fpUnzip + '/test2PW.zip'
let pw = 'abc'
async function test() {
//zipFile
console.log('zipFile before')
console.log('zipFile', await wz.mZip.zipFile(fpSrc1, fpZip1))
console.log('zipFile after')
//zipFolder
console.log('zipFolder before')
console.log('zipFolder', await wz.mZip.zipFolder(fpSrc2, fpZip2))
console.log('zipFolder after')
//zipFolder with password
console.log('zipFolder with password before')
console.log('zipFolder with password', await wz.mZip.zipFolder(fpSrc2, fpZip2PW, { pw }))
console.log('zipFolder with password after')
//unzip
console.log('unzip1 before')
console.log('unzip1', await wz.mZip.unzip(fpZip1, fpUnzipExtract + '/test1'))
console.log('unzip1 after')
//unzip
console.log('unzip2 before')
console.log('unzip2', await wz.mZip.unzip(fpZip2, fpUnzipExtract + '/test2'))
console.log('unzip2 after')
//unzip with password
console.log('unzip2 with password before')
console.log('unzip2 with password', await wz.mZip.unzip(fpZip2PW, fpUnzipExtract + '/test2PW', { pw }))
console.log('unzip2 with password after')
//listEntries
console.log('listEntries before')
console.log('listEntries', (await wz.mZip.listEntries(fpZip2)).map((v) => v.filename))
console.log('listEntries after')
//readEntry
console.log('readEntry before')
console.log('readEntry', JSON.parse(await wz.mZip.readEntry(fpZip1, 'file1(中文).txt')).name)
console.log('readEntry after')
//readEntry with password
console.log('readEntry with password before')
console.log('readEntry with password', (await wz.mZip.readEntry(fpZip2PW, 'folder1/f1-1.xlsx', { type: 'u8', pw })).length)
console.log('readEntry with password after')
}
test()
.catch((err) => {
console.log(err)
})
// zipFile before
// zipFile done: test1.zip
// zipFile after
// zipFolder before
// zipFolder done: test2.zip
// zipFolder after
// zipFolder with password before
// zipFolder with password done: test2PW.zip
// zipFolder with password after
// unzip1 before
// unzip1 done: test1
// unzip1 after
// unzip2 before
// unzip2 done: test2
// unzip2 after
// unzip2 with password before
// unzip2 with password done: test2PW
// unzip2 with password after
// listEntries before
// listEntries [
// 'folder1/f1-1.xlsx',
// 'folder1/f1-2.xlsx',
// 'folder1/folder2/',
// 'folder1/folder2/f2-1.xlsx',
// 'folder1/folder2/f2-2.xlsx',
// 'folder1/folder2/f2-3(中文).xlsx'
// ]
// listEntries after
// readEntry before
// readEntry w-backup
// readEntry after
// readEntry with password before
// readEntry with password 6663
// readEntry with password after
Returns:
回傳壓縮物件,可使用函數zipFile、zipFolder、unzip、listEntries、readEntry
- Type
- Object
Methods
(async, static) listEntries(src) → {Promise}
- Description:
列出壓縮檔內項目,不解壓至資料夾
加密之壓縮檔不需密碼即可列出。含不安全路徑(例如'../'、絕對路徑、磁碟代號)項目之壓縮檔會整個reject。 項目名以''分隔者會正規化為'/'。項目名未標示UTF-8且無Unicode路徑欄位者(例如部分工具以Big5寫入),依zip.js預設以CP437解碼,名稱會呈現亂碼,讀取時須使用回傳之名稱。
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
src |
String | Buffer | Uint8Array | 輸入壓縮檔案位置字串,或壓縮檔內容之Buffer、Uint8Array,給予檔案位置時只讀取中央目錄而不載入整個檔案 |
Returns:
回傳Promise,resolve為項目陣列,依壓縮檔中央目錄順序,各項目為{filename,directory,uncompressedSize,compressedSize,encrypted,lastModDate},filename為壓縮檔內以'/'分隔之路徑,reject為失敗資訊
- Type
- Promise
(async, static) readEntry(src, filename, optopt) → {Promise}
- Description:
讀取壓縮檔內單一項目,不解壓至資料夾
讀出內容會驗證CRC32,資料損毀或密碼錯誤時reject。含不安全路徑(例如'../'、絕對路徑、磁碟代號)項目之壓縮檔會整個reject。
- Source:
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
src |
String | Buffer | Uint8Array | 輸入壓縮檔案位置字串,或壓縮檔內容之Buffer、Uint8Array,給予檔案位置時只讀取中央目錄與該項目而不載入整個檔案 |
|||||||||||||||||
filename |
String | 輸入項目路徑字串,須與listEntries回傳之filename完全相同(以'/'分隔且區分大小寫),若有同名項目則取最後一筆 |
|||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為項目內容之字串或Uint8Array,reject為失敗資訊,找不到項目或項目為資料夾時亦為reject
- Type
- Promise
(async, static) unzip(fpSrc, fpTar, optopt) → {Promise}
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fpSrc |
String | 輸入解壓縮來源檔案位置字串 |
||||||||||||
fpTar |
String | 輸入解壓縮目標資料夾位置字串 |
||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為成功資訊,reject為失敗資訊
- Type
- Promise
(async, static) zipFile(fpSrc, fpTar, optopt) → {Promise}
Parameters:
| Name | Type | Attributes | Default | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
fpSrc |
String | 輸入壓縮來源檔案位置字串 |
|||||||||||||||||
fpTar |
String | 輸入壓縮目標檔案位置字串 |
|||||||||||||||||
opt |
Object |
<optional> |
{}
|
輸入設定物件,預設{} Properties
|
Returns:
回傳Promise,resolve為成功資訊,reject為失敗資訊
- Type
- Promise
(async, static) zipFolder(fpSrc, fpTar, level) → {Promise}
Parameters:
| Name | Type | Description |
|---|---|---|
fpSrc |
String | 輸入壓縮來源資料夾位置字串 |
fpTar |
String | 輸入壓縮目標資料夾位置字串 |
level |
Integer | 輸入壓縮程度整數,範圍為0至9,0為不壓縮而9為最高壓縮,預設9 |
Returns:
回傳Promise,resolve為成功資訊,reject為失敗資訊
- Type
- Promise