2015-02-24 18 views
0
try 
    { 
    Response.Clear(); 
    Response.BufferOutput = false; 
    string archiveName = String.Format("arch{0}.zip",         DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
    Response.ContentType = "application/zip"; 
    Response.AddHeader("content-disposition", "filename=" + archiveName); 

    using (ZipFile zip = new ZipFile()) 
    {           
    zip.AddFiles(ListOfPastPapers,"PastPapers");      
    zip.Save(Response.OutputStream); 
    Response.Close(); 
    } 
    } 
catch (System.Exception ex1) 
{     
} 

我使用Ionic.Zip dll壓縮圖像。我給列表的字符串類型zip.addfiles()與完整的服務器路徑時,它會拋出一個異常,當特定路徑上的任何圖像未找到。zip.AddFiles()處理不存在的目錄上的文件只是跳過這些文件和zip其他現有

我該如何處理它?

我想要的,如果圖像不存在只是跳過該圖像和壓縮所有其他現有的圖像。

我該怎麼做?

回答

0

File.Exists(string)甚至FileInfo.Exists會幫助你的情況。只需跳到下一個不存在案例的迭代。

0

在添加文件之前,請檢查文件是否存在。

using (ZipFile zip = new ZipFile()) 
{ 

foreach(var name in ListOfPastPapers) 
{ 
    if(System.IO.File.Exists(name)) 
    { 
     zip.AddFiles(filePath,"PastPapers");      
     zip.Save(Response.OutputStream); 
     Response.Close(); 
    } 
    } 
} 
相關問題