|
Public Class frmCompress Public Sub subZipFile(ByVal strFile As String, Optional ByVal strZipFile As String = "") Try ' Existiert die Datei überhaupt If File.Exists(strFile) Then ' Falls keine Zieldatei angegeben, Quelldatei mit Endung ".gzip" annehmen If strZipFile.Length = 0 Then With New FileInfo(strFile) strZipFile = .DirectoryName If Not strZipFile.EndsWith("\") Then strZipFile += "\" strZipFile += .Name.Substring(0, .Name.Length - .Extension.Length) & ".gzip" End With End If ' Inhalt der Datei einlesen Dim oStream As FileStream oStream = New FileStream(strFile, IO.FileMode.Open) Dim bBuffer(oStream.Length - 1) As Byte With oStream .Read(bBuffer, 0, bBuffer.Length) .Close() End With ' Jetzt Daten komprimieren und in Zieldatei speichern oStream = New FileStream(strZipFile, IO.FileMode.Create) Dim oCompress As New Compression.GZipStream( _ oStream, IO.Compression.CompressionMode.Compress) With oCompress .Write(bBuffer, 0, bBuffer.Length) .Flush() .Close() End With oStream.Close() End If Catch ex As Exception 'ERROR End Try End Sub
Public Sub subUnzipFile(ByVal strZipFile As String, ByVal strDestFile As String) Try ' Existiert die Datei überhaupt If System.IO.File.Exists(strZipFile) Then ' GZIP-Datei öffnen Dim oStream As New FileStream(strZipFile, IO.FileMode.Open) Dim oCompress As New Compression.GZipStream( _ oStream, IO.Compression.CompressionMode.Decompress) ' Inhalt blockweise auslesen und dekomprimieren Dim bBuffer() As Byte Dim nOffset As Integer = 0 Dim nCount As Integer = 0 While True ReDim Preserve bBuffer(nCount + 100) Dim nBytes As Integer = oCompress.Read(bBuffer, nOffset, 100) If nBytes = 0 Then Exit While nOffset += nBytes nCount += nBytes End While ReDim Preserve bBuffer(nCount - 1) ' Byte-Array in Zieldatei speichern My.Computer.FileSystem.WriteAllBytes(strDestFile, bBuffer, False) oStream.Close() oCompress.Close() End If Catch ex As Exception 'ERROR End Try End Sub End Class |