实现生成加密zip压缩包的nodejs脚本
pnpm i -D archiver archiver-zip-encrypted
const { cwd } = require ( "node:process" ) ;
const path = require ( "path" ) ;
const fs = require ( "fs-extra" ) ;
const archiver = require ( "archiver" ) ;
if ( ! archiver. isRegisteredFormat ( "zip-encrypted" ) ) { archiver. registerFormat ( "zip-encrypted" , require ( "archiver-zip-encrypted" ) ) ;
}
const defaultOption = { sourceName : "dist" , targetName : "dist" , password : "password" ,
} ; function compression ( options = defaultOption ) { const sourceName = options. sourceName ?? "dist" ; const targetName = options. targetName ?? "dist" ; const password = options. password ?? "password" ; const sourcePath = path. resolve ( cwd ( ) , sourceName) ; const targetPath = path. resolve ( cwd ( ) , ` ${ targetName} .zip ` ) ; try { const output = fs. createWriteStream ( targetPath) ; const archive = archiver ( "zip-encrypted" , { zlib : { level : 9 } , encryptionMethod : "zip20" , password, } ) ; output. on ( "close" , ( ) => { console. log ( ` Zip file ${ targetName} .zip created successfully. ` ) ; } ) ; archive. on ( "error" , ( err ) => { console. error ( "Error while creating zip:" , err) ; } ) ; archive. pipe ( output) ; archive. directory ( sourcePath, false ) ; archive. finalize ( ) ; console. log ( ` Created ${ targetName} .zip with password. ` ) ; } catch ( err) { console. error ( "Error while creating zip:" , err) ; }
} compression ( ) ;
封装成一个vite插件
const { cwd } = require ( 'node:process' ) ;
const path = require ( 'path' ) ;
const fs = require ( 'fs-extra' ) ;
const archiver = require ( 'archiver' ) ; if ( ! archiver. isRegisteredFormat ( 'zip-encrypted' ) ) { archiver. registerFormat ( 'zip-encrypted' , require ( 'archiver-zip-encrypted' ) ) ;
} const defaultOption = { sourceName : 'dist' , targetName : 'dist' , password : 'password!' ,
} ; function compression ( options = defaultOption ) { const sourceName = options. sourceName ?? 'dist' ; const targetName = options. targetName ?? 'dist' ; const password = options. password ?? 'password' ; const sourcePath = path. resolve ( cwd ( ) , sourceName) ; const targetPath = path. resolve ( cwd ( ) , ` ${ targetName} .zip ` ) ; return { name : 'compression' , closeBundle ( ) { console. log ( 'Starting to zip...' ) ; try { const output = fs. createWriteStream ( targetPath) ; const archive = archiver ( 'zip-encrypted' , { zlib : { level : 9 } , encryptionMethod : 'zip20' , password, } ) ; output. on ( 'close' , ( ) => { console. log ( ` Zip file ${ targetName} .zip created successfully. ` ) ; } ) ; archive. on ( 'error' , ( err ) => { console. error ( 'Error while creating zip:' , err) ; } ) ; archive. pipe ( output) ; archive. directory ( sourcePath, false ) ; archive. finalize ( ) ; console. log ( ` Created ${ targetName} .zip with password. ` ) ; } catch ( err) { console. error ( 'Error while creating zip:' , err) ; } } , } ;
} module. exports = { compression,
} ;
vite.config.js中使用vite插件
import { compression } from './plugin/zip.js' ; export default ( { mode } ) => { return defineConfig ( { ... plugins : [ ... compression ( ) , ] , } ) ;
} ;