当前位置:编程学习 > VB >>

VB读取rar加密文件

分别有个123.txt和abc.jpg文件,用rar压缩并加密(密码"123a")
希望用VB6做个程序:里面有text1和Picture1
按下command1 之后要求程序读取到rar中的资料并显示到控件中

若有代码等加分100
在线等
答案:1、装个UnrarDLL库,没有这个库什么都做不了
2、基于这个库粘贴如下代码,要求有一个窗体一个文本框一个按钮,同时要有一个模块
目前我只实现了读文本,剩下的部分百度HI我我发给你
窗体代码
Option Explicit
Private Declare Function RARGetDllVersion Lib "C:\\Program Files\\UnrarDLL\\unrar.dll" () As Long
Private Declare Function RAROpenArchive Lib "C:\\Program Files\\UnrarDLL\\unrar.dll" (ByRef ArchiveData As RAROpenArchiveData) As Long
Private Declare Function RARCloseArchive Lib "C:\\Program Files\\UnrarDLL\\unrar.dll" (ByVal Handle As Long) As Long
Private Declare Sub RARSetCallback Lib "C:\\Program Files\\UnrarDLL\\unrar.dll" (ByVal hArcData As Long, ByVal Callback As Long, ByVal UserData As Long)
Private Declare Function RARProcessFile Lib "C:\\Program Files\\UnrarDLL\\unrar.dll" (ByVal hArcData As Long, ByVal Operation As Long, ByVal DestPath As String, ByVal DestName As String) As Long
Private Declare Function RARReadHeader Lib "C:\\Program Files\\UnrarDLL\\unrar.dll" (ByVal hArcData As Long, ByRef HeaderData As RARHeaderData) As Long
Private Const RAR_OM_EXTRACT = 1
Private Const RAR_SKIP = 0
Private Const RAR_TEST = 1
Private Const RAR_EXTRACT = 2
Private Type RAROpenArchiveData

ArcName As String

OpenMode As Long

OpenResult As Long

CmtBuf As String

CmtBufSize As Long

CmtSize As Long

CmtState As Long
End Type

Private Type RARHeaderData
ArcName(1 To 260) As Byte
FileName(1 To 260) As Byte

Flags As Long
PackSize As Long
UnpSize As Long
HostOS As Long
FileCRC As Long
FileTime As Long
UnpVer As Long
Method As Long
FileAttr As Long
CmtBuf As String
CmtBufSize As Long
CmtSize As Long
CmtState As Long
End Type

Private hrar As Long
Private Sub Command1_Click()

Dim x As RAROpenArchiveData

Dim Y As RARHeaderData

Dim l As Long

x.ArcName = "C:\\Program Files\\UnrarDLL\\1.rar" & vbNullChar'目标文件名

x.OpenMode = RAR_OM_EXTRACT

x.CmtBuf = String(1024, vbNullChar)

x.CmtBufSize = 1024

hrar = RAROpenArchive(x)

Y.CmtBuf = String(1024, vbNullChar)

Y.CmtBufSize = 1024

RARReadHeader hrar, Y

RARSetCallback hrar, AddressOf RarCallback, UCM_NEEDPASSWORD

l = RARProcessFile(hrar, RAR_SKIP, "C:\\Program Files\\UnrarDLL" & vbNullChar, "C:\\Program Files\\UnrarDLL\\whatsnew.txt" & vbNullChar)

l = RARProcessFile(hrar, RAR_EXTRACT, "C:\\Program Files\\UnrarDLL" & vbNullChar, "C:\\Program Files\\UnrarDLL\\2.txt" & vbNullChar)

l = RARCloseArchive(hrar)
End Sub
模块代码
Public Enum UNRARCALLBACK_MESSAGES

UCM_CHANGEVOLUME

UCM_PROCESSDATA

UCM_NEEDPASSWORD
End Enum
Public Const PASSWD = "111"' 这个是密码
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Byte, ByVal Source As Long, ByVal Length As Long)
Public Declare Sub CopyMemoryPass Lib "kernel32" Alias "RtlMoveMemory" (ByVal Destination As Long, ByVal Source As String, ByVal Length As Long)

Public Function RarCallback(ByVal msg As Long, ByVal UserData As Long, ByVal P1 As Long, ByVal P2 As Long) As Long

Select Case msg

Case UCM_PROCESSDATA


Dim db() As Byte


ReDim db(0 To P2) As Byte


Dim s As String


CopyMemory db(0), P1, P2


s = StrConv(db, vbUnicode)


Form1.Text1.Text = s


RarCallback = -1

Case UCM_NEEDPASSWORD


Dim t As String


t = PASSWD & vbNullChar


CopyMemoryPass P1, t, Len(t)

End Select
End Functio

上一个:有关VB的一些问题。
下一个:怎么样才能学好VB

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,