Design and use Class 1 in ASP

Source: Internet
Author: User
'-----------------------------------------------------
'Description: ASP packaging class
'Author: ash (quxiaohui_0@163.com)
'Link: http://asp2004.net http://blog.csdn.net/iuhxq http://bbs.asp2004.net
& Apos; version: 1.0 Beta
'Copyright: this file is free of charge, but do not remove the copyright information.
'-----------------------------------------------------
Class RAR
Dim files, packname, S, S1, S2, rootpath, FSO, F, Buf
Private sub class_initialize
Randomize
Dim rannum
Rannum = int (90000 * RND) + 10000
Packname = year (now) & month (now) & Day (now) & hour (now) & minute (now) & Second (now) & rannum & ". asp2004"

Rootpath = server. mappath ("./")

Set files = server. Createobject ("scripting. Dictionary ")
Set FSO = server. Createobject ("scripting. FileSystemObject ")

Set S = server. Createobject ("ADODB. Stream"): S. Open: S. type = 1
Set S1 = server. Createobject ("ADODB. Stream"): s1.open: s1.type = 1
Set S2 = server. Createobject ("ADODB. Stream"): s2.open: s2.type = 2
End sub

Private sub class_terminate
S. Close: Set S = nothing
S1.close: Set S1 = nothing
S2.close: Set S2 = nothing

Set FSO = nothing
End sub

Public sub add (OBJ)
If FSO. fileexists (OBJ) then
Set F = FSO. GetFile (OBJ)
Files. Add OBJ, F. Size
Elseif FSO. folderexists (OBJ) then
Files. Add OBJ,-1
Set F = FSO. getfolder (OBJ)
Set fc = f. Files
For each F1 in FC
Add (lcase (f1.path ))
Next
End if
End sub

Public sub pack
Dim Str
A = files. Keys
B = files. Items
For I = 0 to files. Count-1
If B (I)> = 0 then
S. loadfromfile (A (I ))
Buf = S. Read
If not isnull (BUF) Then s1.write (BUF)
End if
STR = STR & B (I) & ">" & replace (a (I), rootpath, "") & vbcrlf
Next
STR = CSTR (right ("000000000" & Len (STR), 10) & Str
Buf = texttostream (STR)
S. Position = 0
S. Write Buf
S1.position = 0
S. Write s1.read
S. seteos
S. savetofile (packname)
End sub

Public sub unpack

If not FSO. folderexists (rootpath) then
FSO. createfolder (rootpath)
End if
Dim size
'Size of the converted file
S. loadfromfile (packname)
Size = CINT (streamtotext (S. Read (10 )))
STR = streamtotext (S. Read (size ))
Arr = Split (STR, vbcrlf)

For I = 0 to ubound (ARR)-1
Arrfile = Split (ARR (I), "> ")
If arrfile (0) <0 then
If not FSO. folderexists (rootpath & arrfile (1) then
FSO. createfolder (rootpath & arrfile (1 ))
End if
Elseif arrfile (0)> = 0 then
If FSO. fileexists (rootpath & arrfile (1) then
FSO. deletefile (rootpath & arrfile (1 ))
End if
S1.position = 0
Buf = S. Read (arrfile (0 ))
If not isnull (BUF) Then s1.write (BUF)
S1.seteos
S1.savetofile (rootpath & arrfile (1 ))
End if
Next
End sub

Public Function streamtotext (Stream)
If isnull (Stream) then
Streamtotext = ""
Else
Set Sm = server. Createobject ("ADODB. Stream"): Sm. Open: Sm. type = 1
SM. Write (Stream)
SM. Position = 0
SM. type = 2
SM. charset = "gb2312"
SM. Position = 0
Streamtotext = Sm. readtext ()
SM. Close: Set Sm = nothing
End if
End Function

Public Function texttostream (text)
If text = "" then
Texttostream = "" 'How do I write it here? Empty stream?
Else
Set Sm = server. Createobject ("ADODB. Stream"): Sm. Open: Sm. type = 2: Sm. charset = "gb2312"
SM. writetext (text)
SM. Position = 0
SM. type = 1
SM. Position = 0
Texttostream = Sm. Read
SM. Close: Set Sm = nothing
End if
End Function
End Class

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.