728x90
zip and unzip with node
ref : https://new.atsit.in/13087/
Project init
// project init
mkdir node-zip-archiver
cd node-zip-archiver
npm init -y
touch archiver.js
// install package
npm install archiver –save
fs
: for managing file system tasksarchiver
: for zipping file or directory
file -> zip
const archiver = require("archiver");
const fs = require("fs");
// create ZIP from file
const createZipFromFile = (file) => {
const filePath = __dirname + "/" + file;
const output = fs.createWriteStream(filePath + ".zip");
const archive = archiver("zip", {
zlib: { level: 9 }, // compression level to highest
});
// adding method to archive object
archive.pipe(output);
archive.file(filePath, { name: file });
archive.finalize();
};
createZipFromFile("test.js");
folder -> zip
// create ZIP from folder
const createZipFromFolder = (folder) => {
const folderPath = __dirname + '/' + folder
const output = fs.createWriteStream(folderPath + '.zip')
const archive = archiver('zip', {
zlib: { level: 9 } // set compression level to the highest
})
archive.pipe(output)
archive.directory(folderPath, false)
archive.finalize()
}
zip --unzip--> file
npm install unzipper --save
const unzipper = require("unzipper")
//function to extract ZIP file
const extractZip = async (file) => {
const filePath = __dirname + '/' + file
const outputPath = __dirname + '/extracted'
await fs.createReadStream(filePath)
.pipe(unzipper.Extract({ path: outputPath }))
.promise()
}
728x90
'Research > Node.js' 카테고리의 다른 글
node.js_fs로 csv 파일 생성하는 방법 (0) | 2023.09.22 |
---|---|
35.node.js Buffer? (0) | 2023.09.20 |
Express.js_싱글톤 패턴, 비즈니스 로직 분리 (0) | 2023.03.24 |
Express.js_router 분리하는 방법 (0) | 2023.03.24 |
Express.js_CRUD API (0) | 2023.03.24 |
댓글