Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET How to prserve the select path only

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

    I have Following Folder and Files Structure

    C:\Folder1\Folder2\File1,Fill2……Filen
    C:\Folder1\Folder2\Folder21\File1,file2,…..Filen
    C:\Folder1\Folder2\Folder22\
    C:\Folder1\Folder2\Folder23\

    I have selected Folder2 to zip
    Now I want to zip all the folders and Files under the Folder2
    I am using the QuickZip Class to do this.
    I want to preserve the path structure only from Folder 2 onwards.

    i am using following following code:
    For Zip–> QuickZip .Zip(“C:\Test1.zip”, False, True, True, “C:\Folder1\Folder2\*.*”)
    For UnZip–> QuickZip .Unzip(“C:\Test1.rpe”,”C:\TestFolder”, True, True, True, “*.*”)

    Suppose i Unzip to a Folder “C:\TestFolder\” my expectation is as follows
    C:\TestFolder\File1,Fill2……Filen
    C:\TestFolder\Folder21\File1,file2,…..Filen
    C:\TestFolder\Folder22\
    C:\TestFolder\Folder23\

    But result i am getting is as follows

    C:\TestFolder\Folder1\Folder2\File1,Fill2……Filen
    C:\TestFolder\Folder1\Folder2\Folder21\File1,file2,…..Filen
    C:\TestFolder\Folder1\Folder2\Folder22\
    C:\TestFolder\Folder1\Folder2\Folder23\

    Imported from legacy forums. Posted by Anil_R_B (had 5558 views)

    Xceed Support
    Member
    Post count: 5658

    Hi Anil_R_B,

    The result you are getting is what it should be. The design was made like this to mimics the way WinZip store the path when you use the option “Save full path info”.

    To keep the paths the way you want to do it, you will have to use the FileSystem approach for it.

    Here’s a code snippet that will zip the files and folders under your C:\Folder1\Folder2 folder to an archive and preserving the paths from this point.

    DiskFile zipFile = new DiskFile( @”C:\Test1.zip” );

    // Make sure we use a new and empty archive.
    if( zipFile.Exists )
    zipFile.Delete();

    ZipArchive archive = new ZipArchive( zipFile );

    DiskFolder source = new DiskFolder( @”C:\Folder1\Folder2″ );

    // No need to get recursively since calling CopyTo on a folder will
    // copy the whole structure.
    FileSystemItem[] items = source.GetItems( false );

    foreach( FileSystemItem item in items )
    {
    item.CopyTo( archive, true );
    }

    To unzip this archive, you then have the choice to use the QuickZip object or use the FileSystem approach.

    Hope this helps!

    Imported from legacy forums. Posted by Jacques [Xceed] (had 393 views)

    User (Old forums)
    Member
    Post count: 23064

    You need to use the FileSystem classes to accomplish this. Do something like the following :

    AbstractFile zip = new DiskFile( @”C:\Test1.zip” );
    ZipArchive archive = new ZipArchive( zip );

    //Zip
    AbstractFolder source = new DiskFolder( @”C:\Folder1\Folder2\” );
    source.CopyFilesTo( archive, true, true, “” );

    //Unzip
    AbstractFolder dest = new DiskFolder( @”C:\TestFolder\” );
    archive.CopyFilesTo( dest, true, true, “” );

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

    Xceed Support
    Member
    Post count: 5658

    That is also a way to go yes 🙂

    The difference between the two is if you use the approach I posted, empty folders will also be recreated in the archive and the way of Andre will not. (it will create the folders only if there is files inside that needs to be copied)

    Both ways works great.. it depends on you you need.

    Have a good day!

    Imported from legacy forums. Posted by Jacques [Xceed] (had 365 views)

    User (Old forums)
    Member
    Post count: 23064

    I would like to thank you all for a such quick response.
    I really apprreciate for the support.

    I will gat back to you once i use on of the solution.

    Imported from legacy forums. Posted by Anil_R_B (had 334 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jacques,

    I followed your solution and its working fine.
    But i have one question on this as i faced some problem on this.

    C:\Folder1\Folder2\

    There is only one Folder under Folder1.
    Ther are more number of Folders, sub folders and files under Folder2.

    Now i select Folder1 to zip.
    When the code “FIleSystemItem[] items = source.GetItems(False);” is executed the number of items will be one, and the following Code For Loop will work in no time results are perfect.

    Now i select Folder2 to zip.
    When the code “FIleSystemItem[] items = source.GetItems(False);” is executed the number of items will be some n (>1)

    But when the following For Loop does not end at all. It goes in to infinite loop.

    I did not get why this is happening.

    Currently problem is solved for me as in my requirement there is only one folder always in the selected folder to zip.

    Imported from legacy forums. Posted by Anil_R_B (had 403 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Jacques,

    I am soory to give wrong information.
    Actully the for loop in the sample program is not going to infinite loop, but the time taken is too long.

    When the items are listeed using source.GetItems(False) the no of files in thr items object is 190.

    Is it causing problem?

    Imported from legacy forums. Posted by Anil_R_B (had 264 views)

    Xceed Support
    Member
    Post count: 5658

    Oups.. my bad 🙂

    I forgot something important in your case. You need to use an AutoBatchUpdate object on the ZipArchive object so that the archive is not rebuilt each time you call CopyTo. Here’s an updated code snippet:

    DiskFile zipFile = new DiskFile( @”C:\Test1.zip” );

    // Make sure we use a new and empty archive.
    if( zipFile.Exists )
    zipFile.Delete();

    ZipArchive archive = new ZipArchive( zipFile );

    DiskFolder source = new DiskFolder( @”C:\Folder1\Folder2″ );

    // No need to get recursively since calling CopyTo on a folder will
    // copy the whole structure.
    FileSystemItem[] items = source.GetItems( false );

    // This object will call BeginUpdate on the archive and EndUpdate when it is disposed.
    // This will prevent the archive from begin rebuilt each time an item is copied into the archive.
    using( new AutoBatchUpdate( archive ) )
    {
    foreach( FileSystemItem item in items )
    {
    item.CopyTo( archive, true );
    }
    }

    This should fix the speed problem you are experiencing.

    Have a nice day!

    Imported from legacy forums. Posted by Jacques [Xceed] (had 6556 views)

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