Zip和解压缩文件

问题描述 投票:1回答:5

我正在研究一个需要解压缩文件并将其存储在特定文件夹中的项目。但是问题是,我不知道该怎么做。这是我第一次从事这种项目。

我正在使用Visual Studio2010。并且我不会使用任何第三方应用程序。

谁能建议我该怎么做?

我已经尝试过此代码,但是ZIPFILE无法识别。它上面有一条红线。

using System;
using System.IO;
using System.IO.Compression;

namespace UnzipFile
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnZip_Click(object sender, RoutedEventArgs e)
    {
        string startPath = @"c:\example\start";
        string zipPath = @"c:\example\result.zip";
        string extractPath = @"c:\example\extract";

        ZipFile.CreateFromDirectory(startPath, zipPath);

        ZipFile.ExtractToDirectory(zipPath, extractPath);
    }
}

}

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS93R0VvRS5wbmcifQ==” alt =“在此处输入图像描述”>

c# unzip
5个回答
7
投票

Microsoft现在已通过使用ZipFile命名空间将存档包括在.NET框架中。

为了简短起见,要压缩目录,可以使用以下代码:

ZipFile.CreateFromDirectory(sourceFolder, outputFile);

1
投票
I've use this code to solve my problem...

我也在我的参考资料中也做了此]

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9Ib0NnQi5wbmcifQ==” alt =“在此处输入图像描述”>

然后添加此代码。

public static void UnZip(string zipFile, string folderPath)
    {
        if (!File.Exists(zipFile))
            throw new FileNotFoundException();

        if (!Directory.Exists(folderPath))
            Directory.CreateDirectory(folderPath);

        Shell32.Shell objShell = new Shell32.Shell();
        Shell32.Folder destinationFolder = objShell.NameSpace(folderPath);
        Shell32.Folder sourceFile = objShell.NameSpace(zipFile);

        foreach (var file in sourceFile.Items())
        {
            destinationFolder.CopyHere(file, 4 | 16);
        }
    }

和按钮事件

private void btnUnzip_Click(object sender, RoutedEventArgs e)
    {
        UnZip(@"E:\Libraries\Pictures\EWB FileDownloader.zip", @"E:\Libraries\Pictures\sample");
    }

我告诉你,它确实有效。


1
投票

System.IO.Compression.FileSystem.dll


0
投票

以下链接显示了两个示例,分别将zip和zip解压缩到C#中的文件。您可以使用此示例。


0
投票

使用System.IO.Compression;

© www.soinside.com 2019 - 2024. All rights reserved.