Background
As the company's CMS system, only accept files compressed in RAR format, so it is impossible to directly use the ZIP compression components provided in the Nodejs. Only from the WinRAR software to start, but there is not much on the internet related things, so the following is also trying to do.
A few of the main problems solved
Problem of location acquisition of RAR Software
Through node's command-line component Child_process, run the Registry query command reg query ' key value name ', you can find out the relevant software directory, and then use regular expressions to match the results, return to the software directory. The command is as follows:
var cp=require (' child_process ');
Cp.exec ("reg query Hkey_classes_root\\winrar\\shell\\open\\command/ve", function (E, stdout, stderr) {
if (!e) {
var str = stdout.match (/\ "([^\"]+) \ "/) [0];
if (str) {
Console.log (' already found WinRAR program, detailed address: ' +str ');
}else{
Console.log (' did not find WinRAR program, unable to complete the compression function! ‘);
}
}
});
The above command can get the absolute path to the WinRAR program in the registry, if you install correctly you should get the following result
environment variable Setting problem
The above step can solve the WinRAR software path problem, but how to easily invoke the RAR command? My first thought was to set environment variables. Set path=%path% by running command-line commands; The new directory, the node command is as follows:
Cp.exec ("Set path=%path%" + "; c:\\", Function (E, stdout, stderr) {
if (!e) {
Console.log ("Environment variable path added; c:\\ success!") ")
}
});
Get the results as follows
There seems to be no problem here. In fact, there is a problem here, the environment variable setting is temporary, when the command line is closed, the environment variable disappears.
Cannot be used the next time the command line is invoked, so there is no way to go on.
In the process of calling, setting environment variables and running commands is a two step:
Cp.exec ("Set path=%path%" + "; c:\\", Function (E, stdout, stderr) {
if (!e) {
Console.log ("Environment variable path added; c:\\ success!") ");
Cp.exec (' rar a-r f:/build/1.rar f:/xc/gulpfile.js ', {encoding: ' binary '},function (e,stin,stout) {
Console.log (e,stin,stout);
});
}
});
The following methods do not work and do not get the correct results.
Calling the WinRAR program directly
There are two ways to solve this, one is a stupid method, and each user manually configures the environment variable. The second is to use the full program path call directly on the command line.
The first method, is the final step, it is not the way to use this method, manual is not the programmer to do.
The second method, the test can be used, runs the following code,
Cp.exec (' C:\\Program Files\\winrar\\winrar.exe ' a-r f:/build/1.rar f:/xc/gulpfile.js ', {encoding: ' binary '});
Will get the following results
It is important to note that when there are spaces in the path, the entire path can be wrapped in quotation marks, otherwise the command-line command will fail to run.
But here must endure, below this window.
Some extensions
1, in the process of compression, will contain the parent folder, how to compress only contains files do not contain folders, compressed package as shown below
2, the contents of the file directory to filter, and the required files and sub-folder compression, such as the file directory as follows, I just want to compress the Htm,txt file inside.
You can pass the FS component of node, then call the Fs.readdirsync () method, and then filter the resulting array.
Issues to be aware of
1. When there are spaces in the program running path, do not forget to enclose the path in quotation marks and escape the symbol.
2, when the RAR compressed file is called, when the filter out of the directory containing the same file name as the current directory files, will also be the folder corresponding to the file compression in.
The file structure is as follows
Run the following command
Cp.exec (' C:\\Program Files\\winrar\\winrar.exe ' a-r f:/build/1.rar f:/xc/gulp.js ', {encoding: ' binary '});
Although there is no corresponding file in this directory, but the RAR program will automatically, traverse subfolders, and finally generate the following compression package
Because this component folder contains gulp.js files. This will make the above filter problems, I have not solved, have not thought of a way.
Summarize
Basic functions are completed, just build a small part of the tool, complete the source file merging, compression, MD5, add a little function. Later, we will find a data to see how to solve the above problems. Here is a hero who has a solution, can comment or private messages, thank you very much.
Appendix I: WinRAR command
After configuration, winrar environment variables, directly run RAR, you can get the following list
Examples of Use:
RAR a Contact.rar Contact.dat
If Contact.rar does not exist, the Contact.rar file will be created, and if Contact.ext is already present in the Contact.rar package, the Contact.ext in the compressed package will be updated
RAR a-r-V2000-SFX Vudroid2.rar Vudroid2
Recursive compression Vudroid2 directory for all files in 2M size sub-volume self-extracting files (self-extracting files are compressed files already contain tools to understand the compression, without the user to install the decompression tool) Vudroid2.part1.sfx,vudroid2.part2.rar,vudroid2.part3.rar and so on, change command A to command m to compress the file and delete it
RAR x Contact.rar
Use the absolute path to extract, if it is rar x Contact.rar ~/hehe/, if the hehe folder to exist. is extracted to the current path of the hehe directory, there is an e parameter, the explanation is pressurized into the current directory, in Ubuntu 10.04 I experimented with, rar E and rar x can be extracted with relative path and absolute path, which I do not know why
RAR A-pzaba Contact1.rar Contact.dat
Compressing contact1.rar files with a password Zaba
Appendix II: Node's child_process component Child_process.exec (command, [options], callback) Source: "Node. js v4.2.4 Manual & Documentation-child_process 》
command
{String} The command to execute, separating the arguments with a space
options
{Object}
cwd
{String} The current working directory of the child process
env
{Object} environment variable key value pair
encoding
{String} encoding (default ' UTF8 ')
shell
{String} The shell on which the command was run (default to '/bin/sh ' on UNIX, ' cmd.exe ' is the default on Windows.) The shell should accept switches on UNIX -c
and should accept switches on Windows /s /c
. In Windows, command-line parsing should be compatible cmd.exe
. )
timeout
{Number} timeout (default = 0)
maxBuffer
{Number} maximum buffering (default = 200*1024)
killSignal
{String} end signal (default ' SIGTERM ')
callback
{Function} callback at end of process and with output
error
{Error}
stdout
{Buffer}
stderr
{Buffer}
- Return: Childprocess Object
Executes a command in the shell and buffers the output.
child = exec(‘cat *.js bad_file | wc -l‘, function (error, stdout, stderr) { console.log(‘stdout: ‘ + stdout); console.log(‘stderr: ‘ + stderr); if (error !== null) { console.log(‘exec error: ‘ + error); }});
The callback parameter is (error, stdout, stderr)
. When it succeeds, it error
will be null
. When an error is encountered, it error
will be an Error
instance, and it err.code
will be the exit code for the child process, and it err.signal
will be set to the signal name of the end process.
The Second optional parameter specifies some options, with the default options:
{ encoding: ‘utf8‘, timeout: 0, maxBuffer: 200*1024, killSignal: ‘SIGTERM‘, cwd: null, env: null }
If it is timeout
greater than 0, it is terminated when the process runs more than a timeout
millisecond. The child process killSignal
ends with a signal (default ‘SIGTERM‘
). maxBuffer
Specifies the maximum amount of data allowed by stdout or stderr, and if this value is exceeded, the child process is terminated.
Node implementation of RAR format compression