用VB6.0自制压缩与解压缩程序(三)

用记事本打开 ** modMain.bas ** 文件, copy 以下内容到其中 :

Attribute VB_Name = "modMain"

' ==============================================

' 信息打包与展开 ( 启动模块 )

'

' 功能 : 利用系统所存在的资源自作压缩与解压缩程序

'

' 作 者 : 谢家峰

' 整理日期 : 2004-08-08

' Email :[email protected]

'

' ==============================================

'

Option Explicit

Public WindowsPath As String

Public WindowsSysPath As String

Sub Main ()

Dim BootTrapPath As String

Dim SetupFilePath As String

Dim regExeFilePath As String

Dim regInfo() As String

Dim regStr() As String

Dim regFileName As String

Dim str As String

Dim resultat As Long

Dim resultat2 As Long

Dim res As Double

Dim startinfo As STARTUPINFO

Dim procinfo As PROCESS_INFORMATION

Dim secu As SECURITY_ATTRIBUTES

Dim i As Integer

If App.PrevInstance Then MsgBox " 系统已启动 !", , App.EXEName: End

' 获得系统安装目录

WindowsPath = GetWindowsDir

WindowsSysPath = GetWindowsSysDir

Load frmMain

frmMain.Show

End Sub

用记事本打开 ** modAPI.bas ** 文件, copy 以下内容到其中 :

Attribute VB_Name = "modAPI"

' ==============================================

' 信息打包与展开 ( 所调用的 API 及通用函数模块 )

'

' 功能 : 利用系统所存在的资源自作压缩与解压缩程序

'

' 作 者 : 谢家峰

' 整理日期 : 2004-08-08

' Email :[email protected]

'

' ==============================================

'

Option Explicit

Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As String, ByVal lpCommandLine As String, lpProcessAttributes As SECURITY_ATTRIBUTES, lpThreadAttributes As SECURITY_ATTRIBUTES, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Any, ByVal lpCurrentDriectory As String, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long

Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Public Declare Function GetShortPathName Lib "kernel32" Alias "GetShortPathNameA" (ByVal lpszLongPath As String, ByVal lpszShortPath As String, ByVal cchBuffer As Long) As Long

Public Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpbuffer As String, ByVal nSize As Long) As Long

Public Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpbuffer As String, ByVal nSize As Long) As Long

Public Const gstrSEP_DIR$ = ""

Public Const gstrSEP_URLDIR$ = "/"

Public Const gintMAX_SIZE% = 255

Public Const INFINITE = &HFFFF

Public Type STARTUPINFO

cb As Long

lpReserved As String

lpDesktop As String

lpTitle As String

dwX As Long

dwY As Long

dwXSize As Long

dwYSize As Long

dwXCountChars As Long

dwYCountChars As Long

dwFillAttribute As Long

dwFlags As Long

wShowWindow As Integer

cbReserved2 As Integer

lpReserved2 As Long

hStdInput As Long

hStdOutput As Long

hStdError As Long

End Type

Public Type PROCESS_INFORMATION

hProcess As Long

hThread As Long

dwProcessId As Long

dwThreadId As Long

End Type

Public Type SECURITY_ATTRIBUTES

nLength As Long

lpSecurityDescriptor As Long

bInheritHandle As Long

End Type

Function StripTerminator(ByVal strString As String) As String

Dim intZeroPos As Integer

intZeroPos = InStr(strString, Chr$(0))

If intZeroPos > 0 Then

StripTerminator = Left$(strString, intZeroPos - 1)

Else

StripTerminator = strString

End If

End Function

' -----------------------------------------------------------

' 给目录添加分割线

'

' -----------------------------------------------------------

'

Sub AddDirSep(strPathName As String)

If Right(Trim(strPathName), Len(gstrSEP_URLDIR)) <> gstrSEP_URLDIR And _

Right(Trim(strPathName), Len(gstrSEP_DIR)) <> gstrSEP_DIR Then

strPathName = RTrim$(strPathName) & gstrSEP_DIR

End If

End Sub

' -----------------------------------------------------------

' 调用 API 函数获得 Windows 的系统目录

'

' -----------------------------------------------------------

'

Function GetWindowsSysDir() As String

Dim strBuf As String

strBuf = Space$(gintMAX_SIZE)

If GetSystemDirectory(strBuf, gintMAX_SIZE) > 0 Then

strBuf = StripTerminator(strBuf)

AddDirSep strBuf

GetWindowsSysDir = strBuf

Else

GetWindowsSysDir = vbNullString

