Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Relative path with filtered list of files

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

    Hi, hope someone can help…

    I am trying to zip up a set of files from a directory and several subdirectories. However, I do not want ALL the files in these directories, only a select list of them.

    I currently have a c# List of strings that appear similar to:

    c:\unittest\test1.txt
    c:\unittest\test2.txt
    c:\unittest\subdir\test1.txt
    c:\unittest\subdir2\test2.txt

    I’ve been converting the list to an array of strings using

    string[] files = fileMask.ToArray()

    Is there a way I can feed this into a zip archive, retaining the RELATIVE path? I’ve gotten it to zip up the list of files, but it always had the complete path, ie:

    QuickZip.Zip(zipFilename, false, false, true, files)

    However, when I do this, I get the complete path, whereas I wish all the files to be relative to the base c:\unittest folder, so the contents would be:

    test1.txt
    test2.txt
    subdir\test1.txt
    subdir2\test2.txt

    currently, I get

    unittest\test1.txt
    unittest\test2.txt
    unittest\subdir\test1.txt
    unittest\subdir2\test2.txt

    inside the zip archive…

    I’ve tried using a DiskFolder CopyFilesTo and providing the string array into the params[] field, but this returns an error that states there are paths within the filemask…

    Any suggestions?

    -Blayne

    Imported from legacy forums. Posted by bwatt (had 3400 views)

    User (Old forums)
    Member
    Post count: 23064

    Here’s the current code:

    public static void AddFilesToZip(string zipFilename, string sourceFolder, List<string> fileMask, bool recursive, string password)
    {
    if (sourceFolder.Length == 0)
    throw new ArgumentException(“You must specify a source folder from which files will be added to the zip file.”, “sourceFolder”);
    // Create a DiskFile object for the specified zip filename

    DiskFile zipFile = new DiskFile(zipFilename);
    if (zipFile.Exists)
    {
    zipFile.Delete();
    }
    ZipArchive arc = new ZipArchive(zipFile);
    arc.BeginUpdate();
    foreach (string file in fileMask)
    {
    Trace.WriteLine(“File: ” + file);
    DiskFile source = new DiskFile(file);
    source.CopyTo(test, true);
    }
    arc.EndUpdate();
    }

    Imported from legacy forums. Posted by bwatt (had 388 views)

    User (Old forums)
    Member
    Post count: 23064

    sorry, small typo:

    source.CopyTo(test, true);

    should be :

    source.CopyTo(arc, true);

    -Blayne

    Imported from legacy forums. Posted by bwatt (had 304 views)

    User (Old forums)
    Member
    Post count: 23064

    Think I got it all working… posting for reference, in case I did something wrong…

    public static void AddFilesToZip(string zipFilename, string sourceFolder, List<FileData> fileMask, bool recursive, string password)
    {
    if (sourceFolder.Length == 0)
    throw new ArgumentException(“Please specify a source folder.”, “sourceFolder”);

    DiskFile zipFile = new DiskFile(zipFilename);
    if (zipFile.Exists)
    {
    zipFile.Delete();
    }
    ZipArchive arc = new ZipArchive(zipFile);
    arc.BeginUpdate();
    foreach (FileData file in fileMask)
    {
    Trace.WriteLine(“File: ” + file.PathFull + file.Name);
    AbstractFile source = new DiskFile(file.PathFull + file.Name);
    AbstractFile zip = new ZippedFile(zipFile, file.PathRelative + file.Name);
    source.CopyTo(zip, true);
    }
    arc.EndUpdate();
    }

    Imported from legacy forums. Posted by bwatt (had 4261 views)

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