Unzipper skipper folders inside zip file - javascript

I'm trying to unzip a file that includes files and folders , but i can only see
one file got extracted and rest of the files and folders are missing after unzipping it. I'm using unzipper library.
Here is the code snippet:
var writeStream = fstream.Writer(outputPath);
readStream
.pipe(unzipper.Parse())
.pipe(writeStream)
Update
After looking at closely, it looks like only last file from zip exists in the target directory.

Did you tried this
const unzipper =require('unzipper');
const fs =require('fs');
fs.createReadStream('./files.zip')
.pipe(unzipper.Extract({path:'output/path'}))
reference

Related

Cannot find module './config.json'

I want to make a module linked to a config.json located outside the node_modules folder. The problem is that my index.js located in modules can't take into account the config.json located outside node_modules, do you have any idea what I should do?
I have tried
let configFile = require('./config.json')
and
let configFile = require('.../config.json')
It is important to first locate where the 'distance' of your index.js file to your config.json file.
Say, for example, if your file tree is like this:
cool-bot / modules / index.js
cool-bot / config.json
You could have your index.js file like this:
let configFile = require('../config.json')
../ will look up to the parent of the parent of your current folder.
./ will look up in the current folder.
Hope this was useful for you.
Okay first of all I have one question. Why? Why would you put your index.js in node_modules?
Anyways, you should check how far the bot config json file is from your index.js, for example:
v RootProjectFolder
⠀⠀config.json
⠀⠀v node_modules
⠀⠀⠀⠀v bot_folder
⠀⠀⠀⠀⠀⠀index.js
config.json's parent folder is RootProjectFolder
index.js is 3 folders away from RootProjectFolder;
RootProjectFolder -> node_modules -> bot_folder -> index.js
so you will need to go 3 folders back:
./ = 1 folder back (folder in which the file is in)
../ = 2 folders back (parent folder of the folder in which the file is in)
../../ = 3 folders back
So in this situation, you would do:
const configFile = require('../../config.json');
I can't give a specific answer based on your situation as I don't know your
project's structure.

How to unzip a file without creating archive folder in nodejs?

I have a file myarchive.zip that contains many directories, files, etc. Let's say this myarchive.zip file lives in a directory called "b". Currently, I am using the unzip module in Nodejs, using that I am able to create a directory by default called "myarchive" with the contents of the zip file. I do not want the code to create this "myarchive" directory - I just want the contents to be extracted to directory "b". Is this possible? If so, then how. Thanks!
The function that I am currently using for unzipping is:
const extractArchive = async (inputFileName, extractToDirectory) => {
fs.createReadStream(inputFileName)
.pipe(unzip.Extract({
path: extractToDirectory
}));
PS: inputFileName is the path of the zip file(base directory in which zip is present+ the file name)

config file on node_modules

I created a generic npm package which has my business logic, but I need some google cloud storage information that is in my config files. How can I access this file, if my package is in my node_modules folder? What would be a good solution for that?
this is the structure:
-config
-google_storage_config
-node_modules
-package
-serviceWhichNeedsThatConfig
Based on your folder structure, we will assume your path to the config will be ../../../config/google_storage_config, since node_modules/package/serviceWhichNeedsThatConfig should always be in the root directory.
Now, to access any variables from this config file, simply include the following code in the serviceWhichNeedsThatConfig,
var config = require('../../../config/google_storage_config');
console.log(config.myVariable);
Hi~Have you tried require?
var config = require('../../config/google_storage_config');

Node-Webkit won't open file

First I'd like to say thanks for taking the time to read this.
I am trying to open a JSON file that is in the following directory structure:
#--> Root Folder
--> App.exe
#--> Configuration
---> JSON File
So I used the Code:
var ConfigFile = "./Configuration/JSON.json";
Followed by:
var fs = require('fs');
var file_content = fs.readFileSync(ConfigFile);
var content = JSON.parse(file_content);
// Manipulate the Data
For some odd reason, Node-Webkit seems to be looking for the folder in a Temp Directory located in:
C:\Users\User\AppData\Local\Temp\nw9740_14956\Configuration
The file is not there, and thus in the Console I get the following Error:
Uncaught Error: ENOENT: no such file or directory, open
'C:\Users\User\AppData\Local\Temp\nw9740_14956\Configuration\JSON.json'
I am running Windows (as you can tell), and I would like for fs to pull the file from the Folder (Configuration) that is adjacent to the app.exe.
Any help is appreciated
I've only done this once, so I may be wrong, but it looks like you're bundling your app content into the exe? If you do this, node-webkit will extract the app contents into the %TEMP% folder and then run the content from there.
Try checking the command line arguments to see if arg[0] will point you to the actual node-webkit exe that's running the app. From there, you should be able to construct a path to your configuration data.

ajax call to a local csv file

The folder structure is:
HOME\htmlhelp\wwhelp\wwhimpl\js\scripts
HOME\auxi\
I can read a csv file (located in the HOME\htmlhelp dir) from a js file located in the HOME\htmlhelp\wwhelp\wwhimpl\js\scripts dir using the following:
var file_path="../../../../../htmlhelp/rule_mapping.csv";
However, the same js file (from the same location) cannot read the same csv file in the HOME\auxi\ directory!
Am using the path as var file_path="../../../../../auxi/rule_mapping.csv";
It throws an "NS_ERROR_DOM_BAD_URI: Access to restricted URI denied" error. I have checked the file permissions and stuff but no luck.
Any help would be appreciated.
The problem lies within:
var file_path="../../../../../htmlhelp/rule_mapping.csv";
It's not possible to traverse up to paths out of the root directory where the document is located.
Assuming you work under UNIX (Linux/Mac), one solution would be to create a symbolic link on the root directory of the document, such as (executing from the scripts directory):
ln -s ../../../../../htmlhelp/rule_mapping.csv rule_mapping.csv
And changing the file_path to:
var file_path="./rule_mapping.csv";
I hope this helps!

Categories

Resources