Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Strore relative path information within the zip archive

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

    Hello,

    I have the request to store relative path structures within the archive/zip file.

    I can give you an example for this. Assume we want to store a complete automation project which is located at D:\Common\My Projects\Project_01. The project itself has also a file structure like for example:
    D:\Common\My Projects\Project_01\db
    D:\Common\My Projects\Project_01\devices
    D:\Common\My Projects\Project_01\communication
    and so on.

    So my question is whether it’s possible to create an archive from Project_01 which only have stored the relative path information beginning from directory Project_01 which is:
    Project_01\db
    Project_01\devices
    Project_01\communication

    That would enable us to archive the whole project inlucding its relative path structure and to restore this project afterwards on any location. The restored project itself would have the same path structure and so it would be executable within our automation environment.

    Thanks in advance for your support,
    Wolfgang

    Imported from legacy forums. Posted by wschreck (had 8030 views)

    Xceed Support
    Member
    Post count: 5658

    Hi wschreck,

    Xceed Zip always keep the relative path of each items in the archive. Here’s how you can achieve what you need:

    // Get an instance on the .zip file itself.
    DiskFile zipFile = new DiskFile( @”D:\MyZip.zip” );

    // We want a new archive so we delete the .zip file if already existing.
    if( zipFile.Exists )
    zipFile.Delete();

    ZipArchive archive = new ZipArchive( zipFile );

    // Get a reference on our source folder
    DiskFolder source = new DiskFolder( @”D:\Common\My Projects\Project_01″ );

    // Copy the folder and it’s content to the zip archive replacing any existing items.
    source.CopyTo( archive, true );

    The final zip file will have all items relative to Project_01.

    Hope this help!

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

    User (Old forums)
    Member
    Post count: 23064

    Jacques, I have a similar problem. I am trying to zip a folder, but only the files that have changed after a certain date. The code below does not store the relative path of each file. What could be wrong? Thanks.

    –DT

    Dim datTmp As Date = CDate(strLastSync)

    Xceed.Zip.Licenser.LicenseKey = gstrZipLicense

    Dim objSourceFolder As New DiskFolder(strPath)
    Dim objZipFile As New DiskFile(strZipFilePath)
    Dim objZip As ZipArchive = New ZipArchive(objZipFile)

    Dim objFiles() As AbstractFile
    Dim objFile As AbstractFile

    ‘– get files changed after specified date
    objFiles = objSourceFolder.GetFiles(True, New DateTimeFilter(New DateTime(datTmp.Year, datTmp.Month, datTmp.Day), DateTime.MaxValue, DateTimeFilter.ApplicableDateTime.LastWrite))

    objZip.BeginUpdate()

    ‘– loop through files
    For Each objFile In objFiles

    Try

    ‘– add file to archive
    objFile.CopyTo(objZip, False)

    Catch ex2 As Exception

    glngSkippedFiles += 1

    End Try

    Application.DoEvents()

    Next

    objZip.EndUpdate()

    Imported from legacy forums. Posted by Darius (had 706 views)

    User (Old forums)
    Member
    Post count: 23064

    Then your destination should not be “objZip” (a ZipArchive, or the root ZippedFolder), but a ZippedFolder who’s name is strPath.Substring(2):

    Dim objDest As ZippedFolder = objZip.GetFolder( strPath.Substring(2) )

    But since you are calling “GetFiles” recursively on your source folder, you’re handling a flat view of the files to zip. I suggest you check the “CopyFilesTo” method, which would recreate the subfolders into the destination too. You could handle “skip” counts via the “ItemException” event of a “FileSystemEvents” object you pass to the “CopyFilesTo” method.

    Imported from legacy forums. Posted by Martin (had 939 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks Martin. I changed my code. See below. One more question: what if I wanted to zip files changed after a certain date and skip certain types of files? Can I combine filters? For instance, I want to zip all files changed after 5/1/06 and skip if I encounter “catalog.wci”.

    –DT

    Dim datTmp As Date = CDate(strLastSync)

    Xceed.Zip.Licenser.LicenseKey = gstrZipLicense

    Dim objSourceFolder As New DiskFolder(strPath)
    Dim objZipFile As New DiskFile(strZipFilePath)
    Dim objZip As ZipArchive = New ZipArchive(objZipFile)

    objSourceFolder.CopyFilesTo(objZip, True, True, New DateTimeFilter(New DateTime(datTmp.Year, datTmp.Month, datTmp.Day), DateTime.MaxValue, DateTimeFilter.ApplicableDateTime.LastWrite))

    Imported from legacy forums. Posted by Darius (had 404 views)

    User (Old forums)
    Member
    Post count: 23064

    Sure. Pass the following filter:

    Dim filter As Filter = New AndFilter( _
    New DateTimeFilter(New DateTime(datTmp.Year, datTmp.Month, datTmp.Day), DateTime.MaxValue, DateTimeFilter.ApplicableDateTime.LastWrite),
    New NotFilter(“catalog.wci”) )

    objSourceFolder.CopyFilesTo(objZip, True, True, filter)

    You could have passed filters like this too:

    objSourceFolder.CopyFilesTo( _
    objZip, True, True, _
    New DateTimeFilter(New DateTime(datTmp.Year, datTmp.Month, datTmp.Day), DateTime.MaxValue, DateTimeFilter.ApplicableDateTime.LastWrite), _
    New NotFilter(“catalog.wci”))

    This is the same since when you pass more than one filter, an AndFilter is created around them.

    Imported from legacy forums. Posted by Martin (had 877 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks. You rock!

    –DT

    Imported from legacy forums. Posted by Darius (had 426 views)

    User (Old forums)
    Member
    Post count: 23064

    One more. I’d like to know my compression ratio. I can check the size of the zip file after it’s created, but what’s the best way to find out the size of all files that were zipped? Do I add their sizes up in the event handler or is there a better way? Thanks.

    –DT

    Imported from legacy forums. Posted by Spoko (had 558 views)

    User (Old forums)
    Member
    Post count: 23064

    If you want to display the compresson ratio during of right after a zipping operation, I’d go for the ByteProgression, specifically the <a href=”http://doc.xceedsoft.com/products/FileSystemCore/ref/xceed.filesystem.byteprogressioneventargsmembers.html”>ByteProgressionEventArgs</a>’s AllFilesBytes member.

    If you want to display the compression ratio of random zip files, get the full list of ZippedFile by calling GetFiles( true ) on the ZipArchive, then sum up both the Size and CompressedSize properties of each ZippedFile.

    Imported from legacy forums. Posted by Martin (had 1156 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Martin,
    thank you very much. This was great helpful to me too.

    Imported from legacy forums. Posted by Sri (had 7586 views)

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