<%
'/******************************* binary file operation class ************************************
"* * Author: Dead Fish in the water (dead fish)
' * * Date: September 7, 2004
"'/*blog:http://blog.csdn.net/bpfish
''/*
'/* Usage:
"'/*dim obj,sdata,ipos,isize
"'/*set obj=new Fileclass
"/*obj." OpenFile "File absolute path address", "Open with" ' Open file, open with the same value as the Mode property of ADODB.stream
"* * The number of related values open mode: 1 = Read Only 2 = write only 3 or null = read-write
"/*obj." Appendto "[Append file] Absolute path address" Appends the currently open file to the end of the append file
"/*obj." Move value ' moves the current open file pointer relative to the [value] byte
"/*obj." MoveTo value ' "Moves the currently open file pointer to the file header in absolute terms [value] bytes
"/*sdata=obj." Read (Numeric)
'/* ' Description: obj. Read (value) reads the currently opened file [value] bytes '/* Note: The read position is related to the file pointer, and if the value is 0, the data is read from the current file position until the end of the file
"/*sdata=obj." ReadAll ' reads all data for the currently open file
"/*ipos=obj." Position ' Returns the file pointer position of the current file (Position property is read-only and cannot be written)
"/*isize=obj." Size ' Returns the amount of the current file, that is, the total number of bytes
'/*if obj. Eof Then Response.Write "End of File"
'/* Note: obj. EOF is to determine whether the file pointer is moved to the end of the file, when obj. When Eof=true
'/* obj. Position=obj. Size. But obj. Position=obj. Size is not necessarily obj.eof=true
"/*obj." SaveAs "Save as file name", "Saved Mode" "Saves the currently open file as a new file, the value of the save mode is 1 or 2 1 = not covered 2 = overwrite
"/*obj." CloseFile ' Closes the currently open file
"/*obj." NewFile "New filename", byte stream ' creates a new file for the [byte data stream], overwrites if the file exists, or fails the new file if the non-byte data stream
"/*obj." AppendFile "File 1", "File 2", "Generate File"
'/* ' merges [file 1] and [file 2] into a new [makefile], and if [makefile] exists, the merge fails! If the makefile is set to NULL, the [file 2] is merged into [file 1].
'/*response.write obj. GetText (Byte data stream) converts a byte data stream into a string and returns
"'/*set obj=nothing
''/*
'/* Description: Byte data stream is binary data
"/* The following methods may not need to first openfile:newfile and AppendFile and GetText methods, otherwise you must first use the OpenFile method to open the file
''/**********************************************************************************
Class Fileclass
Private objstream ' ADODB. Stream object variable
Private Openflag ' Whether there is an open file
Private fileiseof
''/**********************************************************************************
'/* Function name: Initialize class
''/**********************************************************************************
Private Sub Class_Initialize
Openflag=false
Fileiseof=true
End Sub
''/**********************************************************************************
'/* Function name:
''/**********************************************************************************
Private Sub Class_Terminate
Call CloseFile
Set objstream=nothing
End Sub
''/**********************************************************************************
'/* Function name: Gets the size of the file (-1 = no file opened)
''/**********************************************************************************
Public Property Get Size
If Openflag Then
Size=objstream.size
Else
Size=-1
End If
End Property
''/**********************************************************************************
'/* Function name: Gets the file pointer position of the current file (-1 = no file opened)
''/**********************************************************************************
Public Property Get Position
If Openflag Then
Position=objstream.position
Else
Position=-1
End If
End Property
''/**********************************************************************************
'//Function name: Gets whether the current file is at the end of the file (True = file Tail False = no)
"* * Note: When returned to true, the file is only at the last byte, and returns True because it was tried to move the pointer to the end of the file (larger than the size of the file)
'/* If this value is called externally, it is recommended that when the value is true, stop any read data operations, or there may be data returned
''/**********************************************************************************
Public Property Get Eof
Eof=fileiseof
End Property
''/**********************************************************************************
'/* Function name: Open file
'/* parameter: filename = file name to open, absolute address
'/*openmode = The schema value of the open file is the same as the Mode property of the ADODB.stream object
''/**********************************************************************************
Public Function OpenFile (ByVal filename,byval OpenMode)
On Error Resume Next
Dim Sfilename,imode
If Openflag Then ' If a file is already open, close the previous file
Call CloseFile
End If
Sfilename=trim (FileName)
If sfilename= "" Then
Fileiseof=true
Openflag=false
Openfile=false
Exit Function
End If
Imode=cint (OpenMode) ' If you don't enter a number, use the default 3 open mode (read-write)
If err.number<>0 Then
Imode=3
Err.Clear
End If
If imode<>0 and Imode<>1 and Imode<>2 and Imode<>3 and Imode<>4 and imode<>8 <>12 and imode<>16 and imode<>4194304 Then
Imode=3 ' If the number entered is not in the Adodb.readstream mode property The value accepted is the default value of 3
End If
' On Error Goto 0
If not Initobjstream Then ' initializes the object, and returns a false value if it cannot be initialized
Fileiseof=true
Openflag=false
Openfile=false
Exit Function
End If
Objstream.type=1
Objstream.mode=imode
objStream.Open
objStream.LoadFromFile sFileName
If err.number<>0 Then
Err.Clear
Exit Function
End If
Fileiseof=false
Openflag=true
Openfile=true
End Function
''/**********************************************************************************
'/* Function name: Read binary content of specified length
'/* parameter: numbytes = number of bytes to read (NumBytes = 0 gets all content after the current file pointer)
'/* return: Byte data read
''/**********************************************************************************
Public Function Read (ByVal numbytes)
Dim Inum
If not openflag Then Exit Function
If not Isinteger (numbytes) Then Exit Function
INUM=CLNG (numbytes)
Fileiseof=false
If inum<0 Then
Exit Function
ElseIf (inum+objstream.position) >objstream.size Then
"' Inum=objstream.size-objstream.position
Fileiseof=true
End If
If inum=0 Then
Read=objstream.read
Else
Read=objstream.read (Inum)
End If
End Function
''/**********************************************************************************
'/* Function name: Read all the contents of the file
'/* Parameter:
'/* return: Read all byte data for file
''/**********************************************************************************
Public Function ReadAll
If not openflag Then Exit Function
Objstream.position=0
Readall=objstream.read
Fileiseof=true
End Function
''/**********************************************************************************
'/* Function name: Save File as
'/* parameter: FileName = Save As file absolute path Options = File Save As mode (1= 2 = overwrite)
'/* return: Read all byte data for file
''/**********************************************************************************
Public Function SaveAs (ByVal filename,byval Options)
On Error Resume Next
Dim sfilename,ioption
If not Openflag Then
Saveas=false
Exit Function
End If
Sfilename=trim (FileName)
If sfilename= "" Then
Saveas=false
Exit Function
End If
Ioption=cint (Options)
If err.number<>0 Then ' if not numeric data, then the default value of 1 = Not overwrite method to save
Ioption=1
Err.Clear
End If
If ioption<>1 and ioption<>2 Then ' if the value of the options is not in the range of 1 and 2, the default value of 1 is used
Ioption=1
End If
Objstream.savetofile sfilename,ioption
If err.number<>0 Then
Err.Clear
Saveas=false
Exit Function
End If
Saveas=true
End Function
''/**********************************************************************************
'/* Function name: Append file data to another file
'/* parameter: filename = file absolute path appended
'/* return: True = Append Data succeeded False = Append file data failed
''/**********************************************************************************
Public Function appendto (ByVal FileName)
On Error Resume Next
Dim Sfilename,appendobj
If not Openflag Then
Appendto=false
Exit Function
End If
Sfilename=trim (FileName)
If sfilename= "" Then
Appendto=false
Exit Function
End If
"Open the file to be appended
Set appendobj=server.createobject ("ADODB. Stream ")
Appendobj.type=1
Appendobj.mode=3
Appendobj.open
Appendobj.loadfromfile sFileName
If err.number<>0 Then
Err.Clear
Appendto=false
Exit Function
End If
Appendobj.position=appendobj.size ' Move pointer to end of file
Appendobj.write readall ' append data to the first file
Appendobj.savetofile sfilename,2 ' Save as Self
If err.number<>0 Then
Err.Clear
Appendto=false
Exit Function
End If
Appendobj.close
Set appendobj=nothing
Appendto=true
End Function
''/**********************************************************************************
'/* Function name: Save Sdata data as a file
'/* parameter: filename = file absolute path appended
'/* return: True = Append Data succeeded False = Append file data failed
''/**********************************************************************************
Public Function NewFile (ByVal filename,byref sData)
On Error Resume Next
Dim Sfilename,appendobj
Sfilename=trim (FileName)
If sfilename= "" Then
Newfile=false
Exit Function
End If
"Open the file to be appended
Set appendobj=server.createobject ("ADODB. Stream ")
Appendobj.type=1
Appendobj.mode=3
Appendobj.open
If err.number<>0 Then
Err.Clear
Newfile=false
Exit Function
End If
If Left (TypeName (SData), 4) = "Byte" Then "is a stream of bytes data to append
Appendobj.write sData ' append data to the first file
Appendobj.savetofile sfilename,2 ' Save as Self
Else
Newfile=false
Exit Function
End If
If err.number<>0 Then
Err.Clear
Newfile=false
Exit Function
End If
Appendobj.close
Set appendobj=nothing
Newfile=true
End Function
''/**********************************************************************************
'/* Function name: file merge
'/* parameter: Appendfilename = merge file 1 sourcefilename = Merge file 2 targetfilename = merged file (if empty, save as first file name)
'/* return: True = Merge Succeeded False = merge failed
''/**********************************************************************************
Public Function appendfile (ByVal appendfilename,byval sourcefilename,byval targetfilename)
On Error Resume Next
Dim Sfilename1,sfilename2,appendobj1,appendobj2
Dim Stargetfile
Sfilename1=trim (Appendfilename)
Sfilename2=trim (sourceFileName)
Stargetfile=trim (TargetFileName)
If sfilename1= "" Or sfilename2= "" Then
Appendfile=false
Exit Function
End If
' Open the file to be appended and appended
Set appendobj1=server.createobject ("ADODB. Stream ")
Set appendobj2=server.createobject ("ADODB. Stream ")
Appendobj1.type=1
Appendobj1.mode=3
Appendobj1.open
Appendobj1.loadfromfile sFileName1
Appendobj2.type=1
Appendobj2.mode=3
Appendobj2.open
Appendobj2.loadfromfile sFileName2
If err.number<>0 Then
Err.Clear
Appendfile=false
Exit Function
End If
Appendobj1.position=appendobj1.size ' Move pointer to end of file
Appendobj1.write appendobj2.read ' append data to the first file
If stargetfile= "" Then
Appendobj1.savetofile sfilename1,2 ' Save as Self
Else
Appendobj1.savetofile stargetfile,1 ' Save as other file without mandatory override
End If
If err.number<>0 Then
Err.Clear
Appendfile=false
Exit Function
End If
Appendobj1.close
Set appendobj1=nothing
Appendobj2.close
Set appendobj2=nothing
Appendfile=true
End Function
''/**********************************************************************************
'/* Function name: Move file pointer (relative move)
'/* parameter: Tonum = number of bytes to move
''/*
''/**********************************************************************************
Public Sub-Move (ByVal tonum)
Dim Inum
If not openflag Then Exit Sub
If not Isinteger (tonum) Then Exit Sub
INUM=OBJSTREAM.POSITION+CLNG (Tonum)
Fileiseof=false
If inum<0 Then
Inum=0
ElseIf inum>objstream.size Then
Inum=objstream.size
Fileiseof=true
End If
Objstream.position=inum
End Sub
''/**********************************************************************************
'/* Function name: Move file pointer (absolute move)
'/* parameter: Tonum = number of bytes to move
''/*
''/**********************************************************************************
Public Sub MoveTo (ByVal tonum)
Dim Inum
If not openflag Then Exit Sub
If not Isinteger (tonum) Then Exit Sub
INUM=CLNG (Tonum)
Fileiseof=false
If inum<0 Then
Inum=0
ElseIf inum>objstream.size Then
Inum=objstream.size
Fileiseof=true
End If
Objstream.position=inum
End Sub
''/**********************************************************************************
'/* Function name: Close Open File
''/**********************************************************************************
Public Sub CloseFile
On Error Resume Next
If not IsNothing (objstream) Then
objStream.Close
End If
Openflag=false
Fileiseof=true
End Sub
''/**********************************************************************************
'/* Function name: Initializing Objstream Object
''/**********************************************************************************
Private Function Initobjstream
On Error Resume Next
If isnothing (objstream) Then
Set objstream=server.createobject ("ADODB. Stream ")
If err.number<>0 Then
Err.Clear
Initobjstream=false
Exit Function
End If
End If
Initobjstream=true
End Function
''/**********************************************************************************
'//Function name: Gets binary converted to string data
''/**********************************************************************************
Public Function GetText (ByRef vIn)
On Error Resume Next
Dim Strreturn, I, Thischarcode, Nextcharcode
Strreturn = ""
For i = 1 to LenB (vIn)
Thischarcode = AscB (MidB (VIn, I, 1))
If Thischarcode < &h80 Then
Strreturn = Strreturn & Chr (Thischarcode)
Else
Nextcharcode = AscB (MidB (vIn, i + 1, 1))
Strreturn = Strreturn & Chr (CLng (thischarcode) * &h100 + CInt (nextcharcode))
i = i + 1
End If
Next
GetText = Strreturn
End Function
''/**********************************************************************************
'/* Function name: Determine if obj object is null
''/**********************************************************************************
Private Function IsNothing (OBJ)
If not IsObject (OBJ) Then
Isnothing=true
Exit Function
End If
If OBJ is nothing Then
Isnothing=true
Exit Function
End If
If IsNull (OBJ) Then
Isnothing=true
Exit Function
End If
Isnothing=false
End Function
''/**********************************************************************************
'/* Function name: Determine whether the numeric value (0,1,2,3,4,5...........9)
''/**********************************************************************************
Private Function Isinteger (Para)
On Error Resume Next
Dim Str
Dim L,i
If ISNULL (para) Then
Isinteger=false
Exit Function
End If
STR=CSTR (para)
If Trim (str) = "" Then
Isinteger=false
Exit Function
End If
L=len (str)
For I=1 to L
If Mid (str,i,1) > "9" Or Mid (str,i,1) < "0" Then
Isinteger=false
Exit Function
End If
Next
Isinteger=true
If err.number<>0 Then Err.Clear
End Function
End Class
%>