首页 > 开发 > 综合 > 正文

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

2024-07-21 02:21:06
字体:
来源:转载
供稿:网友
当我们编写程序时,会常常遇到程序信息内容更新的问题,对于小的文件更新,可以提供给客户自己到网络上下载,但对于大且多的文件,由于网络的原因,通过下载却又不实际,动辄是更新不完整,影响了程序的运行。当时我编写“商务娱乐频道系统”时,也遇到了这样的问题,对于大型的视频及图片文件,我考虑到了使用压缩包提供给客户,但是通过使用压缩程序却不能将我的文件按要求进行解压到其他相应的目录,那时我想到了何不自己制作压缩与解压缩程序呢。解压时将文件解压到程序所要的位置。

为了这个项目,我仔细的研究了vb的安装程序,原来vb是通过系统所自带的资源来进行压缩与解压缩,如makecab.exe、vb6stkit.dll等。

其实真真做起来还是挺简单的,就是调用几个api函数便可以搞定。近日,闲着有空,翻看自己的旧程序,故决定将该程序整理出来,与大家共享。



下面是具体的程序编写模块,首先你需要建立一个工程(名称由你自己确定了):

1. 添加两个模块,在这里我给它们分别命名为modapi、modmain;

2. 添加三个窗体,在这里我给它们分别命名为frmmain、frmlogin、frmaddinfo;

3. 以下是各个模块的源代码内容,请先保存该工程,并且关闭,然后转到该工程的文件夹下,按下面的提示进行源代码拷贝;



用记事本打开frmmain.frm文件,copy以下内容到其中:



version 5.00

object = "{831fdd16-0c5c-11d2-a9fc-0000f8754da1}#2.0#0"; "mscomctl.ocx"

object = "{f9043c88-f6f2-101a-a3c9-08002b2f49fb}#1.2#0"; "comdlg32.ocx"

begin vb.form frmmain

borderstyle = 1 'fixed single

caption = "信息文件更新"

clientheight = 5385

clientleft = 45

clienttop = 330

clientwidth = 8550

controlbox = 0 'false

icon = "frmmain.frx":0000

linktopic = "form1"

lockcontrols = -1 'true

maxbutton = 0 'false

minbutton = 0 'false

scaleheight = 5385

scalewidth = 8550

startupposition = 2 '屏幕中心

begin vb.commandbutton cmdok

caption = "导出更新列表"

height = 375

index = 3

left = 5385

tabindex = 6

top = 4980

width = 1545

end

begin vb.commandbutton cmdok

caption = "关 闭"

height = 375

index = 2

left = 7620

tabindex = 5

top = 4980

width = 885

end

begin vb.commandbutton cmdok

caption = "打 包"

height = 375

index = 1

left = 3810

tabindex = 1

top = 4980

width = 885

end

begin vb.commandbutton cmdok

caption = "展 开"

height = 375

index = 0

left = 0

tabindex = 0

top = 4980

width = 885

end

begin mscomctllib.listview lstinfo

height = 4275

left = 0

tabindex = 2

top = 330

width = 8505

_extentx = 15002

_extenty = 7541

view = 3

arrange = 1

labeledit = 1

multiselect = -1 'true

labelwrap = -1 'true

hideselection = 0 'false

fullrowselect = -1 'true

gridlines = -1 'true

_version = 393217

forecolor = -2147483640

backcolor = -2147483643

borderstyle = 1

appearance = 1

numitems = 3

beginproperty columnheader(1) {bdd1f052-858b-11d1-b16a-00c0f0283628}

text = "序号"

object.width = 1235

endproperty

beginproperty columnheader(2) {bdd1f052-858b-11d1-b16a-00c0f0283628}

subitemindex = 1

text = "压缩包文件"

object.width = 6068

endproperty

beginproperty columnheader(3) {bdd1f052-858b-11d1-b16a-00c0f0283628}

subitemindex = 2

text = "目标信息"

object.width = 7832

endproperty

end

begin mscomdlg.commondialog comdinfo

left = 0

top = 360

_extentx = 847

_extenty = 847

_version = 393216

