Trying to get the AddSelectedFiles() method to store relative path to file

Nov 10, 2009 at 5:10 PM

I am using this nifty zip dll for the first time.  I am trying to use the AddSelectedFiles() method to add files to a zip file.  In this simple example, I can use AddFile() which produces the desired results but beyond my simple example I do want to use AddSelectedFiles() so I can use wildcards in the filename.  I can use AddFile() as follows:

Using zip As ZipFile = New ZipFile

zip.AddFile("c:\users\public\myapp\mydatabase.mdb", "")

zip.Save(zipFileName)

End Using

This works just fine.  The file is added to the 'root' of the zip file (no path is shown when viewing the file with WinZip.  However, when I substitute AddFile() for AddSelectedFiles() like this:

Using zip As ZipFile = New ZipFile

zip.AddSelectedFiles("name = 'mydatabase.mdb'", "c:\users\public\myapp\", "", False)

zip.Save(zipFileName)

End Using

I get a zip file that contains mydatabase.mdb but it is not stored in the 'root' folder of the zip file.  Rather, the zip file shows the file to be in "users\public\myapp\".  Note the path stored in the zip file does not begin with a backslash. 

I am running Visual Studio 2008.  The operating system is 64-bit Vista if that matters.  Any advise would be greatly appreciated!

Andy

 

Nov 10, 2009 at 5:16 PM

Just for grins, I tried the following variations:

zip.AddSelectedFiles("name = 'mydatabase.mdb'", "c:\users\public\myapp\", "blah", False)

zip.AddSelectedFiles("name = 'mydatabase.mdb'", "c:\users\public\myapp\", "blah\", False)

zip.AddSelectedFiles("name = 'mydatabase.mdb'", "c:\users\public\myapp\", "\blah\", False)

All made no difference.  In all cases (as in my original message) I got a zip file containing mydatabase.mdb, but there was an internal zip path recorded of "users\public\myapp\".  It is as if the 3rd parameter is being ignored. 

Andy

Nov 10, 2009 at 5:23 PM

I am using ionic.zip.reduced.dll version 1.8.4.27.

Coordinator
Nov 11, 2009 at 9:23 AM

Andy, thanks for using the forums.

I understand what you're saying, and I cannot reproduce that behavior. The version of the OS should not matter.  Here's the test case I'm using, full source code.  Can you please try this and send me the output you get:

' AddSelected2.vb
'
' For Andy.
'
' Instructions:
'  1. modify the code to specify the directories and filespecs you want.
'  2. Compile with:
'       c:\.net3.5\vbc.exe /R:Ionic.Zip.Reduced.dll /out:AddSelected2.exe AddSelected2.vb
'  3. open command prompt and run it
'
' ------------------------------------------------------------------
'
' Copyright (c) 2009 Dino Chiesa
' All rights reserved!
'
' ------------------------------------------------------------------


Imports System
Imports Ionic.Zip


Namespace Ionic.TestForAndy


Public Class AddSelected2

    Dim sb As System.Text.StringBuilder

    Private Sub StdErrorRead(sender As Object,  e As System.Diagnostics.DataReceivedEventArgs)
        If Not String.IsNullOrEmpty(e.Data) Then
            sb.Append(e.Data)
        End If
    End Sub

    
    Public Sub Run()

        Dim zipFileName As String = "AddSelected2.zip"
        
        Dim dirOnDisk1 As String = "c:\dinoch\dev\ISAPI\IIRF\v2.0\AdminGuide"
        Dim filespec1 As String = "IirfGuide.shfbproj"
        Dim dirInArchive1 As String = ""
        
        Dim dirOnDisk2 As String = "c:\Desktop\PhonePhotos"
        Dim filespec2 As String = "*.jpg"
        Dim dirInArchive2 As String = "Photos"

        If System.IO.File.Exists(zipFileName)  Then
            System.IO.File.Delete(zipFileName)
        End If 
        
        System.Console.WriteLine("Using DotNetZip version: {0}", ZipFile.LibraryVersion)
        
        Using zip As ZipFile = New ZipFile
            zip.StatusMessageTextWriter = System.Console.Out
            zip.AddSelectedFiles("name = '" & filespec1 & "'", dirOnDisk1, dirInArchive1, False)
            zip.AddSelectedFiles("name = '" & filespec2 & "'", dirOnDisk2, dirInArchive2, False)
            zip.Save(zipFileName)

        End Using

        
        '' now, list the contents of the file
        System.Console.WriteLine
        System.Console.WriteLine("Checking the generated zip...")
        Dim numErrors As Integer = 0
        Using zip As ZipFile = ZipFile.Read(zipFileName)
            Dim e As ZipEntry
            For Each e In zip
                System.Console.WriteLine("{0}", e.FileName)
                Dim d As String
                d = System.IO.Path.GetDirectoryName(e.FileName)
                If (d <> dirInArchive1  AND  d <> dirInArchive2) Then
                    System.Console.WriteLine(" *** Path on entry {0} is unexpected.", e.FileName)
                    numErrors = numErrors + 1
                End If
            Next
        End Using
        
        System.Console.WriteLine(New String(Chr(45), 72))
        System.Console.WriteLine("# unexpected paths: {0}", numErrors)

    End Sub


    Public Shared Sub Main(ByVal args As String())
        Try 
            Dim X as New AddSelected2()
            X.Run
        Catch exc1 As Exception
            Console.WriteLine("Exception: {0}", exc1.ToString)
        End Try
    End Sub


End Class


End Namespace

 

Nov 11, 2009 at 3:08 PM

Hi Cheeso.

I figured out the problem using your test program.  Originally when I ran your test, I set the variable ‘dirOnDisk1’ equal to “ c:\users\public\myapp\”.  The resulting zip file contained the full path rather than putting it into the root of the zip file.  Then, I changed it to “c:\users\public\myapp” because in the original sample you sent to me did not have the ending backslash.  It worked!  I would have never guessed to remove the ending backslash in the directory designation.

Andy

From: Cheeso [mailto:notifications@codeplex.com]
Sent: Wednesday, November 11, 2009 4:23 AM
To: andy.pelkey@earthlink.net
Subject: Re: Trying to get the AddSelectedFiles() method to store relative path to file [DotNetZip:74687]

From: Cheeso

Andy, thanks for using the forums.

I understand what you're saying, and I cannot reproduce that behavior. The version of the OS should not matter. Here's the test case I'm using, full source code. Can you please try this and send me the output you get:

' AddSelected2.vb
'
' For Andy.
'
' Instructions:
'  1. modify the code to specify the directories and filespecs you want.
'  2. Compile with:
'       c:\.net3.5\vbc.exe /R:Ionic.Zip.Reduced.dll /out:AddSelected2.exe AddSelected2.vb
'  3. open command prompt and run it
'
' ------------------------------------------------------------------
'
' Copyright (c) 2009 Dino Chiesa
' All rights reserved!
'
' ------------------------------------------------------------------
 
 
Imports System
Imports Ionic.Zip
 
 
Namespace Ionic.TestForAndy
 
 
Public Class AddSelected2
 
    Dim sb As System.Text.StringBuilder
 
    Private Sub StdErrorRead(sender As Object,  e As System.Diagnostics.DataReceivedEventArgs)
        If Not String.IsNullOrEmpty(e.Data) Then
            sb.Append(e.Data)
        End If
    End Sub
 
    
    Public Sub Run()
 
        Dim zipFileName As String = "AddSelected2.zip"
        
        Dim dirOnDisk1 As String = "c:\dinoch\dev\ISAPI\IIRF\v2.0\AdminGuide"
        Dim filespec1 As String = "IirfGuide.shfbproj"
        Dim dirInArchive1 As String = ""
        
        Dim dirOnDisk2 As String = "c:\Desktop\PhonePhotos"
        Dim filespec2 As String = "*.jpg"
        Dim dirInArchive2 As String = "Photos"
 
        If System.IO.File.Exists(zipFileName)  Then
            System.IO.File.Delete(zipFileName)
        End If 
        
        System.Console.WriteLine("Using DotNetZip version: {0}", ZipFile.LibraryVersion)
        
        Using zip As ZipFile = New ZipFile
            zip.StatusMessageTextWriter = System.Console.Out
            zip.AddSelectedFiles("name = '" & filespec1 & "'", dirOnDisk1, dirInArchive1, False)
            zip.AddSelectedFiles("name = '" & filespec2 & "'", dirOnDisk2, dirInArchive2, False)
            zip.Save(zipFileName)
 
        End Using
 
        
        '' now, list the contents of the file
        System.Console.WriteLine
        System.Console.WriteLine("Checking the generated zip...")
        Dim numErrors As Integer = 0
        Using zip As ZipFile = ZipFile.Read(zipFileName)
            Dim e As ZipEntry
            For Each e In zip
                System.Console.WriteLine("{0}", e.FileName)
                Dim d As String
                d = System.IO.Path.GetDirectoryName(e.FileName)
                If (d <> dirInArchive1  AND  d <> dirInArchive2) Then
                    System.Console.WriteLine(" *** Path on entry {0} is unexpected.", e.FileName)
                    numErrors = numErrors + 1
                End If
            Next
        End Using
        
        System.Console.WriteLine(New String(Chr(45), 72))
        System.Console.WriteLine("# unexpected paths: {0}", numErrors)
 
    End Sub
 
 
    Public Shared Sub Main(ByVal args As String())
        Try 
            Dim X as New AddSelected2()
            X.Run
        Catch exc1 As Exception
            Console.WriteLine("Exception: {0}", exc1.ToString)
        End Try
    End Sub
 
 
End Class
 
 
End Namespace
 

Read the full discussion online.

To add a post to this discussion, reply to this email (DotNetZip@discussions.codeplex.com)

To start a new discussion for this project, email DotNetZip@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Coordinator
Nov 11, 2009 at 4:41 PM

Thanks Andy, sounds like a bug.  I'll get that checked out.

In the meantime you are on your way, I guess.

 

Nov 11, 2009 at 4:49 PM

Yes I am able to work around it easily enough.

From: Cheeso [mailto:notifications@codeplex.com]
Sent: Wednesday, November 11, 2009 11:41 AM
To: andy.pelkey@earthlink.net
Subject: Re: Trying to get the AddSelectedFiles() method to store relative path to file [DotNetZip:74687]

From: Cheeso

Thanks Andy, sounds like a bug. I'll get that checked out.

In the meantime you are on your way, I guess.

Read the full discussion online.

To add a post to this discussion, reply to this email (DotNetZip@discussions.codeplex.com)

To start a new discussion for this project, email DotNetZip@discussions.codeplex.com

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on codePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com

Coordinator
Nov 11, 2009 at 4:51 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Jul 19, 2013 at 5:54 PM
I know this is a very old post but feel the need to resurrect it.

Just like Apelkey I am finding the same issue: the relative path is copied into the zip file. However I have the latest patches and software and is 4 years later and got it again.
Certainly without this link I would not had found out what my bug was.

There is still a bug if this condition is met on looking at the code:

dirOndisk1 does not handle paths returned by unix machines. I happened to get the relative path where the files needed to be zipped from a box that contains those remote paths. The path returned contained no ending slash but had a forward slash in the middle of the string as in "...\folder1\folder2\folder3/folder4/folder5"
Once I replaced all "/" for "\" in the string the code works great.

Would be nice to include this in a patch because to find this issue is almost impossible. Thanks for this post indeed.