Node js. Writing directory to the archive, without path to this directory - node.js

Got some task, it is not hard, but i have some trouble.
Maybe someone already has similar problem.
My task is writing to zip archive some folder, with files and other folders in it with using NodeJS
I try to use AdmZip pakage
folder project structure:
var AdmZip = require('adm-zip');
let Archive = new AdmZip();
Archive.addLocalFolder('Archive/filesFolder', '');
Archive.writeZip('Archive/newArchive.zip);
I must get archive with 'filesFolder', instead i get archive with 'Archive' folder and in it i have 'filesFolder'.
If anybody know, how to record only target folder, and not the sequence of a way folders?

What happens is that you are providing Archive/filesFolder as value to writeZip and that means include in the zip Archive folder and inside that include filesFolder.
For testing purpose change the value of writeZip() to just filesFolder.zip and it should zip content of Archive as filesFolder.zip in current working directory. See below (you can copy/paste the bit of code and run it and it should work).
var zip = new AdmZip();
// I don't know why you provided a second argument to below, I removed it
// There was nothing about it in the documentation.
zip.addLocalFolder('./Archive');
//rename the value of the following to just filesFolder.zip
zip.writeZip('filesFolder.zip');
The above should output the content of Archive to the current working directory as filesFolder.zip
I did mention this in my comment and your commend seem to indicate that you have path issue, so try the above script.

Related

Node : What is the right way to delete all the files from a directory?

So I was trying to delete all my files inside a folder using node.
I came across 2 methods .
Method 1
Delete the folder using rmkdir. But if I plan on adding the images on the same folder then I use mkdir and creates the same folder again and appends the files to it.
Example: I have an Add Files and Delete ALL button. When I click deleteAll , the folder gets deleted. And when I click add then the folder gets created and the file gets added to that folder
Method 2
Using readdir , I loop through the files and stores in an array and then delete only the files instead of the folder.
Which is the best way to do it ? If its not among these then please advice me a better solution.
The rm function of ShellJS will do the trick. It works as a one-liner, and it works cross-platform, and is well tested and documented. It even supports recursive deletes.
Basically, something such as:
const { rm } = require('shelljs');
rm('-rf', '/tmp/*');
(Sample code taken from ShellJS' documentation.)

Alternative to fs.readdirSync large directory directory in Node

I have a single directory with a few million json files in it. I ultimately want to iterate over each file in the directory, read it, do something with the information and then write something into a database.
My script works perfectly when I use a test directory with a few hundred files. However, it stalls when I use the real directory. I strongly believe that I have pinpointed the problem to the use of:
fs.readdirSync('my dir path')
Converting this to the Async function would not help anything since I need the file names before anything else can happen anyways. However, my belief is that this operation hangs because it simply "takes too long" for it to read the entire directory.
For reference here is a broader portion of the function:
function traverseFS(){
var path = 'my dir name and path';
var files = fs.readdirSync(path);
for (var i in files) {
path + '/' + files[i];
var fileText = fs.readFileSync(currentFile,'utf8');
var json= JSON.parse(fileText);
if(json)
// do something
}
}
My question is either:
Is there something I can do get this to work using readdirSync?
Is there another operation I should be using?
You would need to either use a child process (easiest) that creates a directory listing and parse that or write your own streamable binding to scandir() (on *nix) and/or whatever the equivalent is on Windows and use that. For the latter, you may want to use the libuv code (*nix, Windows) as a guide.

I want to copy files from temporary folder to in my c drive by creating new folder

if FileCopy(ExpandConstant('{tmp}\SPECTRUMJOBS_Data.MDF'),
ExpandConstant('C:\Program Files\Microsoft SQL Server\MSSQL11.LOGISTICS\MSSQL\DATA\SPECTRUMJOBS_Data.MDF'),
False) then
I tried the above one but I am not able to copy because there is no folder named DATA in the location.
so please help me to copy the DATA folder and the file
To force creation of directories you can use (text is from InnoSetup help):
Prototype:
function ForceDirectories(Dir: string): Boolean;
Description:
Creates all the directories along the specified directory
path all at once. If the first directories in the path do exist, but
the latter ones don't, ForceDirectories creates just the ones that
don't exist. Returns True if successful, False otherwise.
InnoSetup documentation is at ishelp
You answered yourself. If there is no folder DATA in folder MSSQL, the folder must be created first.
And... if there is no folder MSSQL in folder MSSQL11.LOGISTICS, that folder must be created first.
And so on...
Now read the above sentences in reverse order, and you're done.

node.js and ncp module - fails to copy single file

I am using Node.js v6.3.1 and ncp v2.0.0
I can only get ncp to copy the contents of a directory, but not a single file within that directory.
Here is the code copying the contents of a directory recursively that works:
var ncp = require("ncp").ncp;
ncp("source/directory/", "destination/directory/", callback);
...and here is the same code but with a file as the source:
var ncp = require("ncp").ncp;
ncp("source/directory/file.txt", "destination/directory/", callback);
From this all I can think is that ncp was specifically designed to copy directories recursively, not single files maybe?
I had thought about using something like fileSystem's read/write stream functions as described here but really for consistency I was hoping to stick with ncp.
Update:
I have found another package called node-fs-extra which does what I want without the need for me to add event handlers to the operations, like I would have to do with the fileSystem read/write solution.
Here is the code that is working:
var fsExtra = require("fs-extra");
fsExtra.copy("source/directory/file.txt", "destination/directory/file.txt", callback);
Obviously this still is inconsistent, but at least is a little less verbose.
Ok I have figured out what I was doing wrong.
I was trying to copy a file into a directory, where as I needed to copy and name the file inside a directory.
So here is my original code that does not work:
var ncp = require("ncp");
ncp("source/directory/file.txt", "destination/directory/", callback);
...and here is the fixed code working, notice the inclusion of a file name in the destination directory:
var ncp = require("ncp");
ncp("source/directory/file.txt", "destination/directory/file.txt", callback);
So it looks like ncp wont just take the file as is, but needs you to specify the file name at the other end to successfully copy. I guess I was assuming that it would just copy the file with the same name into the destination directory.
I have found another package called node-fs-extra which does what I want without the need for me to add event handlers to the operations, like I would have to do with the fileSystem read/write solution.
Here is the code that is working:
var fsExtra = require("fs-extra");
fsExtra.copy("source/directory/file.txt", "destination/directory/file.txt", callback);
Obviously this still is inconsistent, but at least is a little less verbose.

ChromeWorker File Not including in the extension package

Hi I am building a Mozilla Extension through CFX tool. I have used ChromeWorker in it. It is working fine while i am running : cfx run command. But while building up a package using cfx xpi the Chrome Worker file is not included in the xpi package.
I am using this to create the worker thread.
var tworker = new ChromeWorker("chrome://addons/content/t_worker.js");
my t_worker.js file is present in addons/lib
I have also put one chrome.manifest file in the package that contains :
content addons ./resources/addons/lib/
Please tell the possible reason for this problem and also how to fix it .
Try moving your file into the data folder then do:
const self = require('sdk/self');
var tworker = new ChromeWorker(self.data.url('t_worker.js')
im totally not sure of this syntax, i just typed off top of my head
I putted that worker file in the lib thats why its not working. I just changed the location of my file to data folder and made changes in my chrome.manifest file : content addons ./resources/addons/data(previously it was lib)/ . Its working fine Thanks to #Noitidart for the suggestion of putting it in data folder

Resources