【问题标题】:How can I have files automatically overwrite existing files when unzipping my folder?解压缩文件夹时如何让文件自动覆盖现有文件?
【发布时间】:2021-04-28 00:59:43
【问题描述】:

所以我找到了很多关于这方面的信息,而且有很多不同的方式,这似乎非常令人不知所措,因为我还是新手。所以我的问题是,我如何将此功能合并到我自己的代码中,以便在提取文件时自动覆盖文件而不是给我一个错误?如果不是这样,有人可以将我引向易于理解和教我的教学领域吗?

以下是我当前保存和从内存中提取文件夹的代码。

public static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName)
{

    Assembly assembly = Assembly.GetCallingAssembly();

    using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName))
    using (BinaryReader r = new BinaryReader(s))
    using (FileStream fs = new FileStream(outDirectory + "//" + resourceName, FileMode.OpenOrCreate))
    using (BinaryWriter w = new BinaryWriter(fs))
        w.Write(r.ReadBytes((int)s.Length));

}

这就是我目前正在开展业务的地方。

private void button2_Click(object sender, EventArgs e)
        {

            Extract("nameSpace", @"outDirectory", "internalFilePath", "resourceName");

            string sourceZipFile = @"C:\test.zip";
            string targetFolder = @"C:\";
            ZipFile.ExtractToDirectory(sourceZipFile, targetFolder);


            Process process = new Process();
            ProcessStartInfo p= new ProcessStartInfo();
            p.FileName = @"C:\test.zip";

            if ((System.IO.File.Exists(p.FileName)))
            {
                System.IO.File.Delete(p.FileName);
            }


        }

【问题讨论】:

    标签: c# overwrite


    【解决方案1】:

    使用具有overwriteFiles 参数的正确重载:

    ZipFile.ExtractToDirectory(sourceZipFile, targetFolder, true);
    

    如果您在 Framework

    【讨论】:

    • 我如何自己编写这个逻辑?还是切换到较新的框架?甚至没有意识到有不同的框架具有不同的命令等。不过感谢您的信息!
    • 使用ZipFile.OpenRead()然后循环遍历每个ZipArchiveEntry得到一个带有.Open()的流并检查写入位置的目录是否存在并删除现有文件。或者你可以copy the source code for it
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    • 2010-09-05
    • 1970-01-01
    • 2010-12-14
    • 2019-06-20
    • 1970-01-01
    相关资源
    最近更新 更多