rar程式設計 實現

2021-04-12 20:28:02 字數 2616 閱讀 5153

我們在壓縮檔案時,最常用的壓縮工具為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  

批量解壓 python程式設計 批量解壓RAR檔案

本文實現rar批量解壓的功能,通過python指令碼呼叫winrar.exe解壓檔案時似乎不會再有廣告框彈出。通過python呼叫winrar.exe程式實現rar檔案的批量解壓,如下 import argparseimport osclass rarextractor def init self,...

php實現rar檔案的讀取和解壓

php rar archiving 模組 php rar 是乙個讀取和解壓rar檔案的模組,但不提供rar壓縮 打包 的功能。3.在php.ini中加入一行php rar擴充套件引用宣告 extension php rar.dll 4.如果使用apache伺服器,就需要重啟apache。iis下以f...

Python實現rar壓縮包遍歷解壓

1.匯入rarfile模組,實現檔案解壓 2.匯入os模組,實現檔案刪除 3.使用for迴圈實現檔案遍歷。import rarfile 匯入rarfile模組,實現檔案解壓 import os 匯入os模組,實現已解壓檔案的刪除 通過for迴圈,實現0 for i in range 0 20 定義原...