VB中利用WinRAR进行文件压缩

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 tripofdream 的 blog

作者:王莉梅
一些数据库文件(如access文件)在远程传输过程中可能由于文件比较大而影响传递效果。如果进行压缩以后再传递,会减少传递时间,避免意外的发生,同时也保证了传递效果。我们在压缩文件时,最常用的压缩工具为winrar和winzip,笔者在vb编程过程中利用winrar工具来压缩数据库文件,并完成远程传输,十分方便,在此向大家介绍一下。用winzip的方法类似。

  一、shell函数
  shell函数是vb中的内部函数,它负责执行一个可执行文件,返回一个variant(double),如果成功的话,代表这个程序的进程id,若不成功,则会返回0。

  shell的语法:shell(pathname[,windowstyle])。

  pathname 为必需参数。类型为string,它指出了要执行的程序名,以及任何需要的参数或命令行变量,也可以包括路径名。

  windowstyle为可选参数。integer类型,指定在程序运行时窗口的样式。windowstyle有以下这些值。

  常量 值 描述
  vbhide 0 窗口被隐藏,且焦点会移到隐式窗口。
  vbnormalfocus 1 窗口具有焦点,且会还原到它原来的大小和位置。
  vbminimizedfocus 2 窗口会以一个具有焦点的图标来显示(缺省值)。
  vbmaximizedfocus 3 窗口是一个具有焦点的最大化窗口。
  vbnormalnofocus 4 窗口会被还原到最近使用的大小和位置,而当前活动的窗口仍然保持活动。
  vbminimizednofocus 6 窗口会以一个图标来显示,而当前活动的窗口仍然保持活动。

  二、关于winrar的用法
  主要介绍以下如何在winrar中用命令行来压缩和解压缩文件。

  压缩:winrar a [-switches] [files] [@file lists]

  例如你想把try.mdb压缩到c盘下,可以winrar a c:try.rar c:try.mdb

  解压缩:如果带目录解压缩

     winrar x [-switches] [files] [@file lists] [destionation folder]

     如果在当前目录解压缩,即解压缩时不写目录名

     winrar e [-switches] [files] [@file lists] [destionation folder]

  例如你想把try.rar解压缩到c盘下,可以winrar x c:try.rar c:try.mdb

  三、一个例子
  在vb中新建一个工程,在form1中添加两个按钮command1、command2和command3,把他们的caption属性分别设为"压缩文件"、"解压缩文件"和"传递文件"。按command1时把文件try.mdb压缩成try.rar。
private sub command1_click() 

  dim rarexe as string ‘winrar执行文件的位置 

  dim source as string ‘ 压缩前的原始文件 

  dim target as string ‘ 压缩后的目标文件 

  dim filestring as string ‘shell指令中的字符串 

  dim result as long 

   rarexe="c:program fileswinrarwinrar" 

  source="c:try.mdb" 

  target="c:try.rar" 

  filestring = rarexe & " a " & target & " " & source 

  result = shell(filestring, vbhide) 

  end sub 

  解压的过程类似,按command2可以把try.rar解压生成 try.mdb。在执行了上面的压缩过程后,可以删除文件try.mdb,来解压缩重新生成try.mdb。 

  private sub command2_click() 

  dim rarexe as string ‘winrar执行文件的位置 

  dim source as string ‘ 解压缩前的原始文件 

  dim target as string ‘ 解压缩后的目标文件 

  dim filestring as string ‘shell指令中的字符串 

  dim result as long 

   rarexe="c:program fileswinrarwinrar" 

  source="c:try.rar" 

  target="c:try.mdb" 

  filestring = rarexe & " x " & source & " " & target 

  result = shell(filestring, vbhide) 

  end sub 

  文件从一台计算机传输到另一台计算机前,应知道另一台计算机的名字,然后用filecopy语句就可以了。假设要把压缩后try.rar传递到计算机名为"other"的共享目录"want"下。 

  private sub command3_click() 

  dim sourcefile, destinationfile 

   sourcefile ="c:try.rar " ‘ 指定源文件名。 

   destinationfile = "otherwanttry.rar" ‘ 指定目的文件名。 

   filecopy sourcefile, destinationfile ‘ 将源文件的内容复制到目的文件中。 

  end sub 

  怎么样,十分简单吧?

 

本文关键:WinRAR
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top