繁体   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