我有以下 curl 命令可以正常作業...
curl -L -v "https://us-central1-npm.pkg.dev/my-project/npm-public/@my-scope/bucky-barnes/-/@my-scope/bucky-barnes-0.0。 1.tgz" -o 測驗.tgz
這會輸出一個我可以解壓縮的 test.tgz 檔案。
我正在嘗試使用 NodeJS 自動執行此操作(注意-Lfor 重定向)。我想出了以下...
import express from "express";
import tar from "tar-stream";
import https from "https";
const app = express();
const PORT = 3000;
const request = function(req, res, url) {
const reqSent = https.get(url, (response) => {
if (response.statusCode == 302 || response.statusCode == 307) {
const url = `https://${reqSent.host}${response.headers.location}`;
console.log("Redirecting ", url);
request(req, res, url);
} else {
console.log("Extracting ", reqSent.host);
const extract = tar.extract()
extract.on('entry', function(header, stream, next) {
console.log(`The filename is ${header.name}`)
stream.on('end', function() {
next() // ready for next entry
})
stream.resume() // just auto drain the stream
})
extract.on("error", (e)=>{
res.status(500).send(`Error decompressing ${e}`)
})
extract.on('finish', function() {
res.send("Completed")
})
response.pipe(extract);
};
} ).on("error", (e)=>{
console.log(`There was an error ${e}`);
res.status(500).send("Failed");
});
};
app.use((req, res)=>{
request(req, res, "https://us-central1-npm.pkg.dev/my-project/npm-public/@my-scope/bucky-barnes/-/@my-scope/bucky-barnes-0.0.1.tgz")
});
app.listen(PORT, () => console.log(`Server listening on port: ${PORT}`));
當我運行它失敗時說 tar-streamer 無法決議回應......
解壓錯誤錯誤:資料意外結束
我錯過了什么?為什么管道不適用于 tgz 檔案?
uj5u.com熱心網友回復:
tgz不是tar。這是一個壓縮的 tar 球。
來自https://www.npmjs.com/package/tar-stream:
請注意,如果您有 .tar.gz,您仍然需要對資料進行壓縮。我們建議將 gunzip-maybe 與此結合使用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/455402.html
