将带有子目录的文件解压缩到根文件夹

时间:2018-12-08 05:21:14

标签: c# .net zip

我需要一些帮助。

我现在有此代码。这段代码有效,但还不够。

我的代码;

DirectoryInfo dirFile = new DirectoryInfo(currentDir);
FileInfo[] infoFile = dirFile.GetFiles("*.zip", SearchOption.AllDirectories);

foreach (FileInfo currentFile in infoFile)
{
    using (ZipFile zipFile = ZipFile.Read(currentFile.FullName))
    {
        zipFile.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(unZipFiles_ExtractProgressChanged);
        foreach (ZipEntry currentZip in zipFile)
        { currentZip.Extract(currentFile.DirectoryName, ExtractExistingFileAction.OverwriteSilently); }
    }
    currentCount = increaseCount + 1; increaseCount = currentCount;
    if (downloadType == 1) { bar2SetProgress((ulong)currentCount, (ulong)totalCount); }
    lblFileName.Text = currentFile.Name;
}

我想从所有子目录的_ZipFiles文件夹中提取所有zip文件到Application.StartupPath文件夹。

这里是一个例子;

我有一个zip文件夹。名称:_ZipFolder

解压缩之前;

Application.StartupPath\_ZipFiles\startProgram.zip
Application.StartupPath\_ZipFiles\updateProgram.zip
Application.StartupPath\_ZipFiles\Pack\testDownload.zip
Application.StartupPath\_ZipFiles\Pack\Version\repo2.zip
Application.StartupPath\_ZipFiles\Pack\Version\newClass.zip
Application.StartupPath\_ZipFiles\Ack\Library\argSetup.zip
Application.StartupPath\_ZipFiles\Ack\learnMachine.zip
Application.StartupPath\_ZipFiles\Code\zipVersion4.zip

解压缩后(我确实要提取);

Application.StartupPath\startProgram.exe
Application.StartupPath\updateProgram.exe
Application.StartupPath\Pack\testDownload.exe
Application.StartupPath\Pack\Version\repo2.cs
Application.StartupPath\Pack\Version\newClass.cs
Application.StartupPath\Ack\Library\argSetup.exe
Application.StartupPath\Ack\learnMachine.pdf
Application.StartupPath\Code\zipVersion4.exe

所有文件都需要从_ZipFiles文件夹及其子目录移至Application.StartupPath。

如何制作?请帮助我。

我希望你理解我想要的。对不起,我的英语不好。

2 个答案:

答案 0 :(得分:1)

提取时从当前文件目录名称中删除zip文件夹名称

根据当前示例,您拥有_ZipFiles文件夹

DirectoryInfo dirFile = new DirectoryInfo(currentDir);
FileInfo[] infoFile = dirFile.GetFiles("*.zip", SearchOption.AllDirectories);

var zipFolderName = @"\_ZipFiles";

foreach (FileInfo currentFile in infoFile) {
    using (ZipFile zipFile = ZipFile.Read(currentFile.FullName)) {
        zipFile.ExtractProgress += new EventHandler<ExtractProgressEventArgs>(unZipFiles_ExtractProgressChanged);
        var destination = currentFile.DirectoryName.Replace(zipFolderName, "");
        foreach (ZipEntry currentZip in zipFile) { 
            currentZip.Extract(destination, ExtractExistingFileAction.OverwriteSilently); 
        }
    }
    currentCount = increaseCount + 1; increaseCount = currentCount;
    if (downloadType == 1) { bar2SetProgress((ulong)currentCount, (ulong)totalCount); }
    lblFileName.Text = currentFile.Name;
}

答案 1 :(得分:0)

如果我对您的理解正确,则希望将所有文件提取到Application.StartupPath目录,而不是子文件夹中。

尝试更改:

currentZip.Extract(currentFile.DirectoryName, ExtractExistingFileAction.OverwriteSilently);

currentZip.Extract(Application.StartupPath, ExtractExistingFileAction.OverwriteSilently);

如果Application.StartupPath不适合,则可以使用AppDomain.CurrentDomain.BaseDirectory