문제

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?

도움이 되었습니까?

해결책

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 */
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top