在我们运用VB.NET这门语言时,我们尝尝会遇到VB.NET压缩和解压缩的问题,如何能更好的实现VB.NET压缩的问题,在这里我们用一个实例给大家详细分析一下,希望可以给大家带来帮助。
VB.NET压缩和解压缩实现代码:
- PublicEnumZip
- Zip=0'压缩
- UnZip=1'解压缩
- EndEnum
- '压缩、解压缩
- PublicFunctionZipFile(ByValinDirPathAsString,OptionalByVal_zipAsZip=Zip.Zip,_
- OptionalByValstrFileNameAsString="C:\BACKUP")AsBoolean
- DimInfoAsNewInformation
- DimPrintMessageAsNewErrorinf
- DimstrCommandAsString
- If_zip=Zip.ZipThen
- DimdirectoryEntriesAsString()
- directoryEntries=System.IO.Directory.GetFileSystemEntries(inDirPath)
- DimstrFileAsString,strFileAllAsString
- ForEachstrFileIndirectoryEntries
- strFileAllstrFileAll=strFileAll&"-ep1"&strFile
- Next
- DimstrPassAsString=InputBox(Info.NOR_InputPassword,Info.NOR_PasswordInput)
- IfstrPass.Length>0Then
- strCommand="winrarA-hp"+strPass+"-ep1"
- Else
- strCommand="winrarA-ep1"
- EndIf
- strCommandstrCommand=strCommand&Space(1)&strFileName&Space(1)&strFileAll
- Try
- IfSystem.IO.File.Exists(strFileName)Then
- IfMsgBox(Info.NOR_ReplaceFile,_
- MsgBoxStyle.OKCancel+_
- MsgBoxStyle.Information,Info.NOR_Prompt)=MsgBoxResult.CancelThen
- ReturnFalse
- Else
- Shell(strCommand,AppWinStyle.NormalFocus,True)
- EndIf
- Else
- Shell(strCommand,AppWinStyle.NormalFocus,True)
- EndIf
- MsgBox(Info.SUC_ZipBackSuccess,MsgBoxStyle.OKOnly+MsgBoxStyle.Information,Info.NOR_Prompt)
- ReturnTrue
- Catch
- PrintMessage.WriteErr("Cls_Common->ZipFile",Info.ERR_ZipBackError)
- MsgBox(Info.ERR_ZipBackError,MsgBoxStyle.OKOnly+MsgBoxStyle.Information,Info.NOR_Prompt)
- ReturnFalse
- EndTry
- Else
- DimOpenDialogAsNewOpenFileDialog
- OpenDialog.Filter="还原压缩数据*.rar|*.rar"
- IfOpenDialog.ShowDialog=DialogResult.CancelThen
- OpenDialog.Dispose()
- ReturnFalse
- EndIf
- strFileName=OpenDialog.FileName
- DimstrRePassAsString=InputBox(Info.NOR_InputPassword,Info.NOR_PasswordInput)
- IfstrRePass.Trim.Length>0Then
- strCommand="winrarX-hp"+strRePass.Trim+"-o+-inul-ep2"
- Else
- strCommand="winrarX-o+-inul-ep2"
- EndIf
- strCommandstrCommand=strCommand&Space(1)&strFileName&Space(1)&Application.StartupPath
- OpenDialog.Dispose()
- Try
- Shell(strCommand,AppWinStyle.NormalFocus,True)
- MsgBox(Info.SUC_ZipRevertSuccess,MsgBoxStyle.OKOnly+MsgBoxStyle.Information,Info.NOR_Prompt)
- ReturnTrue
- Catch
- PrintMessage.WriteErr("Cls_Common->ZipFile",Info.ERR_ZipRevertError)
- MsgBox(Info.ERR_ZipRevertError,MsgBoxStyle.OKOnly+MsgBoxStyle.Information,Info.NOR_Prompt)
- ReturnFalse
- EndTry
- EndIf
- 'Debug.Write(strCommand)
- EndFunction
你的应用程序的bin下要有一个winrar的可执行文件.
【编辑推荐】