cancelerror = -1 'true

maxfilesize = 30000

end

begin mscomctllib.progressbar pgbar

height = 345

left = 30

tabindex = 4

top = 4620

width = 8505

_extentx = 15002

_extenty = 609

_version = 393216

appearance = 0

scrolling = 1

end

begin vb.label lblabout

backstyle = 0 'transparent

caption = "关于本程序..."

height = 255

left = 7260

tabindex = 8

top = 60

width = 1215

end

begin vb.label lblinfo

autosize = -1 'true

caption = "请等待,正在创建包信息文件..."

height = 180

index = 1

left = 30

tabindex = 7

top = 4740

width = 4980

end

begin vb.label lblinfo

autosize = -1 'true

caption = "展开打包信息更新列表:"

height = 180

index = 0

left = 30

tabindex = 3

top = 30

width = 1980

end

end

attribute vb_name = "frmmain"

attribute vb_globalnamespace = false

attribute vb_creatable = false

attribute vb_predeclaredid = true

attribute vb_exposed = false





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

' 信息打包与展开 (主窗体模块,即展开窗体)

'

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

'

' 作 者 :谢家峰

' 整理日期 :2004-08-08

' email :[email protected]

'

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

'



option explicit



private declare function extractfilefromcab lib "vb6stkit.dll" _

(byval cab as string, byval file as string, byval dest as string, _

byval icab as long, byval ssrc as string) as long

'说明:

'cab 为系统安装目录下的压缩包

'file 为压缩包内的某文件名称(需在该文件名前加“@”字符)

'dest 为压缩包内的某文件解压后的完全路径名

'icab 为压缩包的数目

'ssrc 临时文件夹,一个有效的文件夹路径



dim s_filenames() as string '源文件名(不含路径)

dim d_filenames() as string '目标文件名(含路径)

dim cab_filename as string '包文件名





private sub cmdok_click(index as integer)

dim filenum as long

dim i as long

dim j as long

dim filename as string



select case index

case 0

filename = app.path & "/更新.ini"

'查找包文件信息

s_filenames = getfiles(app.path & "/*.cab_")

if ubound(s_filenames) = 0 then

msgbox "当前目录下没找到“商务频道系统文件更新”包文件!", , app.exename

exit sub

end if



if ubound(s_filenames) > 1 then

with comdinfo

.filter = "商务频道系统文件更新包|*.cab_|"

.dialogtitle = "请指定“商务频道系统文件更新”包的位置"

.initdir = app.path

.flags = cdlofnfilemustexist or cdlofnhidereadonly

.filename = app.path & "/" & s_filenames(1)

on error goto errfind

.showopen



cab_filename = trim(right(.filename, len(.filename) - len(app.path & "/")))

on error goto 0

end with

else

cab_filename = s_filenames(1)

end if



screen.mousepointer = 11

pgbar.visible = false

lblinfo(1).visible = true

doevents



'将当前包复制到系统安装文件夹下

if fileexists(windowspath & cab_filename) then kill windowspath & cab_filename

filecopy app.path & "/" & cab_filename, windowspath & cab_filename

'转换包路径信息(为系统安装目录下的文件)

cab_filename = windowspath & cab_filename

setattr cab_filename, vbnormal



'获得“更新.ini”文件

j = extractfilefromcab(cab_filename, "@更新.ini", filename, 1, app.path & "/")

setattr filename, vbnormal



lblinfo(1).visible = false

pgbar.visible = true

screen.mousepointer = 1

doevents



if j = 0 then

msgbox "该压缩包信息不完整,或不是“商务频道系统文件更新”包!" & vbcrlf & vbcrlf & "解压没完成,请索取最新的更新包!", , app.exename

'删除系统安装目录下的复制包

kill cab_filename

exit sub

else

setattr filename, vbnormal

end if



screen.mousepointer = 11

'解压信息

filenum = clng(clng(readinifile(filename, "文件数目", "filenum")))

redim s_filenames(filenum)

redim d_filenames(filenum)

'其中s_filenames的最后一个数据为播放信息文件

for i = 1 to filenum

