2011年12月6日 星期二

VB.NET 使用7-zip壓縮指定目錄所有檔案

參考引用
--
7-zip 下載
--
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        '參數一:要壓縮的檔名
        '參數二:要壓縮的檔案
        SevenZip("c:\test.zip", "c:\test\*.*", "")
End Sub

Public Sub SevenZip(ByVal ZipFileName, ByVal ZipSourcePath, ByVal ZipPassWord)
        Dim ZipPs As New Diagnostics.Process
        Try
            Dim strExec As String = Server.MapPath("7z/7za.exe")
            Dim strAgr As String
           '需要設定密碼
            If ZipPassWord <> "" Then
                strAgr = "a " & ZipFileName & " " & ZipSourcePath & " -p" & ZipPassWord
            Else
                '不需要密碼
                strAgr = "a " & ZipFileName & " " & ZipSourcePath & ""
            End If

            ZipPs.StartInfo.FileName = strExec
            ZipPs.StartInfo.Arguments = strAgr
            ZipPs.StartInfo.WindowStyle = Diagnostics.ProcessWindowStyle.Normal
            ZipPs.Start()
            ZipPs.WaitForExit()
            ZipPs.Close()
            If Dir(ZipFileName) <> "" Then
                Response.Write("Zip Complete.") 'check zip file complete
            End If
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try 
End Sub 

沒有留言:

張貼留言