2022-06-08 13:45:37 +03:00
|
|
|
import fs from 'fs'
|
2022-09-21 07:08:59 +03:00
|
|
|
import Jszip from 'jszip'
|
|
|
|
|
|
|
|
const compressionConfig = {
|
|
|
|
type: "nodebuffer",
|
|
|
|
compression: "DEFLATE",
|
|
|
|
compressionOptions: {
|
|
|
|
level: 9
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fileConfig = {
|
|
|
|
binary: true,
|
|
|
|
compression: "DEFLATE"
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function save(filePath) {
|
|
|
|
const jszip = new Jszip()
|
|
|
|
const directories = filePath.split('/')
|
|
|
|
const fileName = directories[directories.length - 1]
|
2022-06-08 13:45:37 +03:00
|
|
|
|
|
|
|
try {
|
2022-09-21 07:08:59 +03:00
|
|
|
const data = fs.readFileSync(`${filePath}`)
|
|
|
|
|
|
|
|
await jszip.file(`${fileName}`, data, fileConfig)
|
|
|
|
await jszip.generateNodeStream({
|
|
|
|
...compressionConfig,
|
|
|
|
streamFiles: true
|
|
|
|
})
|
|
|
|
.pipe(fs.createWriteStream(`${filePath}.zip`))
|
2022-06-08 13:45:37 +03:00
|
|
|
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
2022-09-21 07:08:59 +03:00
|
|
|
console.log('on save error', filePath, err.message)
|
2022-06-08 13:45:37 +03:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|