Link to home
Start Free TrialLog in
Avatar of zameer21
zameer21

asked on

How to zip files in a folder using Vb.net 2005

I need to zip files in a folder using Vb.net,how can i do it.
ASKER CERTIFIED SOLUTION
Avatar of Jorge Paulino
Jorge Paulino
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
As of .NET 2.0 (i.e. Visual Studio 2005) you no longer need to use a 3rd party library.   The namespace System.IO.Compression now has the necessary tools to get done the basics of managing zip files via gzip compression.   You should be able to use System.IO.Compression.GZipStream and then add your files using a standard file stream object.

Visit this website for a code example of how to do this:
http://blogs.msdn.com/dotnetinterop/archive/2006/04/05/.NET-System.IO.Compression-and-zip-files.aspx
Hi have tried also to use that namespace (System.IO.Compression) but I found complicated and not easy to handle. I still prefer SharpZipLib for my apps.
Indeed GZipStream or DeflateStream alone won't allow you to create valid Zip files.
.net 2.0 doesn't have a direct facility to do this. .net 3.0 instead gives you the ZipPackage class in the new System.IO.Packaging namespace.
(http://msdn2.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx)
But you can use a library like DotNetZip, as previously suggested, or, if you don't want 3rd party libraries, you can use Windows shell facilities as explained here:
http://www.codeproject.com/KB/cs/compresswithwinshellapics.aspx

Avatar of zameer21
zameer21

ASKER

I really like SharpZipLib,but still i would like to write my own code if i could
>> I really like SharpZipLib,but still i would like to write my own code if i could

Don't understand because you have to write you code. Here's an example of SharpZipLib, that uses a progress bar and writes to a log file:

Private Sub ZipIt()
        ZipProgressBar.Maximum = lvFicheiros.Items.Count
        ZipProgressBar.Value = 0
        ZipProgressBar.Step = 1
        ZipProgressBar.Visible = True
        lblBackup.Visible = True
        Application.DoEvents()
 
        Try
 
            Dim strmZipOutputStream As ZipOutputStream
            Dim outputname As String = DateTime.Now.ToString("yyyyMMdd-HHmm") & ".zip"
 
            Directory.SetCurrentDirectory(My.Settings.OutputDir)
            strmZipOutputStream = New ZipOutputStream(File.Create(outputname))
 
            ' Compression Level: 0-9
            strmZipOutputStream.SetLevel(9)
 
            For Each item As ListViewItem In lvFicheiros.Items
 
                Dim strmFile As FileStream
                If TryOpenFile(item.SubItems(1).Text) Then
                    strmFile = File.OpenRead(item.SubItems(1).Text)
                Else
                    Dim fl As New FileInfo(item.SubItems(1).Text)
 
                    File.Copy(item.SubItems(1).Text, Environment.CurrentDirectory & "\" & fl.Name, True)
                    strmFile = File.OpenRead(Environment.CurrentDirectory & "\" & fl.Name)
                    FilesToDelete.Add(Environment.CurrentDirectory & "\" & fl.Name)
                    fl = Nothing
                End If
 
                Dim abyBuffer(CInt(strmFile.Length - 1)) As Byte
 
                strmFile.Read(abyBuffer, 0, abyBuffer.Length)
                Dim objZipEntry As ZipEntry = New ZipEntry(item.SubItems(0).Text)
 
                objZipEntry.DateTime = DateTime.Now
                objZipEntry.Size = strmFile.Length
                strmFile.Close()
 
                strmZipOutputStream.PutNextEntry(objZipEntry)
                strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)
 
                ZipProgressBar.PerformStep()
                ZipProgressBar.Update()
                Application.DoEvents()
            Next
 
            strmZipOutputStream.Finish()
            strmZipOutputStream.Close()
 
            Dim outputstring As String = Now.ToString("yyyy/MM/dd HH:mm:ss") & " - Backup name: " & outputname
            My.Computer.FileSystem.WriteAllText(Environment.CurrentDirectory & "\autobackup.log", outputstring & vbNewLine, True)
            lvLog.Items.Add(outputstring)
            lvLog.EnsureVisible(lvLog.Items.Count - 1)
 
        Catch ex As Exception
            MsgBox("Erro message:" & vbCrLf & vbCrLf & ex.Message, MsgBoxStyle.Critical)
 
        Finally
            ZipProgressBar.Visible = False
            lblBackup.Visible = False
        End Try
End Sub

Open in new window

I think what he means is that he would like to have all of the code in his project and not in a third-party library.   I will say that since SharpZipLib is open source you COULD take the code and incorporate it into your software, just remember that you have to live by the open source license rules.

The page I linked to earlier has a source example of how to use System.IO.Compression to create a Zip class that is quite easy to use once you've got the base class working.  http://msdn2.microsoft.com/en-us/library/system.io.packaging.zippackage.aspx

Your choice.
Hi JPaulino,
Here is something i found.

  Private Sub Zip()
        ' Zip up the files - From SharpZipLib Demo Code
        Dim s As ZipOutputStream = New ZipOutputStream(System.IO.File.Create("C:\testfile2.zip"))
        s.SetLevel(9) ' 0-9, 9 being the highest level of compression
        Dim buffer() As Byte
        ReDim buffer(4096)
        Dim filenames As String() = Directory.GetFiles("C:\Test\in")
        Dim f As String
        For Each f In filenames
            Dim entry As ZipEntry = New ZipEntry(Path.GetFileName(f))
            entry.DateTime = DateTime.Now
            s.PutNextEntry(entry)
            Dim fs As FileStream = System.IO.File.OpenRead(f)
            Dim sourceBytes As Integer = 1
            Do Until (sourceBytes <= 0)
                sourceBytes = fs.Read(buffer, 0, buffer.Length)
                s.Write(buffer, 0, sourceBytes)
            Loop
            fs.Close()
        Next
        ' clean up
        s.Finish()
        s.Close()
    End Sub

this works and is pretty simple.I will give you all points for the SharpZipLibraray, though i was looking for code.Thanks......
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Missing somethig:

Private FilesToDelete As New ArrayList

and call it this way:

Call ZipIt()
Call DeleteFiles()