Published in: VB.NET
Option Strict On Imports System.ComponentModel Public Class SevenZip Private Shared ZipPath As String = AppController.RootPath & "7z.exe" Public Event OnZipComplete(ByVal ZipFileName As String) Public Shared Function Extract(ByVal filename As String, ByVal zipname As String) As Process Dim p As New Process() p.StartInfo.FileName = ZipPath p.StartInfo.Arguments = "x -y -o""" & filename & """ """ & zipname & """" p.StartInfo.CreateNoWindow = True p.Start() Return p End Function Public Shared Function Compress(ByVal filename As String, ByVal zipname As String) As Process Dim p As New Process() p.StartInfo.FileName = ZipPath p.StartInfo.Arguments = "a -r -tzip """ & zipname & """ """ & filename & """" p.StartInfo.CreateNoWindow = True p.Start() Return p End Function Public Shared Function Compress(ByVal filename As String, ByVal zipnames() As String) As Process Dim p As New Process() p.StartInfo.FileName = ZipPath Dim args As String = "a -r -tzip " Dim s As String For Each s In zipnames args &= """" & s & """ " Next p.StartInfo.Arguments = args & filename & """" p.StartInfo.CreateNoWindow = True p.Start() Return p End Function Private Structure FileCopy Public Source As String Public Destination As String Public Sub New(ByVal source As String, ByVal dest As String) Me.Source = source Me.Destination = dest End Sub End Structure Public Sub CompressInNewThread(ByVal filename As String, ByVal zipname As String) Dim fc As New FileCopy(filename, zipname) 'run zip compress in another thread bw = New System.ComponentModel.BackgroundWorker() bw.RunWorkerAsync(fc) End Sub Private WithEvents bw As BackgroundWorker Private Sub StartZip(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles bw.DoWork Dim fc As FileCopy = DirectCast(e.Argument, FileCopy) Dim p As New Process() p.StartInfo.FileName = ZipPath p.StartInfo.Arguments = "a -r -tzip """ & fc.Destination & """ """ & fc.Source & """" p.Start() p.WaitForExit() 'return the zip filepath e.Result = fc.Destination End Sub Private Sub EndZip(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs) Handles bw.RunWorkerCompleted RaiseEvent OnZipComplete(Convert.ToString(e.Result)) End Sub End Class
Comments
Subscribe to comments
You need to login to post a comment.

realy neat piece of vb code ... though ya would help a starting coder a lot with a little comment and howto's ...