Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET Unable To Zip An Open Access Database

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

    It’s my understanding that Zip for .NET should be able to zip an open file; however, this doesn’t seem to work in the case of an Access database. This issue is easily reproducible by running the ZipDemo sample application. If I open a database in read-only mode, the ZipDemo is able to zip the file, but if I open it in write-mode, it cannot. I’m positive the database is opened in shared-mode and not exclusive. I am able to copy the database while it is open, but the zip will not work. I am using version 2 of Zip for .NET.

    Is there any way to get this to work, or is this just a limitation of the zip library and/or Access?

    Imported from legacy forums. Posted by Todd (had 3522 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello Todd.

    The CopyTo method opens the source AbstractFile to copy using OpenRead without parameters, which reverts to calling OpenRead( FileShare.Read ).

    You will need to copy the data yourself, like this:

    <code>
    ZipArchive zip = new ZipArchive( new DiskFile( @”d:\dest.zip” ) );

    zip.BeginUpdate();

    try
    {
    DiskFile sourceFile = new DiskFile( @”d:\database.mdb” );
    AbstractFile destFile = zip.GetFile( “database.mdb” );

    // Cannot call OpenWrite on a file that does not exist.
    if( !destFile.Exists )
    destFile.Create();

    using( Stream sourceStream = sourceFile.OpenRead( FileShare.ReadWrite ) )
    {
    using( Stream destStream = destFile.OpenWrite( true ) )
    {
    byte[] buffer = new byte[ 32768 ];
    int read = 0;

    while( ( read = sourceStream.Read( buffer, 0, buffer.Length ) ) > 0 )
    {
    destStream.Write( buffer, 0, read );
    }
    }
    }
    }
    finally
    {
    zip.EndUpdate();
    }
    </code>

    There is a shorter way to achieve the same result, using the StreamFile class:

    <code>
    ZipArchive zip = new ZipArchive( new DiskFile( @”d:\dest.zip” ) );

    zip.BeginUpdate();

    try
    {
    DiskFile sourceFile = new DiskFile( @”d:\database.mdb” );

    using( Stream sourceStream = sourceFile.OpenRead( FileShare.ReadWrite ) )
    {
    StreamFile sourceFileToo = new StreamFile( sourceStream, sourceFile.Name );
    sourceFileToo.CopyTo( zip, true );
    }
    }
    finally
    {
    zip.EndUpdate();
    }
    </code>

    BTW, I didn’t test this code, but only compiled it… I hope I didn’t make any mistake! 🙂

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

    User (Old forums)
    Member
    Post count: 23064

    I used the second (shorter) approach and it works. I can’t say for sure that your syntax works as I converted it to VB.NET, but the basic logic does work.

    Thank you very much.

    Imported from legacy forums. Posted by Todd (had 261 views)

    User (Old forums)
    Member
    Post count: 23064

    Hi Todd,

    Please can you put the vb code on the forum

    Thanks

    Imported from legacy forums. Posted by sw_tst (had 4551 views)

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