Imports System.IO
Imports System.IO.Compression
Module Module1
Sub Main()
Dim dirpath As String = "c:\users\public\reports"
Dim di As DirectoryInfo = New DirectoryInfo(dirpath)
For Each fi As FileInfo In di.GetFiles()
Compress(fi)
Next
End Sub
Private Sub Compress(ByVal fi As FileInfo)
Using inFile As FileStream = fi.OpenRead()
If (File.GetAttributes(fi.FullName) And FileAttributes.Hidden) _
<> FileAttributes.Hidden And fi.Extension <> ".gz" Then
Using outFile As FileStream = File.Create(fi.FullName + ".gz")
Using Compress As GZipStream = New GZipStream(outFile, CompressionMode.Compress)
Dim buffer As Byte() = New Byte(4096) {}
Dim numRead As Integer
numRead = inFile.Read(buffer, 0, buffer.Length)
Do While numRead <> 0
Compress.Write(buffer, 0, numRead)
numRead = inFile.Read(buffer, 0, buffer.Length)
Loop
End Using
End Using
End If
End Using
End Sub
End Module