Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports System.IO
- Imports System.IO.Compression
- Public Class Zipper
- Public Event Progress(percent As Integer)
- Public Event StatusChanged(status As String)
- Public Event Complete()
- Public Event Canceled()
- Public Enum Overwrite
- Always
- Never
- End Enum
- Private _cancel As Boolean
- Public Property Cancel As Boolean
- Get
- Return _cancel
- End Get
- Set(value As Boolean)
- If _cancel = True Then Exit Property
- _cancel = value
- End Set
- End Property
- Private _compression As CompressionLevel
- Public Property CompressionLevel As CompressionLevel
- Get
- Return _compression
- End Get
- Set(value As CompressionLevel)
- _compression = value
- End Set
- End Property
- Private _target As String
- Public Property TargetURL As String
- Get
- Return _target
- End Get
- Set(value As String)
- _target = value
- End Set
- End Property
- Private _source As String
- Public Property SourceURL As String
- Get
- Return _source
- End Get
- Set(value As String)
- _source = value
- End Set
- End Property
- Private _isDir As Boolean
- Public ReadOnly Property SourceIsDirectory
- Get
- Return _isDir
- End Get
- End Property
- Private _includeRootDir As Boolean
- Public Property IncludeRootDir As Boolean
- Get
- Return _includeRootDir
- End Get
- Set(value As Boolean)
- _includeRootDir = value
- End Set
- End Property
- Private _overwrite As Overwrite
- Private _sessionLength As Int64
- Private _sessionFiles As String()
- Private _rootDir As String
- Private Function GetSessionLength() As Int64
- Dim sLen As Int64 = 0
- For Each sessionFile As String In _sessionFiles
- sLen += New FileInfo(sessionFile).Length
- If Cancel = True Then Exit For
- Next
- Return sLen
- End Function
- Private Function GetEntriesLength() As Int64
- Dim sLen As Int64 = _sessionFiles.Sum(Function(file) New FileInfo(file).Length)
- Return sLen
- End Function
- Private Function IsDir(source As String) As Int16
- If File.Exists(source) Then
- Return 0
- ElseIf Directory.Exists(source) Then
- Return -1
- Else
- Return 1
- End If
- End Function
- Private Function InlineAssignHelper(Of T)(ByRef target As T, value As T) As T
- target = value
- Return value
- End Function
- Public Sub Compress(source As String, target As String, compressionLevel As CompressionLevel, overwrite As Overwrite)
- RaiseEvent StatusChanged("Gathering required information")
- _overwrite = False
- _includeRootDir = True
- _target = target
- _compression = compressionLevel
- _cancel = False
- If IsDir(source) <> 1 Then
- _isDir = IsDir(source)
- _source = source
- Else
- Throw New Exception("Source file or directory doesn't exist or cannot be accessed.")
- End If
- If SourceIsDirectory Then
- _sessionFiles = Directory.GetFiles(SourceURL, "*", SearchOption.AllDirectories)
- Else
- _sessionFiles = New String() {SourceURL}
- End If
- RaiseEvent StatusChanged("Examining files")
- _sessionLength = GetSessionLength()
- If SourceIsDirectory And IncludeRootDir = False Then
- _rootDir = SourceURL & "\"
- Else
- _rootDir = String.Join("\", SourceURL.Split("\").ToArray, _
- 0, SourceURL.Split("\").ToArray.Length - 1) & "\"
- End If
- RaiseEvent StatusChanged("Compressing")
- Try
- Zip()
- Catch ex As Exception
- MsgBox(ex.Message)
- Exit Sub
- End Try
- If Cancel = True Then
- RaiseEvent Canceled()
- RaiseEvent StatusChanged("Cancelled")
- RaiseEvent Progress(100)
- Else
- RaiseEvent Complete()
- RaiseEvent StatusChanged("Complete")
- End If
- End Sub
- Public Sub Decompress(source As String, target As String, overwrite As Overwrite)
- RaiseEvent StatusChanged("Gathering required information")
- _overwrite = overwrite
- _source = source
- _cancel = False
- _target = target
- RaiseEvent StatusChanged("Examining files")
- _sessionLength = GetEntriesLength()
- RaiseEvent StatusChanged("Decompressing")
- Try
- Unzip()
- Catch ex As Exception
- MsgBox(ex.Message)
- Exit Sub
- End Try
- If Cancel = True Then
- RaiseEvent Canceled()
- RaiseEvent StatusChanged("Cancelled")
- RaiseEvent Progress(100)
- Else
- RaiseEvent Complete()
- RaiseEvent StatusChanged("Complete")
- End If
- End Sub
- Private Sub Zip()
- If Cancel = True Then Exit Sub
- Dim blockSizeToRead As Int32 = 1048576 '1Mib Buffer
- Dim buffer As Byte() = New Byte(blockSizeToRead - 1) {}
- Dim bytesRead As Int64, totalBytesRead As Int64
- Dim liveProg As Int16 = 0
- Dim prevProg As Int16 = 0
- If File.Exists(_target) And _overwrite = Overwrite.Never Then
- Throw New Exception("Target file already exists")
- Else
- File.Delete(_target)
- End If
- Using fs As FileStream = New FileStream(_target, FileMode.CreateNew, FileAccess.Write)
- Using archive As ZipArchive = New ZipArchive(fs, ZipArchiveMode.Create)
- Dim entry As ZipArchiveEntry = Nothing
- For Each sessionFile As String In _sessionFiles
- Try
- Using reader As FileStream = File.Open(sessionFile, FileMode.Open, FileAccess.Read)
- entry = archive.CreateEntry(sessionFile.Replace(_rootDir, ""), _compression)
- Using writer As Stream = entry.Open()
- While (InlineAssignHelper(bytesRead, _
- reader.Read(buffer, 0, buffer.Length - 1))) > 0
- writer.Write(buffer, 0, bytesRead)
- totalBytesRead += bytesRead
- liveProg = CInt((100 / _sessionLength) * totalBytesRead)
- If liveProg <> prevProg Then
- prevProg = liveProg
- RaiseEvent Progress(liveProg)
- End If
- If Cancel = True Then Exit While
- End While
- End Using
- End Using
- Catch ex As Exception
- totalBytesRead += New FileInfo(sessionFile).Length
- Console.WriteLine(String.Format("Unable to add file to archive: {0} Error:{1}", sessionFile, ex.Message))
- End Try
- If Cancel = True Then Exit For
- Next
- End Using
- End Using
- If Cancel = True Then
- File.Delete(_target)
- End If
- End Sub
- Private Sub Unzip()
- If Cancel = True Then Exit Sub
- Dim blockSizeToRead As Int32 = 1048576 '1Mib Buffer
- Dim buffer As Byte() = New Byte(blockSizeToRead - 1) {}
- Dim bytesRead As Int64, totalBytesRead As Int64
- Dim liveProg As Int16 = 0
- Dim prevProg As Int16 = 0
- Using fs As FileStream = New FileStream(_source, FileMode.Open)
- Using archive As ZipArchive = New ZipArchive(fs, ZipArchiveMode.Read)
- For Each entry As ZipArchiveEntry In archive.Entries
- Dim output As String = String.Format("{0}{1}\{2}", _target, _
- Path.GetFileNameWithoutExtension(_source), _
- entry.FullName.Replace("/", "\"))
- Dim directoryPath As String = IIf(output.EndsWith("\"), output, Directory.GetParent(output).ToString)
- If output.EndsWith("\") Then
- Continue For
- Else
- If File.Exists(output) Then
- Select Case _overwrite
- Case Overwrite.Always
- File.Delete(output)
- Case Overwrite.Never
- Throw New Exception("Target file already exists")
- End Select
- End If
- End If
- If Directory.Exists(directoryPath) = False Then
- Directory.CreateDirectory(directoryPath)
- End If
- Using reader As Stream = entry.Open()
- Using writer As FileStream = New FileStream(output, FileMode.Create)
- While (InlineAssignHelper(bytesRead, reader.Read(buffer, 0, buffer.Length - 1))) > 0
- writer.Write(buffer, 0, bytesRead)
- totalBytesRead += bytesRead
- liveProg = CInt((100 / _sessionLength) * totalBytesRead)
- If liveProg <> prevProg Then
- prevProg = liveProg
- RaiseEvent Progress(liveProg)
- End If
- If Cancel = True Then Exit While
- End While
- End Using
- End Using
- If Cancel = True Then Exit For
- Next
- End Using
- End Using
- End Sub
- End Class
Add Comment
Please, Sign In to add comment