none
Zip file using SharpZipLib.dll

    Question

  • I realy need help.
    How do i zip a folder using SharpZipLib.dll?
    I would like a private sub so i can just copy/paste it inside my program and use it with one code line.
    Thanks in advance!
    Saturday, December 12, 2009 5:32 AM

Answers

  • If you can zip a folder using the native Windows compression found on the Explorer context menu: "Sent To --> Compressed (zipped) Folder", this class provides the same functionality.

    Imports System.IO

    Imports Shell32 'Reference Microsoft Shell Controls And Automation on the COM tab.

    Public Class ShellZip

      Sub Compress(ByVal CompressedFileName As String, ByVal FileOrFolderToCompress As String)

        Dim B(21) As Byte

        B(0) = 80 : B(1) = 75 : B(2) = 5 : B(3) = 6

        File.WriteAllBytes(CompressedFileName, B) 'Make an empty PKZip file.

        Dim SH As New Shell

        Dim SF As Folder = SH.NameSpace(CompressedFileName)

        Dim DF As Folder = SH.NameSpace(FileOrFolderToCompress)

        SF.CopyHere(DF)

      End Sub

      Sub Expand(ByVal CompressedFileName As String, ByVal ExpandedFolder As String)

        Dim Sh As New Shell

        Dim SF As Folder = Sh.NameSpace(CompressedFileName)

        Dim DF As Folder = Sh.NameSpace(ExpandedFolder)

        For Each F As FolderItem In SF.Items

          DF.CopyHere(F)

        Next

      End Sub

    End 

    Use it like this:
       

    Public Class Form1

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim FBDlg As New FolderBrowserDialog

        FBDlg.ShowDialog()

        Dim ShlZip As New ShellZip

        ShlZip.Compress("C:\Temp\Test.zip", FBDlg.SelectedPath)

      End Sub

    End Class
    Saturday, December 12, 2009 6:25 PM

