簡體   English   中英

C# 如何檢查下載的 zip 存檔是否損壞(拋出“找不到中央目錄記錄的結尾”異常)

[英]C# How to check if downloaded zip archive is corrupted ('End of Central Directory record could not be found' exception thrown)

我正在使用以下代碼從遠程共享中打開、解壓縮和讀取文件:

using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read, 2 << 18))
using (ZipArchive za = new ZipArchive(fs))
{
    foreach (ZipArchiveEntry zae in za.Entries)
    using (StreamReader sr = new StreamReader(zae.Open(), Encoding.GetEncoding(1251), true, 2 << 18))
    {
        while (!sr.EndOfStream)
        {
            // reading logic
        }
    }
}

如何檢查下載的存檔是否損壞?

解決方案:

bool ValidateZip(FileStream fs)
{
    using (BinaryReader br = new BinaryReader(fs))
    {
        br.BaseStream.Seek(-22, SeekOrigin.End);
        return br.ReadUInt32() == 0x06054b50;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM