2011年12月6日 星期二

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

參考引用
--
7-zip 下載
--
  1. Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. '參數一:要壓縮的檔名
  3. '參數二:要壓縮的檔案
  4. SevenZip("c:\test.zip", "c:\test\*.*", "")
  5. End Sub
  6.  
  7. Public Sub SevenZip(ByVal ZipFileName, ByVal ZipSourcePath, ByVal ZipPassWord)
  8. Dim ZipPs As New Diagnostics.Process
  9. Try
  10. Dim strExec As String = Server.MapPath("7z/7za.exe")
  11. Dim strAgr As String
  12. '需要設定密碼
  13. If ZipPassWord <> "" Then
  14. strAgr = "a " & ZipFileName & " " & ZipSourcePath & " -p" & ZipPassWord
  15. Else
  16. '不需要密碼
  17. strAgr = "a " & ZipFileName & " " & ZipSourcePath & ""
  18. End If
  19.  
  20. ZipPs.StartInfo.FileName = strExec
  21. ZipPs.StartInfo.Arguments = strAgr
  22. ZipPs.StartInfo.WindowStyle = Diagnostics.ProcessWindowStyle.Normal
  23. ZipPs.Start()
  24. ZipPs.WaitForExit()
  25. ZipPs.Close()
  26. If Dir(ZipFileName) <> "" Then
  27. Response.Write("Zip Complete.") 'check zip file complete
  28. End If
  29. Catch ex As Exception
  30. Response.Write(ex.Message)
  31. End Try 
End Sub 

沒有留言:

張貼留言