End If

End Function

' -----------------------------------------------------------

' 调用 API 函数获取 Windows 目录

'

' -----------------------------------------------------------

'

Function GetWindowsDir() As String

Dim strBuf As String

strBuf = Space$(gintMAX_SIZE)

If GetWindowsDirectory(strBuf, gintMAX_SIZE) > 0 Then

strBuf = StripTerminator$(strBuf)

AddDirSep strBuf

GetWindowsDir = strBuf

Else

GetWindowsDir = vbNullString

End If

End Function

' --------------------------------------

' 测试目录是否存在

'

' --------------------------------------

'

Public Function DirExists(Path As String) As Boolean

On Error Resume Next

' 对于网络地址采用 . 形式

If InStr(Path, "\") Then

DirExists = (Dir$(Path & "\.") <> "")

Else

DirExists = (Dir$(Path & "\nul") <> "")

End If

End Function

' --------------------------------------

' 建立文件夹 ( 含多层结构 )

'

' --------------------------------------

'

Public Sub CreateFloder(floder As String)

Dim i As Integer

Dim Path As String

Dim FloderStr() As String

On Error Resume Next

FloderStr = Split(floder, "")

Path = FloderStr(0)

For i = 1 To UBound(FloderStr) - 1

Path = Path & "" & FloderStr(i)

If Not DirExists(Path) Then

MkDir Path

End If

Next

End Sub

' --------------------------------------

' 获得长文件名的短文件名

'

' --------------------------------------

'

Function GetShortFileName(FileName As String) As String

Dim str As String

str = String(LenB(FileName), Chr(0))

If GetShortPathName(FileName, str, LenB(FileName)) <> 0 Then

str = Left(str, InStr(str, vbNullChar) - 1)

If str = "" Then

GetShortFileName = FileName

Else

GetShortFileName = str

End If

Else

GetShortFileName = FileName

End If

End Function

' --------------------------------------

' 获得文件名

'

' --------------------------------------

'

Public Function GetFileName(fileNamePath As String) As String

Dim AuxVar() As String

AuxVar() = Split(fileNamePath, "", , vbTextCompare)

GetFileName = AuxVar(UBound(AuxVar))

End Function

' --------------------------------------

' 获得文件的扩展名

'

' --------------------------------------

'

Public Function GetExt(FileName As String) As String

Dim AuxVar() As String

On Error Resume Next

AuxVar() = Split (FileName, "", , vbTextCompare)

AuxVar() = Split (AuxVar(UBound(AuxVar)), ".", , vbTextCompare)

GetExt = AuxVar(UBound(AuxVar))

End Function

' --------------------------------------

' 测试文件是否存在 ( 不能测试隐含文件和系统文件 )

'

' --------------------------------------

'

Public Function FileExists(FileName As String) As Boolean

On Error Resume Next

FileExists = (Dir$(FileName) <> "")

End Function

' --------------------------------------

' 查找文件

'

' --------------------------------------

'

Function GetFiles(filespec As String, Optional Attributes As VbFileAttribute) As String()

Dim result() As String

Dim FileName As String, count As Long, path2 As String

Const ALLOC_CHUNK = 50

ReDim result(0 To ALLOC_CHUNK) As String

FileName = Dir$(filespec, Attributes)

Do While Len(FileName)

count = count + 1

If count > UBound(result) Then

ReDim Preserve result(0 To count + ALLOC_CHUNK) As String

End If

result(count) = FileName

FileName = Dir$

Loop

ReDim Preserve result(0 To count) As String

GetFiles = result

End Function

' --------------------------------------

' 转换字符串

'

' --------------------------------------

'

Public Function StringFromBuffer(buffer As String) As String

Dim nPos As Long

nPos = InStr(buffer, vbNullChar)

If nPos > 0 Then

StringFromBuffer = Left$(buffer, nPos - 1)

Else

StringFromBuffer = buffer

End If

End Function

' --------------------------------------

' 写内容到文本文件中

'

' --------------------------------------

'

Sub WriteTextFileContents(text As String, FileName As String, Optional AppendMode As Boolean)

Dim fnum As Integer, isOpen As Boolean

On Error GoTo Error_Handler

fnum = FreeFile()

If AppendMode Then

Open FileName For Append As #fnum

Else

Open FileName For Output As #fnum

End If

isOpen = True

Print #fnum, text

<SPAN lang=EN-US style="FONT-SI

Published At
Categories with Web编程
Tagged with
comments powered by Disqus