我必须在 zip 文件中取一个文件的目录 [英] I have to take the directory of a file in zip file

查看:21
本文介绍了我必须在 zip 文件中取一个文件的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能帮我解决我的问题吗?我必须在 zip 文件中获取文件目录,以便我可以计算其 MD5 哈希值(无需解压缩).我正在使用 DotNetZip 库,但我找不到问题的解决方案.我将向您展示我的尝试,并希望您能尽快提供帮助.谢谢!

Can someone helps me out with my problem. I have to take a file's directory in zip file so i can calculate its MD5 hash (without unzip it). I am using DotNetZip Library but i can't find the solution of the problem. I'll show you what i've tryed and hope you will help as fast as possible. Thanks!

if (ofd.ShowDialog() == DialogResult.OK) 
{
    using (ZipFile zip = ZipFile.Read(ofd.FileName))
    {
        foreach (ZipEntry f in zip)
        {
            GetMD5HashFromFile(ofd.FileName+"\\"+f.FileName);
        }
    }
}

推荐答案

问题是您没有提取 Zip 条目,它仍在存档中.这就是它找不到路径的原因.我建议使用流并对其进行计算,而不进行提取.请注意,MD5 不是碰撞安全的.

The problem is that you do not extract the Zip entry, it is still in the archive. That is why it does not find the path. I recommend to use the stream and calculate on that, without extracting. Be aware of that MD5 is no collision safe.

您必须在您的项目中引用System.IO.Compression.FileSystem.dll.完整的控制台应用程序:

You have to reference in your project the System.IO.Compression.FileSystem.dll. Full working console application:

public class Program
{

    static void Main(string[] args)
    {

        var z = ZipFile.OpenRead(@"C:\directory\anyfile.zip");
        foreach (ZipArchiveEntry f in z.Entries)
        {
           var yourhash = GetMD5HashFromFile(f.Open());
        }

    }

    public static string GetMD5HashFromFile(Stream stream)
    {
        using (var md5 = new MD5CryptoServiceProvider())
        {
            var buffer = md5.ComputeHash(stream);
            var sb = new StringBuilder();

            for (int i = 0; i < buffer.Length; i++)
            {
                sb.Append(buffer[i].ToString("x2"));
            }
            return sb.ToString();
        }
    }

这篇关于我必须在 zip 文件中取一个文件的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