⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file.cls

📁 这是一个在vb下实现的各种加密程序,可以实现一般的文本加密和文件加密,但是很多算法都是已经被人破解过的.
💻 CLS
📖 第 1 页 / 共 2 页
字号:
''
' Opens a file for reading through a StreamReader.
'
' @param Path The file to be opened for reading.
' @return A StreamReader used to read from the file with the default encoding (UTF8Encoding).
' @remarks The StreamReader is declared as Cor.NewStreamReader(Path).
'
Public Function OpenText(ByVal Path As String) As StreamReader
    Set OpenText = Cor.NewStreamReader(Path)
End Function

''
' Opens a file for writing as a FileStream.
'
' @param Path The file to be opend for writing to.
' @return A FileStream object used to write to the specified file.
' @remarks The FileStream object is declared as Cor.NewFileStream(Path, FileMode.OpenOrCreate, FileAccess.WriteAccess, FileShare.None).
'
Public Function OpenWrite(ByVal Path As String) As FileStream
    Set OpenWrite = Cor.NewFileStream(Path, FileMode.OpenOrCreate, FileAccess.WriteAccess, FileShare.None)
End Function

''
' Sets the file attributes (ReadOnly, Hidden, ...)
'
' @param sPath The file to set the attributes of.
' @param Attributes The new attributes to set the file to.
' @remarks In order to set a single attribute without unsetting an existing
' attributes, they will first need to be read from the file (GetAttributes), then
' bit manipulated to create the resulting set of desired attributes before setting.
'
Public Sub SetAttributes(ByVal sPath As String, ByVal Attributes As FileAttributes)
    If Len(sPath) = 0 Then _
        Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "sPath")
    
    Call Path.VerifyPath(sPath)
    If API.SetFileAttributes(sPath, Attributes) = BOOL_FALSE Then IOError Err.LastDllError, sPath
End Sub

''
' Sets the time of creation for a file in local time.
'
' @param Path The file to set the time of creation for.
' @param CreationTime A Date or cDateTime object of the new time.
'
Public Sub SetCreationTime(ByVal Path As String, ByVal CreationTime As Variant)
    Call InternalSetFileTime(Path, cDateTime.GetcDateTime(CreationTime).ToFileTime)
End Sub

''
' Sets the time of creation for a file in UTC time.
'
' @param Path The file to set the time of creation for.
' @param CreationTime A Date or cDateTime object of the new time.
'
Public Sub SetCreationTimeUtc(ByVal Path As String, ByVal CreationTime As Variant)
    Call InternalSetFileTime(Path, cDateTime.GetcDateTime(CreationTime).ToFileTimeUtc)
End Sub

''
' Sets the last time the file was accessed in local time.
'
' @param Path The file to set the last access time for.
' @param LastAccessTime A Date or cDateTime object of the new time.
'
Public Sub SetLastAccessTime(ByVal Path As String, ByVal LastAccessTime As Variant)
    Call InternalSetFileTime(Path, , cDateTime.GetcDateTime(LastAccessTime).ToFileTime)
End Sub

''
' Sets the last time the file was accessed in UTC time.
'
' @param Path The file to set the last access time for.
' @param LastAccessTime A Date or cDateTime object of the new time.
'
Public Sub SetLastAccessTimeUtc(ByVal Path As String, ByVal LastAccessTime As Variant)
    Call InternalSetFileTime(Path, , cDateTime.GetcDateTime(LastAccessTime).ToFileTimeUtc)
End Sub

''
' Sets the last time the file was written to in local time.
'
' @param sPath The file to set the last written time for.
' @param LastWriteTime A Date or cDateTime object of the new time.
'
Public Sub SetLastWriteTime(ByVal sPath As String, ByVal LastWriteTime As Variant)
    Call InternalSetFileTime(sPath, , , cDateTime.GetcDateTime(LastWriteTime).ToFileTime)
End Sub

