SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

zip folders using sharpziplib

Last post 08-21-2007 4:08 PM by Martin#. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 08-21-2007 3:21 PM

    • paxa
    • Top 500 Contributor
    • Joined on 07-16-2007
    • Posts 6

    zip folders using sharpziplib

    i'm trying to develop an application in vb.net using visual studio 2005  that zips some files, the zip files is working like a charm.

    but now i was asked to add a part that zips folders and it's contents, the files inside the folders

    Any ideas???
    thanks in advance

     

    Filed under: ,
  • 08-21-2007 3:50 PM In reply to

    Re: zip folders using sharpziplib

    Hello,

    If you are using .Net>1.1 the System.IO.Directory.GetFiles method is able todo a recursive search threw all directorys.

    So no more recursive method needed.

     You can downoad sorcecode and examples here: http://sharpdevelop.net/OpenSource/SharpZipLib/Download.aspx

    A basic example from the link is:

    Dim sourceDir As String = txtSourceDir.Text.Trim()

     

    ' Simple sanity checks

    If sourceDir.Length = 0

    MessageBox.Show("Please specify a directory")

    Return

    Else

    If Not Directory.Exists(sourceDir)

    MessageBox.Show(sourceDir, "Directory not found")

    Return

    End If

    End If

     

    Dim targetName As String = txtZipFileName.Text.Trim()

    If targetName.Length = 0 Then

    MessageBox.Show("No name specified", "Zip file name error")

    Return

    End If

     

    Dim astrFileNames() As String = Directory.GetFiles(sourceDir)

    Dim strmZipOutputStream As ZipOutputStream

     

    strmZipOutputStream = New ZipOutputStream(File.Create(targetName))

    REM Compression Level: 0-9

    REM 0: no(Compression)

    REM 9: maximum compression

    strmZipOutputStream.SetLevel(9)

     

    Dim strFile As String

     

    For Each strFile In astrFileNames

    Dim strmFile As FileStream = File.OpenRead(strFile)

    Dim abyBuffer(strmFile.Length - 1) As Byte

     

    strmFile.Read(abyBuffer, 0, abyBuffer.Length)

    Dim objZipEntry As ZipEntry = New ZipEntry(strFile)

     

    objZipEntry.DateTime = DateTime.Now

    objZipEntry.Size = strmFile.Length

    strmFile.Close()

    strmZipOutputStream.PutNextEntry(objZipEntry)

    strmZipOutputStream.Write(abyBuffer, 0, abyBuffer.Length)

     

    Next

     

    strmZipOutputStream.Finish()

    strmZipOutputStream.Close()

     

    MessageBox.Show("Operation complete")

     

    All the best,

    Martin

  • 08-21-2007 4:02 PM In reply to

    • paxa
    • Top 500 Contributor
    • Joined on 07-16-2007
    • Posts 6

    Re: zip folders using sharpziplib

    this is the code that i have so far.

    Private Sub zipall(ByVal loc As String)

    Dim azaf As String = (System.DateTime.Now.Year - 1).ToString & "A.zip " '# nome do ficheiro anual

    Dim azfile As String = (loc + azaf) '# caminho e nome do ficheiro

    Dim allzfile() As String = Directory.GetFiles(loc)

    Dim allzoutp As ZipOutputStream

    Dim ndate, adate As String '# strings com as datas para comparar

    ndate = (Now.Year - 1).ToString

    If File.Exists(azfile) Then

    Exit Sub '# se o ficheiro zip já existe sai da sub

    End If

     

    allzoutp = New ZipOutputStream(File.Create(azfile))

    allzoutp.SetLevel(7)

    Dim allf As String

    For Each allf In allzfile

    adate = (Directory.GetLastWriteTime(allf).Year.ToString)

    If adate = ndate Then

    Try

    Dim allzflstre As FileStream = File.OpenRead(allf)

    Dim allzbuffer(allzflstre.Length - 1) As Byte

    allzflstre.Read(allzbuffer, 0, allzbuffer.Length)

    Dim allzentry As ZipEntry = New ZipEntry(IO.Path.GetFileName(allf))

    allzentry.DateTime = DateAndTime.Now

    allzentry.Size = allzflstre.Length

    allzflstre.Close()

    allzoutp.PutNextEntry(allzentry)

    allzoutp.Write(allzbuffer, 0, allzbuffer.Length)

     

     

    Catch ex As Exception

    errorstring = ""

    errorstring = ex.ToString

    logs()

    End Try

    End If

    Next

    '# liberta o processo zip

    allzoutp.Finish()

    allzoutp.Close()

    End Sub

     what is it that i need to change in order for it to zip folders ?????

     

     

  • 08-21-2007 4:08 PM In reply to

    Re: zip folders using sharpziplib

Page 1 of 1 (4 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.