Frage

I am using Ionic zip in my WCF service to unzip files uploaded by a 'client'.The files are zipped using Ionic zip.However,there have been instances where the zipped files were 'corrupted'.Since my code scans the entire folder to look for zipped files,exceptions were thrown as it was picking up the same 'corrupted' zipped files everytime.This stopped all the other files from being processed.Here's the code:

foreach (string filePath in Directory.GetFiles(ZippedFilesDestinationFolder))
{
    using (ZipFile zip1 = ZipFile.Read(filePath))
            {
                foreach (ZipEntry e in zip1)
                {
                    e.Extract(unpackdirectory, ExtractExistingFileAction.OverwriteSilently);
                }
            }
}

I want to move the corrupted file to another folder and continue extracting the other zipped files in the folder.How should the code be modified to achieve this?

War es hilfreich?

Lösung

It stopped all other files because the exception was unhandled within the loop, causing the loop to exit. Adding a Try/Catch around the Read of Zipped files will allow files to fail but still allow next file to be processed.

foreach (string filePath in Directory.GetFiles(ZippedFilesDestinationFolder))
{
    try
    {
        using (ZipFile zip1 = ZipFile.Read(filePath))
        {
            foreach (ZipEntry e in zip1)
            {
                e.Extract(unpackdirectory, ExtractExistingFileAction.OverwriteSilently);
            }
        }
    }
    catch(Exception ex) 
    { 
        /* Log ex here */ 
        /* Move corrupt file */
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top