How to pack and compress multiple uploaded files by calling winzip command line on the server side?
To solve this problem, we must first understand Windows scripting Host, referred to as WSH! Here is a quote from Microsoft's explanation:
********************************************************* ***************************
* WSH is a type of Microsoft's scripting technology series. Simply put, it provides a scripting environment, *
* In this environment, some objects are predefined, and other objects in COM can also be used. *
* He uses a script engine to interpret and execute scripts. Microsoft itself supports VBscript and Jscript, *
* Third parties can also develop their own scripting engines. *
********************************************************* ***************************
To be specific, you first compile some script files (Microsoft comes with some examples, with suffixes.vbs or .js).
Then use a program to explain and execute it, and this program is called Windows scripting Host.
The name is Wscript.exe (or Cscript.exe on the command line), you can check your machine
If you have these two files in it, you will know if there is WSH. (win2000 is under winnt/system32/)
This is very similar to a batch file, except that the file is not a command line, but a script written in the script language.
Let’s briefly introduce several built-in objects that come with WSH, including:
1. Object provided by Wscript.exe
Wscript is exposed to the scripting engine as Wscript.
WshArguments Not public; accessed through the Wscript.Arguments property. enter
2. Object provided by WSHom.Ocx.
WshShell Automatic object. ProgID is Wscript.WshShell.
(Note: This is what we want to use, you can execute the dos command)
WshNetwork Automatic object. ProgID is Wscript.WshNetwork.
WshShortcut is not public; accessed through the WshShell.CreateShortcut method.
WshUrlShortcut is not public; accessed through the WshShell.CreateShortcut method.
WshCollection is not published; accessed through the WshNetwork.EnumNetworkDrives or WshNetwork.EnumPrinterConnection method.
WshEnvironment is not public; accessed through the WshShell.Environment property.
WshSpecialFolders is not public; accessed through the WshShell.Folder property.
They can mainly complete the acquisition of environment variables, network login, drive mapping, and quick intercept creation.
Program loading, special folders (such as system folders) information acquisition and other functions.
If your system supports ADO and other COM components, you can also use it.
The following example demonstrates opening a WordPad to view a text file, creating a text file and writing a
You can copy it to the writing board, then save it with .vbs as the suffix, and then double-click it.
'test.vbs
'************************
'The following is to start the program with the SHELL object
'************************
Set WshShell = Wscript.CreateObject(Wscript.Shell)
WshShell.Run (notepad & Wscript.scriptFullName)
'******************************************************
'Use COM object scripting.FileSystemObject to operate text files
'******************************************************
Set fs = Wscript.CreateObject(scripting.FileSystemObject)
Set a = fs.CreateTextFile(c:/testfile.txt, True)
a.WriteLine (This is a test.)
a.Close
It can also be applied in web programming languages such as Asp
<script language=VBscript.Encode runat=server>
'The above starts the program with the SHELL object
Set WshShell = server.CreateObject(Wscript.Shell)
IsSuccess = WshShell.Run (D:/winnt/system32/cmd.exe ,1, true)
if IsSuccess = 0 Then
The Response.write command was successfully executed!
else
The Response.write command failed to execute! Insufficient permissions or the program cannot run in DOS state
end if
</script>
Note:
1. The runat=server must have
2.Set WshShell = Wscript.CreateObject(Wscript.Shell)
To change to Set WshShell = server.CreateObject(Wscript.Shell),
3. Parameter 1 represents SW_SHOWNORMAL, activates and displays a window. If the window is minimized or maximized, it will be restored to its original size and position.
4.TRUE represents an error that returns execution, False or is specified that the script continues to execute without waiting for the process to end.
5. Calling the built-in object of WSH, it can be called like calling functions and procedures.
For example, call WshShell.Run (D:/winnt/system32/cmd.exe ,1, true)
If you are interested in WSH and want to know more, please check it out
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsconwshbasics.asp
http://www.dev-club.com/club/bbs/showEssence.asp?id=11136
Now let's get back to the point and see how to compress and decompress files!
Everyone knows that winzip is easy to decompress and compress files, but how to call them through programs and command lines?
Of course the author of winzip has developed
WinZip Command Line Support Add-On Version 1.0
Everyone can go to http://www.winzip.com/wzcline.htm to download wzcline.exe!
The premise is that the machine must install winzip8.0 or higher support. If you are not winzip8.0, go to
http://www.winzip.com/download.htm Download!
After downloading, just install it!
The winzip command line help file and program WZZIP.exe, WZUNZIP.EXE will be generated in the winzip directory.
You can start running calls:
For example: c:/program files/winzip/wzzip myfile.zip
You can also copy two files here to any directory and run them directly under the dos window.
For example: wzzip.exe myfile.zip
You can add set path=c:/windows;c:/program files/winzip;
You can call it anywhere without adding a pass!
Now let’s briefly understand the basic usage of the two commands in the help
Use WZZIP.exe to compress files:
General format: wzzip [options] zipfile [@listfile] [files...]
[options] include:
-a Default operation, compressed file
-a+ compress the file and delete the file to be compressed
-b[drive|path] is to create temporary compressed files on another drive
-d Delete the target file specified in the zip file
-e<x|n|f|s|0> is to set the compression ratio, x is the maximum and 0 is the minimum
-f Replace the file that exists in the zip file
-h|-? View Help
-v Create a list of compressed files
-@list Create a file that contains all the files to be decompressed, and then compress it by the included file name
............. (For other details, please refer to the help file)
[@listfile] is a list information record of compressed files
[files...] is the multiple files to be compressed, separated by spaces, or a wildcard
example:
Compress all files in the current directory
wzzip test.zip *.*
Compress all files of type txt
wzzip test.zip *.txt
Compress two files
wzzip test.zip abc.txt def.txt
Compress all files of type txt except abc.txt
wzzip -xABC.TXT test.zip *.txt
Compress all files and subdirectories of type txt under D:/docs
wzzip -rp test.zip d:/docs/*.txt
Update the file in zipit.1st to test.zip
wzzip -u test.zip @Zipit.lst
List the list of compressed files
wzzip -v test.zip
Use WZUNZIP.exe to unzip the file:
By format: wzunzip [options] zipfile [@listfile] [path] [files...]
[options] include:
-c[m] decompression is to display the file list in the dos screen
-d Reconstruct the directory structure in the zip file
-f Only decompress files with the same name as the current folder in the zip file. If not, decompress them.
-jhrs Ignore the hidden, read-only, and system properties of files in zip files
-Jhrs only decompress files with hidden, read-only, and system properties
-n Only decompress the new file, replace if the file to be decompressed is newer than the existing one.
-o No need to use yes to determine whether to replace the file
-v Create a list of compressed files
-@list Create a file that contains all the files to be decompressed, and then decompress it by the included file name
............. (For other details, please refer to the help file)
For example:
Create all files to the current directory
wzunzip test.zip
Create abc.txt from test.zip to the current directory
wzunzip test.zip abc.txt
Create the directory structure and file in test.zip to the current directory
wzunzip -d test.zip
Create the directory structure and file in test.zip to c:/docs
wzunzip -d test.zip c:/docs Create a file containing the file name in files.ist from test.zip
wzunzip test.zip @files.lst
Show the contents of the file list of test.zip
wzunzip -v test.zip
Display the contents of all file types txt in the compressed file
wzunzip -v test.zip *.txt
With the above preparation, it will be easy for us to write VBS to perform file decompression and compression:
'test.vbs
'************************
'The above starts the program with the SHELL object
'************************
Set WshShell = Wscript.CreateObject(Wscript.Shell)
WshShell.Run (c:/wzzip.exe c:/test.zip c:/a.txt c:/b.txt)
'test.asp
'************************
'The above starts the program with the SHELL object
'************************
<script language=VBscript.Encode runat=server>
'The above starts the program with the SHELL object
Set WshShell = server.CreateObject(Wscript.Shell)
IsSuccess = WshShell.Run ( c:/wzzip.exe c:/test.zip c:/a.txt c:/b.txt ,1, true)
if IsSuccess = 0 Then
The Response.write command was successfully executed!
else
The Response.write command failed to execute! Insufficient permissions or the program cannot run in DOS state
end if
</script>
It's very simple. If we combine file upload with this, we will produce many functions that will be used. like:
After uploading multiple files with the upload component, you can package multiple files to provide them to others for downloading zip files
Or after uploading a zip file, you can use the program to decompress it to provide it to others for a single file download
Very convenient for users! I will write down these two changes in future articles! There are inevitably omissions in the article, please correct me!
All the above examples have been tested on win2000+iis5.0!