0 votes
by (120 points)
edited by

The ZIP-files that are created using Rebex ZIP contains a rootfolder by the name 0, 1 or 2.

Why is this rootfolder created and how can it be avoided?

I use FileSet.

Using archive As New ZipArchive(Path.Combine(sProjectPath, project.GetZipName), Rebex.IO.Compression.ArchiveOpenMode.CreateNew)
    Dim fileSet As New Rebex.IO.FileSet(project.LocalPath)
    For Each include As String In project.Includes
        fileSet.Include(include, TraversalMode.MatchFilesDeep)
    Next
    For Each exclude As String In project.Excludes
        fileSet.Exclude(exclude)
    Next
    Dim result As ArchiveOperationResult = archive.Add(fileSet, TraversalMode.MatchFilesShallow, TransferMethod.Copy, ActionOnExistingFiles.OverwriteAll)
End Using

My project class looks like this

Public Class Project

    <XmlElement(ElementName:="type")>
    Public Property Type() As String

    <XmlElement(ElementName:="name")>
    Public Property Name() As String

    <XmlElement(ElementName:="localpath")>
    Public Property LocalPath() As String

    <XmlElement(ElementName:="subdirs")>
    Public Property SubDirs() As Boolean

    <XmlArray("includes"),
        XmlArrayItem(GetType(String), ElementName:="include")
        >
    Public Includes As New List(Of String)

    <XmlArray("excludes"),
        XmlArrayItem(GetType(String), ElementName:="exclude")
        >
    Public Excludes As New List(Of String)


    <XmlElement(ElementName:="enabled")>
    Public Property Enabled() As Boolean

    Public Function GetZipName() As String
        Return Me.Type().Replace(" ", "_") + "_" + Me.Name().Replace(" ", "_") + "_" + Date.Now.ToString("yyyyMMddHHmmss") + ".zip"
    End Function

End Class

The created zip file can be seen here:
https://drive.google.com/file/d/0Bxz1qoeLYqrwekVtcGd3NFhpQVE/view?usp=sharing

Applies to: Rebex ZIP

2 Answers

0 votes
by (73.6k points)

Can you please post here your source code which uses ZipArchive and FileSet objects?

The ZipArchive names folders according to names of source folders - there are no hardcoded values such as name 0, 1 or 2.

Is it also possible for you to share source ZIP file (if any) and produced ZIP files which contains 0, 1, 2 root folders?
Or at least some image which shows the problem?
I am not sure, whether I understand the problem correctly.

Thank you.

by (120 points)
I have added sourcecode and an example zip-file to my question.
0 votes
by (73.6k points)

Thank you for the code. I found that the problem is in your code.
Actually, it is caused by "unhandy" tolerance of VB.NET which allows to pass nonstring variables to string parameters. Your code wouldn't be compiled if written in C#.

Please see declarations of the Add method with FileSet argument:

  1. Add(FileSet set)
  2. Add(FileSet set, string archiveDirectoryPath)
  3. Add(FileSet set, string archiveDirectoryPath, TransferMethod transferMethod, ActionOnExistingFiles defaultActionOnExistingFiles)

You used this:
archive.Add(fileSet, TraversalMode.MatchFilesShallow, TransferMethod.Copy, ActionOnExistingFiles.OverwriteAll)

Because of VB.NET, second parameter was implicitly cast to string in runtime and third method was used. In case of TraversalMode.MatchFilesShallow a string "2" was passed (which leads to creation of root folder "2").

The correct solution is to call the method like this:
archive.Add(fileSet, "/", TransferMethod.Copy, ActionOnExistingFiles.OverwriteAll)

by (120 points)
Worked Perfect :-)


Thanks
...