''
' Sets the last time the file was written to in UTC time.
'
' @param sPath The file to set the last written time for.
' @param LastWriteTime A Date or cDateTime object of the new time.
'
Public Sub SetLastWriteTimeUtc(ByVal sPath As String, ByVal LastWriteTime As Variant)
    Call InternalSetFileTime(sPath, , , cDateTime.GetcDateTime(LastWriteTime).ToFileTimeUtc)
End Sub

''
' Reads the entire contents of a file and returns it as a String.
'
' @param Path The path and filename to read in.
' @param Encoding The encoding to be used in reading the file.
' @return A string containing the contents of the file.
'
Public Function ReadAllText(ByVal Path As String, Optional ByVal Encoding As Encoding) As String
    Dim sr As StreamReader
    
    Set sr = Cor.NewStreamReader(Path, Encoding)
    ReadAllText = sr.ReadToEnd
    Call sr.CloseReader
End Function

''
' Reads an entire file into a byte array.
'
' @param Path The path and filename to read into the byte array.
' @return A byte array containing the contents of the file.
'
Public Function ReadAllBytes(ByVal Path As String) As Byte()
    Dim fs      As FileStream
    Dim Ret()   As Byte
    
    Set fs = Cor.NewFileStream(Path, FileMode.OpenExisting, FileAccess.ReadAccess, FileShare.ReadWriteShare)
    
    Dim Length As Long
    Length = fs.Length
    If Length > 0 Then
        ReDim Ret(0 To Length - 1)
        Call fs.ReadBlock(Ret, 0, Length)
    Else
        Ret = Cor.NewBytes()
    End If
    Call fs.CloseStream
    ReadAllBytes = Ret
End Function

''
' Opens a file, reads all lines in a file into an array and closes the files.
'
' @param Path The path and filename of the file to read in.
' @param Encoding The encoding to use to decode the file into text characters.
' @return A String array containing all of the lines in the file.
' @remarks The end of a line is indicated when either a Return (13), LineFeed (10),
' or a Return-LineFeed combination have been encountered.
'
Public Function ReadAllLines(ByVal Path As String, Optional ByVal Encoding As Encoding) As String()
    Dim sr As StreamReader
    Set sr = Cor.NewStreamReader(Path, Encoding)
    
    Dim Ret() As String
    ReDim Ret(31)

    Dim i As Long
    Do
        Ret(i) = sr.ReadLine
        If cString.IsNull(Ret(i)) Then Exit Do
        i = i + 1
        If i > UBound(Ret) Then ReDim Preserve Ret(0 To i * 2 - 1)
    Loop
    Call sr.CloseReader

    If i > 0 Then
        ReDim Preserve Ret(0 To i - 1)
    Else
        Ret = Cor.NewStrings
    End If

    ReadAllLines = Ret
End Function

''
' Opens a files, writes out all contents to the file, then closes the file.
'
' @param Path The path and filename of the file to write to.
' @param Contents The contents to write to the file.
' @param Encoding The encoding to be used when writing to the file.
' @Remarks If the file does not exist, it will be created. If the file already
' exists, it will be overwritten.
'
Public Sub WriteAllText(ByVal Path As String, ByVal Contents As String, Optional ByVal Encoding As Encoding)
    Dim sw As StreamWriter
    
    Set sw = Cor.NewStreamWriter(Path, Encoding)
    Call sw.WriteValue(Contents)
    Call sw.CloseWriter
End Sub

''
' Opens a file, writes all bytes to the file, then closes the file.
'
' @param Path The path and filename of the file to write to.
' @param Bytes The bytes to be written to the file.
' @remarks If the file doesn't exist it will be created. If the file already
' exists, it will be overwritten.
'
Public Sub WriteAllbytes(ByVal Path As String, ByRef Bytes() As Byte)
    If cArray.IsNull(Bytes) Then _
        Throw Cor.NewArgumentNullException(Environment.GetResourceString(ArgumentNull_Array), "Bytes")
    
    Dim fs As FileStream
    Set fs = Cor.NewFileStream(Path, FileMode.Create)
    Call fs.WriteBlock(Bytes, LBound(Bytes), cArray.GetLength(Bytes))
    Call fs.CloseStream
End Sub

