VB.Net/C# - How to add a single file to a ZIP with SharpZipLib


Here is a shared/static function (provided in Visual Basic and C#) that will add a single file into a ZIP file using SharpZipLib. Portions of this code originated from StackExchange, I melded what I saw into a single function.

VB.Net

''' <summary>
''' Adds a single file to a ZIP library using SharpZipLib
''' </summary>
''' <param name="zipFile">The path to the zip file you want to add a file to.</param>
''' <param name="fileToAdd">The path to the file you want to add to the zip.</param>
''' <param name="compressionLevel">The compression level to use from 0 to 10</param>
Public Shared Sub AddFileToZip(zipFile As String, fileToAdd As String, compressionLevel As Integer)
    Using zipStream As New ICSharpCode.SharpZipLib.Zip.ZipOutputStream(File.Create(zipFile))
        zipStream.SetLevel(compressionLevel)

        Dim buffer As Byte() = New Byte(4095) {}
        Dim entry As New ICSharpCode.SharpZipLib.Zip.ZipEntry(System.IO.Path.GetFileName(fileToAdd))

        entry.DateTime = DateTime.Now
        zipStream.PutNextEntry(entry)

        Using fs As FileStream = File.OpenRead(fileToAdd)
            Dim sourceBytes As Integer
            Do
                sourceBytes = fs.Read(buffer, 0, buffer.Length)
                zipStream.Write(buffer, 0, sourceBytes)
            Loop While sourceBytes > 0
        End Using

        zipStream.Finish() : zipStream.Close() : zipStream.Dispose()
    End Using

End Sub

C#

    /// <summary>
    /// Adds a single file to a ZIP library using SharpZipLib
    /// </summary>
    /// <param name="zipFile">The path to the zip file you want to add a file to.</param>
    /// <param name="fileToAdd">The path to the file you want to add to the zip.</param>
    /// <param name="compressionLevel">The compression level to use from 0 to 10</param>
    /// <remarks></remarks>
    public static void AddFileToZip(string zipFile, string fileToAdd, int compressionLevel)
    {
        using (ICSharpCode.SharpZipLib.Zip.ZipOutputStream zipStream = new ICSharpCode.SharpZipLib.Zip.ZipOutputStream(File.Create(zipFile)))
        {	
            zipStream.SetLevel(compressionLevel);
    	    byte[] buffer = new byte[4096];
            ICSharpCode.SharpZipLib.Zip.ZipEntry entry = new ICSharpCode.SharpZipLib.Zip.ZipEntry(System.IO.Path.GetFileName(fileToAdd));
    	    entry.DateTime = DateTime.Now;
            zipStream.PutNextEntry(entry);
    	    using (FileStream fs = File.OpenRead(fileToAdd))
            {		
                int sourceBytes = 0;		
                do
                {			
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                	zipStream.Write(buffer, 0, sourceBytes);
                            		
                } 
                while (sourceBytes > 0);	
            }

            zipStream.Finish();	
            zipStream.Close();	
            zipStream.Dispose();
        }    
    }

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.