kevva / decompress

Extracting archives made easy
MIT License
415 stars 51 forks source link

Set flag for writing file #69

Open Fndroid opened 5 years ago

Fndroid commented 5 years ago

Here is the situation.

let tempPath = '' // C:\Users\abc\AppData\Local\Temp
let tempFilePath = path.join(tempPath, 'geoip.tar.gz')
let destPath = '' // C:\Users\abc\.config\clash
got
  .stream("xxx/GeoLite2-Country.tar.gz")
  .pipe(fs.createWriteStream(tempFilePath))
  .on("finish", async () => {
    let decRes = await decompress(tempFilePath, path.join(destPath, 'Country.mmdb'));
  });

I tried set decompress dest path to my destPath directly, and it threw an unknown error. It seems that destPath is hidden on Windows (.config directory).

So I dig up from fs library, a solution is set flag to 'r+'

On Windows, opening an existing hidden file using the 'w' flag (either through fs.open() or fs.writeFile() or fsPromises.open()) will fail with EPERM. Existing hidden files can be opened for writing with the 'r+' flag.

......
  .on("finish", async () => {
    let decRes = await decompress(tempFilePath, path.join(tempPath, 'decompress_output'));
  });
  if (decRes.length === 1) {
    fs.writeFileSync(path.join(this.clashPath, 'Country.mmdb'), decRes[0].data, {flag: "r+"})
  }

So, is there a way to get rid of this extra decompress_output directory?