s_filenames(i - 1) = readinifile(filename, "源文件信息", "file" & i)

s_filenames(i - 1) = getfilename(s_filenames(i - 1))

d_filenames(i - 1) = readinifile(filename, "目标文件信息", "file" & i)

doevents

next



lstinfo.listitems.clear

pgbar.min = 1

pgbar.max = filenum + 1



for i = 1 to filenum

doevents

'建立文件夹

createfloder d_filenames(i - 1)

'解压文件

if fileexists(d_filenames(i - 1)) then setattr d_filenames(i - 1), vbnormal

j = extractfilefromcab(cab_filename, "@" & s_filenames(i - 1), d_filenames(i - 1), 1, app.path & "/")

if j = 0 then

msgbox "该压缩包信息不完整,或不是“商务频道系统文件更新”包!" & vbcrlf & vbcrlf & "解压没完成,请索取最新的更新包!", , app.exename

lstinfo.listitems.clear

pgbar.min = 0

pgbar.value = 0

screen.mousepointer = 1

exit sub

end if

pgbar.value = i

doevents

lstvinfo_add lstinfo, 3, false, lstinfo.listitems.count + 1, s_filenames(i - 1), d_filenames(i - 1)

next



'删除系统安装目录下的复制包

kill cab_filename

kill filename

pgbar.value = filenum + 1



msgbox "解压缩完成,系统更新完成,谢谢使用!", , app.exename

pgbar.min = 0

pgbar.value = 0



case 1 ' 执行信息打包

lstinfo.listitems.clear

frmlogin.show 1, me

case 2

unload me

case 3

if lstinfo.listitems.count = 0 then msgbox "无信息可供导出!", , app.exename: exit sub

with frmmain.comdinfo

.filter = "更新列表信息|*.txt"

.dialogtitle = "导出包列表信息文件"

.initdir = curdir()

.flags = cdlofnhidereadonly

.filename = "更新列表.txt"

on error goto errlab

.showsave



filename = .filename

if fileexists(filename) then

setattr filename, vbnormal

kill filename

end if

'导出信息

with lstinfo

writeprivateprofilestring "文件数目", "filenum", cstr(.listitems.count), filename

for i = 1 to .listitems.count

writeprivateprofilestring "压缩包文件信息", "file" & i, .listitems(i).subitems(1), filename

writeprivateprofilestring "目标文件信息", "file" & i, .listitems(i).subitems(2), filename

next

end with

end with

msgbox "信息列表被导出在“" & filename & "”文件中!", , app.exename



case else

end select



screen.mousepointer = 1

exit sub



errlab:

if err.number = 32755 then

'解压文件

d_filenames(filenum) = app.path & "/" & s_filenames(filenum)

if fileexists(d_filenames(i - 1)) then setattr d_filenames(filenum), vbnormal

extractfilefromcab cab_filename, "@" & s_filenames(filenum), d_filenames(filenum), 1, app.path & "/"

setattr d_filenames(filenum), vbnormal



pgbar.value = filenum + 1

lstvinfo_add lstinfo, 3, false, lstinfo.listitems.count + 1, s_filenames(filenum), app.path & "/" & s_filenames(filenum)

'删除系统安装目录下的复制包

if fileexists(cab_filename) then kill cab_filename

kill filename



msgbox "您取消了指定用户信息的位置,该用户信息缺省被放在“" & d_filenames(filenum) & "”!" _

& vbcrlf & vbcrlf & "解压缩完成,系统更新完成,谢谢使用!", , app.exename

pgbar.min = 0

pgbar.value = 0

else

err.raise err.number, , err.description

end if



screen.mousepointer = 1

exit sub



errfind:

if err.number = 32755 then

else

err.raise err.number, , err.description

end if

screen.mousepointer = 1

exit sub

end sub



private sub lblabout_click()

lblabout.borderstyle = 1

frmabout.show 1, me

end sub



private sub lstinfo_itemclick(byval item as mscomctllib.listitem)

if not (item is nothing) then

lstinfo.tooltiptext = "[目标信息] " & item.listsubitems(2)

end if

end sub

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表