请任何人指导我,我们如何创建受密码保护的 zip 文件并解压。 使用node-7z-archive https://www.npmjs.com/package/node-7z-archive 实际上我想用文件夹创建密码保护 zip 文件
创建简单 zip 文件的代码:
const fs = require('fs');
const path = require('path');
const archiver = require('archiver');
const sourceFolder = 'create_zip'; // Replace with the actual folder path
const zipFileName = 'output.zip';
// Create a writable stream to the zip file
const output = fs.createWriteStream(zipFileName);
// Create a zip archive
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level
});
// Pipe the archive to the output stream
archive.pipe(output);
// Add the entire folder to the archive
archive.directory(sourceFolder, false); // 'false' means no root directory in archive
// Finalize the archive
archive.finalize();
// Wait for the output stream to finish writing
output.on('close', () => {
console.log('Zip archive created.');
});
// Handle errors during archiving
archive.on('error', (err) => {
throw err;
});
请帮助我,如何使用node-7z-archive包在nodejs中创建密码保护zip文件。
工作代码,但请确保下载:7z https://www.7-zip.org/download.html根据系统(window、mac或任何其他)和设置环境
<pre>
import Seven from 'node-7z'
const options = {
// Replace 'your-password' with the desired password for the archive.
password: '1234',
};
const filesToCompress = 'create_zip'; // List of files to include in the archive.
const archiveName = 'myArchive.7z'; // Specify the name of the archive file.
// Create the 7z archive with a password.
const myStream = Seven.add(archiveName, filesToCompress, options);
// Handle progress events (optional)
myStream.on('progress', (event) => {
console.log(`Progress: ${event.percent}%`);
});
// Handle completion
myStream.on('end', () => {
console.log('7-Zip archive created with password.');
});
// Handle errors
myStream.on('error', (error) => {
console.error(`Error: ${error.message}`);
});
</pre>