How to provide a dynamic name to a ZIP File?

.net , csharp Sydney, Australia
  • 13 years ago

    Hi All,

    I am trying to zip a folder in .NET using J#.

    I want to concatenate system date with the name of the output file.

    Anybody having any idea??

    Many Thanks

  • 13 years ago

    Hi there,

    Imports System.IO
    Imports ICSharpCode.SharpZipLib.Zip
    Imports ICSharpCode.SharpZipLib.Checksums

    Namespace MyProject.Services.Compression

    Public Class ZipHelper

    #Region " Private Shared Methods "

    Private Shared Function CreateEntry(ByVal filename As String, ByVal baseFolder As String, ByRef e As ZipEntry) As Byte()

    Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)

    Dim buffer(fs.Length) As Byte
    fs.Read(buffer, 0, buffer.Length)


    Dim crc As New Crc32

    Dim entry As New ZipEntry(filename.Replace(baseFolder, "").Trim("/"c, "\"c))
    entry.DateTime = DateTime.Now
    entry.Size = buffer.Length
    entry.Crc = crc.Value

    e = entry
    Return buffer

    End Function

    #End Region

    #Region " Public Shared Methods "

    Public Shared Sub CompressDirectory(ByVal path As String, ByVal target As String)

    Dim fs As New FileStream(target, FileMode.Create, FileAccess.Write)
    Dim zip As New ZipOutputStream(fs)

    Dim PathInfo As New DirectoryInfo(path)

    For Each fn As String In Directory.GetFiles(path, "*", SearchOption.AllDirectories)

    Dim e As ZipEntry = Nothing
    Dim buffer As Byte() = CreateEntry(fn, PathInfo.Parent.FullName, e)

    zip.Write(buffer, 0, buffer.Length)



    End Sub

    Public Shared Sub Decompress(ByVal filename As String, ByVal target As String)

    Dim fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
    Dim zip As New ZipInputStream(fs)

    Dim entry As ZipEntry
    While (True)

    entry = zip.GetNextEntry
    If (IsNothing(entry)) Then Exit While

    Dim data(entry.Size) As Byte
    zip.Read(data, 0, entry.Size)

    Dim trg As String = Path.Combine(target, Path.GetDirectoryName(entry.Name))

    If (Not Directory.Exists(trg)) Then
    End If

    If (Not entry.IsDirectory) Then

    Dim fs2 As New FileStream(Path.Combine(target, entry.Name), FileMode.Create, FileAccess.Write)
    fs2.Write(data, 0, entry.Size)

    End If

    End While


    End Sub

    #End Region

    End Class

    End Namespace

    Using Example :

    ZipHelper.CompressDirectory(Path.Combine(installingPath, "Data"), Path.Combine(Common.LavanConfig.BackupDestination, String.Format("{0}.zip", DateTime.Now.Year & "-" & DateTime.Now.Month & "-" & DateTime.Now.Day & "-" & DateTime.Now.Hour & "-" & DateTime.Now.Minute)))

    Download SharpZipLib :

    Convert to C# :

  • 13 years ago


    Thanks for the reply,But my code is in J# which doesn't recognise  ziphelper.Below is the code i am using.

    I want to pass test as outputfile name instead of  zipFileName

     string path_str = DateTime.Now.Day.ToString() + "/"+ DateTime.Now.Month.ToString()+ "/" + DateTime.Now.Year.ToString() + "::"+ DateTime.Now.Hour.ToString() +":" + DateTime.Now.Minute.ToString() + ":" + DateTime.Now.Second.ToString();


    string test ="C://output_" + path_str.ToString();

    test=test + ".zip";    //should be the final name


    zipFileName = "C://"; fis1 = new;  // works fine with zipFileName ,but gives error with test.


    Please help.



  • 13 years ago


     The error is because you are using invalid characters (/, :) in filename. Try replacing these characters with -- or any other valid character.

Post a reply

Enter your message below

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.” - Jon Ribbens