Compress Files

There are a number of ways to compress things in .NET, but I found out that only the code below works for files. I wrote the code below for my console apps .NET back utility.

Imports System.IO.Compression
''' <summary>
''' Compresses the file.
''' </summary>
''' <param name="sourceFileName">Name of the source file.</param>
''' <param name="destinationFileName">Name of the destination file 
                                      including extension.</param>
Public Sub CompressFile(sourceFileName As String,
                           destinationFileName As String)
    If File.Exists(sourceFileName) Then
        Using archive = ZipFile.Open(destinationFileName, 
                                     ZipArchiveMode.Create)
            Dim file = archive.CreateEntryFromFile(sourceFileName, 
                          Path.GetFileName(destinationFileName), 
                          CompressionLevel.Optimal)
        End Using
    End If
End Sub

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s