繁体   English   中英

读取 zip 文件条目时出现找不到文件错误

[英]getting file not found error while reading zip file entry

I have a file 1.txt inside zip file 1.zip which content are base64 encoded, now I am trying to read bytes FromBase64String as a zip entry, but getting error file not found. 我需要提取然后阅读吗?

   using (var zipArchive = ZipFile.OpenRead(@"C:\tmp\1.zip"))
            {
                foreach (var entry in zipArchive.Entries.Where(e => !string.IsNullOrEmpty(e.Name)))
                {
                    Console.WriteLine(entry.Name);

                    var bytes = Convert.FromBase64String(File.ReadAllText(entry.FullName));

                }
            }

找不到文件“C:\repos\net452\net452\bin\Debug\1.txt”。

您当前的问题是您正在使用File.ReadAllText ,它从文件系统读取,而不是 zip 文件的内容。

您需要从zip 文件中读取。 您可以通过打开条目的 stream 来执行此操作。 要将其作为文本阅读,您可以使用StreamReader

using (var zipArchive = ZipFile.OpenRead(@"C:\tmp\1.zip"))
{
    foreach (var entry in zipArchive.Entries.Where(e => !string.IsNullOrEmpty(e.Name)))
    {
        Console.WriteLine(entry.Name);

        byte[] bytes = null;
        using (var reader = new StreamReader(entry.Open()))
        {
            bytes = Convert.FromBase64String(reader.ReadToEnd());
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM