Home Forums .NET libraries Xceed Zip & Real-Time Zip for .NET An IO exception occurred while reading from or writing to th

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

    I have a code that adds bunch of files to a zip file using Xceed.Zip managed classes. My code is written in C#.:

    AbstractFile zipFile = new DiskFile (archiveName);
    if (zipFile.Exists)
    zipFile.Delete ();
    zipFile.Create ();

    ZipArchive archive = new Xceed.Zip.ZipArchive (zipFile);
    archive.DefaultCompressionLevel = Xceed.Compression.CompressionLevel.Highest;

    foreach (string fileName in filesToCompress)
    {
    AbstractFile aFile = new DiskFile (fileName);
    aFile.CopyTo (archive, false);
    }

    After succesfully adding several files from filesToCompress collection aFile.CopyTo throws an exception:

    Error Type: ‘Xceed.Zip.ZipIOException’
    Source: ‘Xceed.FileSystem’
    Error: ‘An IO exception occurred while reading from or writing to the zip file.
    Type: Xceed.Zip.ZipArchive
    FullName: \
    Zip file: g:\SEDUMP\Ver003\stress_files.zip’
    Stack Trace:
    at Xceed.FileSystem.FileSystemItem.CopyTo(FileSystemEvents events, Object userData, AbstractFolder destinationFolder, Boolean replaceExistingFiles) +308 (native offset)
    at Xceed.FileSystem.FileSystemItem.CopyTo(AbstractFolder destinationFolder, Boolean replaceExistingFiles) +41 (native offset)
    Type: ‘Xceed.FileSystem.FileSystemIOException’
    Source: ‘Xceed.FileSystem’
    Error: ‘The file item could not be opened for writing.
    Type: Xceed.FileSystem.DiskFile
    FullName: g:\SEDUMP\Ver003\stress_files.zip’
    Stack Trace:
    at Xceed.FileSystem.AbstractFile.OpenWrite(FileSystemEvents events, Object userData, Boolean overwrite, FileShare share) +87 (native offset)
    at Xceed.FileSystem.AbstractFile.OpenWrite(Boolean overwrite) +45 (native offset)
    at Xceed.Zip.ZipStream..ctor(FileSystemEventsSession session, ZipHandler handler, Int32 dummy) +191 (native offset)
    at Xceed.Zip.ZipHandler.OpenWrite(FileSystemEventsSession session) +286 (native offset)
    at Xceed.Zip.ZipHandler.Flush(FileSystemEventsSession session) +237 (native offset)
    Type: ‘System.IO.IOException’
    Source: ‘mscorlib’
    Error: ‘The process cannot access the file “g:\SEDUMP\Ver003\stress_files.zip” because it is being used by another process.’
    Stack Trace:
    at System.IO.__Error.WinIOError(Int32 errorCode, String str) +614 (native offset)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath, Boolean bFromProxy) +888 (native offset)
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) +45 (native offset)
    at Xceed.FileSystem.DiskFile.DoOpenWrite(FileSystemEventsSession session, Boolean overwrite, FileShare share) +105 (native offset)

    It is very unlikely that the file is actually being used by some other process since it’s a new file and this reproes consistently every time I run the code above. I suspected some virus scanners or other software and added a retry logic to the above code with 1 sec pause between retries and it didn’t help.

    I understand that in my approach to zipping files Xceed.Zip opens the archive file for every file I am adding. Is there a way to open archive once for entire session to make sure that no other process can lock this file while I am still using it?

    Thanks,
    -Alex

    Imported from legacy forums. Posted by ognev (had 4863 views)

    User (Old forums)
    Member
    Post count: 23064

    Hello Alex.

    The error you are getting is indeed strange, though I previously saw antivirus software cause similar problems.

    But indeed, you can open the zip archive once, and not only reduce the risk of getting this error, but also <b>greatly</b> improve performance, by doing so:

    (sorry about the poor “code format” output)
    <code>
    archive.BeginUpdate();

    try
    {
    foreach (string fileName in filesToCompress)
    {
    AbstractFile aFile = new DiskFile (fileName);
    aFile.CopyTo (archive, false);
    }
    }
    finally
    {
    archive.EndUpdate();
    }
    </code>

    Those who like the IDisposable scheme can even do:

    <code>
    using( AutoBatchUpdate auto = new AutoBatchUpdate( archive ) )
    {
    foreach (string fileName in filesToCompress)
    {
    AbstractFile aFile = new DiskFile (fileName);
    aFile.CopyTo (archive, false);
    }
    }
    </code>

    Hope this helps.

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

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