Zip and Unzip file

穿精又带淫゛_ 提交于 2019-12-20 04:35:08

问题


I'm working on a project that needed to unzip file and store in a specific folder. But the problem is, I don't know how to do it. It is my first time to work on this kind of project.

I'm using visual studio 2010. And I'm not gonna use any third party applications.

Can anyone suggest how could I do this?

i've tried this code but the ZIPFILE is not recognizable. It has a red line on it.

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);
    }
}

}


回答1:


Archiving has now been included by Microsoft in .NET framework by using the ZipFile namespace.

To make it very short, to zip a directory, you can use the following code:

ZipFile.CreateFromDirectory(sourceFolder, outputFile);



回答2:


I've use this code to solve my problem...

I also do this one in my reference

Then add this code.

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);
        }
    }

and in button event

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

I'm telling you, it do work.




回答3:


System.IO.Compression.FileSystem.dll

download the above dll file. Go to solution explorer -> Right click on References, Click on Add References. Select "Windows" in window Select "Extension" Select "Browse" and go to the folder where you unzip the System.IO.Compression.FileSystem.dll file.

click on "Ok" and run the app again. :-)




回答4:


The following link shows two example for zip and unzip to the files in C#. You can use this sample.

Sample(using 7-zip):

var tmp = new SevenZipCompressor();
tmp.ScanOnlyWritable = true;
tmp.CompressFilesEncrypted(outputFilePath, password, filePaths);

Sample(using ZipArchive):

ZipArchive zip = ZipFile.Open(filePath, ZipArchiveMode.Create);
zip.CreateEntryFromFile(file, Path.GetFileName(file), CompressionLevel.Optimal);
zip.Dispose();

For more information:

http://csharpexamples.com/zip-and-unzip-files-programmatically-in-c/




回答5:


using System.IO.Compression;

include the above assembly in your code. It contains definition for the below 2 methods

ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath);



来源:https://stackoverflow.com/questions/23906185/zip-and-unzip-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!