| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import fetch from 'node-fetch'
- import fs from 'fs'
- import path from 'path'
- import unzipper from 'unzipper'
- import {pipeline} from 'node:stream';
- import {promisify} from 'node:util'
- import {createReadStream,createWriteStream} from "node:fs";
- const streamPipeline = promisify(pipeline);
- /**
- * 下载并解压离线包
- * @param {string} url 下载地址
- * @param {string} destDir 解压目标目录
- */
- async function downloadAndExtract(url, destDir) {
- const zipPath = path.join(destDir, 'temp.zip')
- console.log(`开始下载语言包: ${url}`)
- const res = await fetch(url, {
- method: 'GET',
- headers: {
- //'X-API-Key':'tgpak_gm2f6ntnor2gy4ztnfuw65twoj2wu2tdovzwwztqoe4ts5q'
- },
- })
- if (!res.ok) {
- throw new Error(`下载语言包失败: ${res.status} ${res.statusText}`)
- }
- // zip文件保存到本地目录
- await streamPipeline(res.body, createWriteStream(zipPath));
- console.log('语言包下载完成,开始解压...')
- // 解压到指定目录
- await streamPipeline(createReadStream(zipPath),unzipper.Extract({ path: destDir }));
- console.log('语言包解压完成!')
- // 删除临时压缩包
- fs.unlinkSync(zipPath)
- }
- // 示例调用
- const url = 'http://192.168.0.211:9012/v2/projects/export?ak=tgpak_gm4f6m3pou4diyzzmiygm33koa2gs3rqha2tonlsgftww2y'
- const dest = './src/lang/locales'
- await downloadAndExtract(url, dest)
|