2012-03-02 35 views
6

Sử dụng ICSharpCode.SharpZipLib cho C#, làm cách nào tôi có thể xác thực rằng tệp đang được chuyển thực sự là tệp zip hợp lệ (không phải cái gì đã được nhấp chuột phải và đổi tên thành .zip)?ICSharpCode.SharpZipLib xác thực tệp zip

[HttpPost] 
     public ActionResult Upload(HttpPostedFileBase fileData) 
     { 
       if (fileData != null && fileData.ContentLength > 0) 
       { 
        if (Path.GetExtension(fileData.FileName) == ".zip") 
        { 
         var zipFile = Server.MapPath("~/Content/uploads/" + Path.GetFileName(fileData.FileName)); 
         fileData.SaveAs(zipFile); 

         FileStream fs = System.IO.File.OpenRead(zipFile); 
         ZipFile zf = new ZipFile(fs); 

         foreach (ZipEntry zipEntry in zf) 
         { 
          if (zipEntry.Name.EndsWith(".htm") || zipEntry.Name.EndsWith(".html")) 
          { 
           return Json(new { success = true });        
          } 
         } 
         fs.Close(); 
         fs.Dispose(); 
         System.IO.File.Delete(zipFile); 
        } 
        else 
        { 
         var fileName = Server.MapPath("~/Content/uploads/" + Path.GetFileName(fileData.FileName)); 
         fileData.SaveAs(fileName);       
         return Json(new { success = true }); 
        } 
       }      
       return Json(new { success = false }); 

    } 

Trả lời

14

Bạn có thể sử dụng phương thức ZipFile.TestArchive. Sau đây là cách nó được khai báo trong SharpZipLib:

/// <summary> 
/// Test an archive for integrity/validity 
/// </summary> 
/// <param name="testData">Perform low level data Crc check</param> 
/// <returns>true if all tests pass, false otherwise</returns> 
/// <remarks>Testing will terminate on the first error found.</remarks> 
public bool TestArchive(bool testData) 
{ 
    return TestArchive(testData, TestStrategy.FindFirstError, null); 
} 

Cách sử dụng Ví dụ:

ZipFile zipFile = new ZipFile("archive.zip"); 
Console.WriteLine("Archive validation result: {0}", zipFile.TestArchive(true)); 
+1

Tuyệt vời, đây chính xác là những gì tôi đang tìm kiếm. – GoldenUser

+0

Cảm ơn, đó là câu trả lời thực sự rõ ràng và hữu ích! –

+0

điều này có xác minh toàn bộ lưu trữ không? Tôi có một tập tin zip được bảo vệ bằng mật khẩu ~ 1GB mà tôi có thể liệt kê thông qua các mục nhập của nó nhưng khi tôi thực hiện việc trích xuất nó bị lỗi ở ~ 56%. Tôi có thể thử nghiệm nó trước khi bắt đầu giải nén nó không? – Adi

0

Hãy cẩn thận với điều mới. Nó tạo ra một lỗi IOAccess trên tập tin cho tôi khi tôi ngay lập tức cố gắng đổi tên, tôi phải thêm một tuyên bố sử dụng:

public static bool ValidateZipFile(string fileToTest) 
{ 
    bool result; 
    //Added using statement to fix IOAccess Blocking 
    using (ICSharpCode.SharpZipLib.Zip.ZipFile zip = new ICSharpCode.SharpZipLib.Zip.ZipFile(fileToTest)) 
    { 
     result = zip.TestArchive(true, TestStrategy.FindFirstError, null); 
    } 
    return result; 
} 
+0

Nếu tôi sử dụng mã chính xác như thế này trong SharpZipLib 0.86.0, tôi nhận được một '' '' ZipException'''' ("Không thể tìm thấy thư mục trung tâm") được ném vào hàm tạo ZipFile, vì hàm tạo đã cố gắng truy cập vào ZIP tập tin. Tuy nhiên, bạn không thể sử dụng TestArchive mà không cần tạo một cá thể. –

Các vấn đề liên quan