Monday, November 5, 2012

CompressFile in VB.NET using ICSharpCode.SharpZipLib.dll


Public Shared Sub CompressFile(ByVal sourceFilePath As String, ByVal destinationFilePath As String)
        Dim outFileInfo As IO.FileInfo = New IO.FileInfo(destinationFilePath)
        Dim inFileInfo As IO.FileInfo = New IO.FileInfo(sourceFilePath)

        'Throw error if source file doesn't exist
        If Not inFileInfo.Exists Then
            Throw New Exception("Source File not found!!")
        End If
        ' Create the output directory if it does not exist
        If Not IO.Directory.Exists(outFileInfo.Directory.FullName) Then IO.Directory.CreateDirectory(outFileInfo.Directory.FullName)

        Using fsOut As IO.FileStream = IO.File.Create(destinationFilePath)
            Using zipStream As ICSharpCode.SharpZipLib.Zip.ZipOutputStream = New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(fsOut)
                zipStream.SetLevel(4)
                Dim newEntry As ICSharpCode.SharpZipLib.Zip.ZipEntry = New ICSharpCode.SharpZipLib.Zip.ZipEntry(inFileInfo.Name)
                newEntry.DateTime = DateTime.UtcNow
                zipStream.PutNextEntry(newEntry)
                Dim buffer(4096) As Byte
                Using streamReader As IO.FileStream = IO.File.OpenRead(sourceFilePath)
                    ICSharpCode.SharpZipLib.Core.StreamUtils.Copy(streamReader, zipStream, buffer)
                End Using
                zipStream.CloseEntry()
                zipStream.IsStreamOwner = True
                zipStream.Close()
            End Using
        End Using
    End Sub

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...