Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Zip w/ relative path. Please help.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #19778 |

    I am working on a windows service that retrieves a data set from a SQL server database that contains paths to files that are to be zipped and moved to an archive location. I have an object that returns a custom collection that contains the file paths that are being requested for archival. I need to be able to archive each file within the collection maintaining the relative path as if it were requested from the C:\ location. For example, the collection may have 3 files that are located at the following locations:

    \\ServerName\SST\Working\User1\textfile1.txt
    \\ServerName\SST\Working\User1\receive\textfile2.txt
    \\ServerName\SST\Working\User1\print\textfile3.txt

    I need the files to show up in the zip file as if the User1 Directory were C:\. So the files displayed in the zip would look like this:

    textfile1.txt
    receive\textfile2.txt
    print\textfile2.txt

    Here is what I have so far:

    <pre>
    <color=”green”>‘// sFile contains the destination zip file name</color>
    Dim zipFile As AbstractFile = New DiskFile(sFile)

    <color=”green”>’// Make sure the file does not exist.</color>
    If zipFile.Exists Then
    zipFile.Delete()
    End If

    <color=”green”>’// Create new archive file</color>
    Dim archive As New ZipArchive(zipFile)

    <color=”green”>‘// Commands is the collection of file paths. Command is the base object</color>
    For Each oCommand As YumSend.Common.DataManagement.Command In Commands
    <color=”green”>‘// sWorkingDirectory contains the UNC path of \\ServerName\SST\Working\
    ‘// Project.Description contains the User name. In this example it is User1.
    ‘// oCommand.Destination is the path of the file. The substring(3) removes C:\ from the string</color>

    Dim source As AbstractFile = New DiskFile(sWorkingDirectory & Project.Description & “\” & oCommand.Destination.Substring(3))
    source.CopyTo(archive, True)
    Next

    </pre>

    Using this method, the relative path is not captured and file names with the same name are overwritten. What is the best way to go about doing this?

    Thanks in advance for your help,
    Seth

    Imported from legacy forums. Posted by Exodore (had 4140 views)

    User (Old forums)
    Member
    Post count: 23064

    You will need to create the corresponding path into the archive, then copy the file to this path. You can do this by using either ZippedFolder or ZippedFile class.

    So in your code, you would modify the foreach loop like this :

    <pre>
    For Each oCommand As YumSend.Common.DataManagement.Command In Commands

    Dim source As AbstractFile = New DiskFile(sWorkingDirectory & Project.Description & “\” & oCommand.Destination.Substring(3))

    Dim zippedFile As ZippedFile = new ZippedFile( archive, oCommand.Destination )
    source.CopyTo(zippedFile, True)
    Next
    </pre>

    Imported from legacy forums. Posted by André (had 281 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Andre. That works perfectly.

    Imported from legacy forums. Posted by Exodore (had 199 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi,
    I ahve try to replicate what you’ve done in C# but i get the following error
    cannot convert from ‘Xceed.Zip.ZipArchive’ to ‘Xceed.FileSystem.AbstractFile’

    there is the code:

    string[] Files = Directory.GetFiles(dir);
    ZipArchive archive = new ZipArchive(zipFile);
    foreach (string file in Files)
    {
    AbstractFile source = new DiskFile(@”test\” + file.Remove(0,file.LastIndexOf(@”\”)+1));
    ZippedFile zippedFile = new ZippedFile(archive, file);
    source.CopyTo(zippedFile, true);
    }

    if i change it to a Abstract id does not work.
    any idear what i am doing wrong.

    Imported from legacy forums. Posted by alban (had 285 views)

    User (Old forums)
    Member
    Post count: 23064

    Dot it working there it is:

    DiskFile zipFile = new DiskFile(zipFilename);
    if (zipFile.Exists)
    zipFile.Delete();

    string[] Files = Directory.GetFiles(dir);
    ZipArchive archive = new ZipArchive(zipFile);
    foreach (string file in Files)
    {
    AbstractFile source = new DiskFile(file);
    AbstractFile zippedFile = new ZippedFile(archive, @”\test\”);
    source.CopyTo(zippedFile, true);
    }

    Imported from legacy forums. Posted by alban (had 4806 views)

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.