All replies

  • Try this http://www.vb-helper.com/howto_2005_compress_folder.html

    or

    http://weblogs.asp.net/dneimke/archive/2005/02/25/380273.aspx  this link is for C# but you can convert the code to vb at http://www.carlosag.net/Tools/CodeTranslator/

    kaymaf
    If that what you want, take it. If not, ignored it and no complain
    Saturday, December 12, 2009 5:40 AM
  • Can't get it work (its my fist time with tis kind of program)

    Tis is my code:

        Private Shared Sub WriteZipFile(ByVal filesToZip As String, ByVal writeToFilePath As String)
            Try
    
                Dim crc As Crc32 = New Crc32
                Dim s As ZipOutputStream = New ZipOutputStream(File.Create(writeToFilePath))
                s.SetLevel(9)
                ' 0 - store only to 9 - means best compression
                Dim i As Integer = 0
                Do While (i < filesToZip.Length)
                    ' Must use a relative path here so that files show up in the Windows Zip File Viewer
                    ' .. hence the use of Path.GetFileName(...)
                    Dim entry As ZipEntry = New ZipEntry(Path.GetFileName(filesToZip(i)))
                    entry.DateTime = DateTime.Now
                    ' Read in the 
                    Dim fs As FileStream = File.OpenRead(filesToZip(i))
                    Dim buffer() As Byte = New Byte((fs.Length) - 1) {}
                    fs.Read(buffer, 0, buffer.Length)
                    ' set Size and the crc, because the information
                    ' about the size and crc should be stored in the header
                    ' if it is not set it is automatically written in the footer.
                    ' (in this case size == crc == -1 in the header)
                    ' Some ZIP programs have problems with zip files that don't store
                    ' the size and crc in the header.
                    entry.Size = fs.Length
                    fs.Close()
                    crc.Reset()
                    crc.Update(buffer)
                    entry.Crc = crc.Value
                    s.PutNextEntry(entry)
                    s.Write(buffer, 0, buffer.Length)
                    i = (i + 1)
                Loop
                s.Finish()
                s.Close()
            Catch ex As Exception
    
            End Try
        End Sub
    And tis is how i use it:

    WriteZipFile(TextBox2.Text, TextBox3.Text)
    Help, Thanks.
    Saturday, December 12, 2009 5:59 AM
  • what error did you get?

    Did you download the SharpZipLib.dll ? if not, you can download it from http://www.icsharpcode.net/OpenSource/SharpZipLib/

    kaymaf
    If that what you want, take it. If not, ignored it and no complain
    Saturday, December 12, 2009 6:07 AM
  • Did not get any error,just no zip is created...
    Yes i have the SharpZipLib.dll in my reference and i have tose line of code at the very first line of my program:

    Imports

     

    ICSharpCode.SharpZipLib.Checksums

    Imports

     

    ICSharpCode.SharpZipLib.Zip

    Imports

     

    ICSharpCode.SharpZipLib.GZip

    Imports

     

    System.IO

    Imports

     

    System.Text

    • Edited by FFA702 Saturday, December 12, 2009 6:21 AM missing information
    Saturday, December 12, 2009 6:19 AM
  • Did not get any error,just no zip is created...


    Try this, i just copy this out of my program and it works.

    Imports ICSharpCode.SharpZipLib.GZip
    Imports ICSharpCode.SharpZipLib.Zip
    Imports ICSharpCode.SharpZipLib.Tar
    Imports ICSharpCode.SharpZipLib.BZip2
    Imports ICSharpCode.SharpZipLib.Encryption
    Imports ICSharpCode.SharpZipLib.Checksums
    
    
           Public sub WriteFileToZip(Byval fileToZip() AS String,byval saveLocation As String)  
                
                Dim zipf As ZipOutputStream = New ZipOutputStream(File.Create(saveLocation))
                zipf.SetLevel(9)
                'Dim buffer() As Byte = New Byte(4096)
                Dim buffer() As Byte = New Byte(4095) {}
                Dim str As FileStream            
                For Each sfile As String In fileToZip
                    Dim entryFile As ZipEntry = New ZipEntry(Path.GetFileName(sfile))
                    entryFile.DateTime = DateTime.Now
                    zipf.PutNextEntry(entryFile)
                    StatusBar1.Text = sfile
                    str = File.OpenRead(sfile)
                    Dim sb As Integer
                    Do
                        sb = str.Read(buffer, 0, buffer.Length)
                        zipf.Write(buffer, 0, sb)
    
                    Loop While (sb > 0)
                  ' 
                Next
                zipf.Finish()
                zipf.Close()
                str.Close()
                
                End Sub
    To use this code, just call the method
    Dim allFiles() As String ={TextBox2.Text}

    WriteFileToZip(allFiles, "C:\locationtosave.zip")

    kaymaf
    If that what you want, take it. If not, ignored it and no complain
    Saturday, December 12, 2009 6:38 AM
  • Well,i get an error here:

    str = File.OpenRead(sfile)

    It say "acses denied".
    I never knew it was that hard to zip a file in vb...

    Saturday, December 12, 2009 6:48 AM
  • Well,i get an error here:

    str = File.OpenRead(sfile)

    It say "acses denied".
    I never knew it was that hard to zip a file in vb...


    You don't have permission to access the file.  Has nothing to do with programming.  Can you navigate to the file in explorer?
    Saturday, December 12, 2009 9:59 AM
  • Yes,its a normal folder on my desktop called "Program"  with some file inside.
    The file is marked as read only...Can i remotly change the permission using VB.NET?
    Saturday, December 12, 2009 4:52 PM
  • Yes,its a normal folder on my desktop called "Program"  with some file inside.

    You can't open a folder with File.OpenRead().  sFile must be the path to a file.  But I don't think that is the problem.  The error message should point you to a resolution of your problem.
    Saturday, December 12, 2009 4:56 PM
  • I must ZIP a folder,not a file...
    Saturday, December 12, 2009 4:58 PM
  • I must ZIP a folder,not a file...

    You can't zip anything until you resolve you errors.
    Saturday, December 12, 2009 5:02 PM
  • I can zip a file.
    My folder is read only ,is there a way to remove the read only in VB?
    And how can i zip a folder?
    Saturday, December 12, 2009 5:12 PM
  • I can zip a file.
    My folder is read only ,is there a way to remove the read only in VB?
    And how can i zip a folder?


    I don't know anything about the program that you're using (I use another one), but my guess is that you'll need to recurse the folder yourself, adding each file from that folder into the zip file.

    Hope this helps
    Saturday, December 12, 2009 5:14 PM
  • If you can zip a folder using the native Windows compression found on the Explorer context menu: "Sent To --> Compressed (zipped) Folder", this class provides the same functionality.

    Imports System.IO

    Imports Shell32 'Reference Microsoft Shell Controls And Automation on the COM tab.

    Public Class ShellZip

      Sub Compress(ByVal CompressedFileName As String, ByVal FileOrFolderToCompress As String)

        Dim B(21) As Byte

        B(0) = 80 : B(1) = 75 : B(2) = 5 : B(3) = 6

        File.WriteAllBytes(CompressedFileName, B) 'Make an empty PKZip file.

        Dim SH As New Shell

        Dim SF As Folder = SH.NameSpace(CompressedFileName)

        Dim DF As Folder = SH.NameSpace(FileOrFolderToCompress)

        SF.CopyHere(DF)

      End Sub

      Sub Expand(ByVal CompressedFileName As String, ByVal ExpandedFolder As String)

        Dim Sh As New Shell

        Dim SF As Folder = Sh.NameSpace(CompressedFileName)

        Dim DF As Folder = Sh.NameSpace(ExpandedFolder)

        For Each F As FolderItem In SF.Items

          DF.CopyHere(F)

        Next

      End Sub

    End 

    Use it like this:
       

    Public Class Form1

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim FBDlg As New FolderBrowserDialog

        FBDlg.ShowDialog()

        Dim ShlZip As New ShellZip

        ShlZip.Compress("C:\Temp\Test.zip", FBDlg.SelectedPath)

      End Sub

    End Class
    Saturday, December 12, 2009 6:25 PM
  • It may work,but i dont find the shell library on the COM tab.
    Sunday, December 13, 2009 4:16 PM
  • It may work,but i dont find the shell library on the COM tab.

    Then browse to it in System32\shell32.dll.
    Sunday, December 13, 2009 4:23 PM
  • I got it,but...i cant use shell to start application now...
    Sunday, December 13, 2009 4:29 PM
  • I got it,but...i cant use shell to start application now...

    Fully qualify shell or remove the shell32 import and fully qualify the shell32 classes.
    Sunday, December 13, 2009 4:43 PM
  • i wanna code to download multifiles in 2.0 framwork
    Friday, April 01, 2011 11:55 AM
  • Hello Friend I am Run This Code Run Successfully ........
    Use Help path : -   http://www.codeguru.com
    -----------------------------------------------------------------------------------------------------------------------
    Imports System.Security.Cryptography
    Imports System.IO
    Imports System.Windows.Forms
    Imports ICSharpCode.SharpZipLib
    Imports ICSharpCode
    ---------------------------------------------------------------------------------------------------------------------
    Partial Class Admin_unZip
        Inherits System.Web.UI.Page
        Protected Sub BtnUnZip_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
            'Dim ZipFileName As String = Server.MapPath(FileUpload1.FileName)
            Dim ZipFileName As String = "D:\ Project Name\DownloadCSV\kamalsaxena.zip"  'Upload  file path
            Dim ExtratePath As String = "D:\ Project Name\UploadCSV\" 'file Extract path
            ExtractArchive(ZipFileName, ExtratePath)
        End Sub
    ----------------------------------------------------------------------------------------------------------------------
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
           
        End Sub
    ----------------------------------------------------------------------------------------------------------------------
        Public Sub ExtractArchive(ByVal zipFilename As String, ByVal ExtractDir As String)
            Dim Redo As Integer = 1
            Dim MyZipInputStream As Zip.ZipInputStream
            Dim MyFileStream As FileStream
            MyZipInputStream = New Zip.ZipInputStream(New FileStream(zipFilename, FileMode.Open, FileAccess.Read))
            Dim MyZipEntry As Zip.ZipEntry = MyZipInputStream.GetNextEntry
            Directory.CreateDirectory(ExtractDir)
            While Not MyZipEntry Is Nothing
                If (MyZipEntry.IsDirectory) Then
                    Directory.CreateDirectory(ExtractDir & "\" & MyZipEntry.Name)
                Else
                    If Not Directory.Exists(ExtractDir & "\" & Path.GetDirectoryName(MyZipEntry.Name)) Then
                        Directory.CreateDirectory(ExtractDir & "\" & Path.GetDirectoryName(MyZipEntry.Name))
                    End If
                    MyFileStream = New FileStream(ExtractDir & "\" & MyZipEntry.Name, FileMode.OpenOrCreate, FileAccess.Write)
                    Dim count As Integer
                    Dim buffer(4096) As Byte
                    count = MyZipInputStream.Read(buffer, 0, 4096)
                    While count > 0
                        MyFileStream.Write(buffer, 0, count)
                        count = MyZipInputStream.Read(buffer, 0, 4096)
                    End While
                    MyFileStream.Close()
                End If
                Try
                    MyZipEntry = MyZipInputStream.GetNextEntry
                Catch ex As Exception
                    MyZipEntry = Nothing
                End Try
            End While
            If Not (MyZipInputStream Is Nothing) Then MyZipInputStream.Close()
            If Not (MyFileStream Is Nothing) Then MyFileStream.Close()
        End Sub
    End Class

    kamal saxena


    Tuesday, August 09, 2011 11:38 AM