''
' Opens a file, writes all strings to the file with appended new line values, then closes the file.
'
' @param Path The path and filename of the file to write to.
' @param Contents The strings to be written to the file.
' @param Encoding The encoding to be used when writing to the file.
' @remarks If the file doesn't exist it will be created. If the file already
' exists, it will be overwritten.
'
Public Sub WriteAllLines(ByVal Path As String, ByRef Contents() As String, Optional ByVal Encoding As Encoding)
    If cArray.IsNull(Contents) Then _
        Throw Cor.NewArgumentNullException(Environment.GetResourceString(ArgumentNull_Array), "Contents")
    
    Dim sw As StreamWriter
    Set sw = Cor.NewStreamWriter(Path, Encoding)
    
    Dim i As Long
    For i = LBound(Contents) To UBound(Contents)
        Call sw.WriteLine(Contents(i))
    Next i
    Call sw.CloseWriter
End Sub


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Friend Interface
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Friend Function GetFileData(ByVal p As String, ByRef Data As WIN32_FILE_ATTRIBUTE_DATA) As Long
    p = cString.TrimEnd(p, mDirectorySeparators)
    
    Call Path.VerifyPath(p, True)
    If API.GetFileAttributesEx(p, 0, Data) = BOOL_FALSE Then
        GetFileData = GetFileDataFallback(p, Data)
    End If
End Function


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Private Helpers
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Function GetFileDataFallback(ByRef Path As String, ByRef Data As WIN32_FILE_ATTRIBUTE_DATA) As Long
    Dim FileData As WIN32_FIND_DATA
    
    If API.FindFirstFile(Path, FileData) = INVALID_HANDLE_VALUE Then
        GetFileDataFallback = Err.LastDllError
    Else
        Data.dwFileAttributes = FileData.dwFileAttributes
        Data.ftCreationTime = FileData.ftCreationTime
        Data.ftLastAccessTime = FileData.ftLastAccessTime
        Data.ftLastWriteTime = FileData.ftLastWriteTime
        Data.nFileSizeHigh = FileData.nFileSizeHigh
        Data.nFileSizeLow = FileData.nFileSizeLow
    End If
End Function

Private Sub InternalSetFileTime(ByRef sPath As String, Optional ByVal CreationTime As Variant, Optional ByVal LastAccessTime As Variant, Optional ByVal LastWriteTime As Variant)
    Dim cnt As Long
    Dim lat As Long
    Dim lwt As Long
    
    If Not IsMissing(CreationTime) Then cnt = VarPtr(CreationTime) + 8
    If Not IsMissing(LastAccessTime) Then lat = VarPtr(LastAccessTime) + 8
    If Not IsMissing(LastWriteTime) Then lwt = VarPtr(LastWriteTime) + 8
    
    If Len(sPath) = 0 Then _
        Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "sPath")
    Call Path.VerifyPath(sPath)
    
    Dim FileHandle As Long
    FileHandle = API.CreateFile(sPath, FileAccess.WriteAccess, FileShare.None, 0, FileMode.OpenExisting, FILE_ATTRIBUTE_NORMAL, 0)
    If FileHandle = INVALID_HANDLE Then IOError Err.LastDllError, sPath
    If SetFileTime(FileHandle, ByVal cnt, ByVal lat, ByVal lwt) = BOOL_FALSE Then
        Dim e As Long
        e = Err.LastDllError
        Call CloseHandle(FileHandle)
        IOError e, sPath
    End If
    Call CloseHandle(FileHandle)
End Sub

Private Sub FillFileData(ByRef Path As String, ByRef Data As WIN32_FILE_ATTRIBUTE_DATA)
    If Len(Path) = 0 Then _
        Throw Cor.NewArgumentException(Environment.GetResourceString(Argument_EmptyPath), "sPath")
    
    Dim e As Long
    e = GetFileData(Path, Data)
    If e <> NO_ERROR Then IOError e, Path
End Sub


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'   Class Events
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub Class_Initialize()
    mDirectorySeparators = cArray.NewArray(ciInteger, Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)
End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -