压缩和解压缩文件

Dar*_*cek 5 vb.net zipfile visual-studio-2012

我是使用VS2012的程序员。我想解压缩一个zip文件(使用Winzip,filzip或其他zip压缩例程制作),然后也能够将文件压缩回一个zip文件。

最佳的库是什么,请问如何使用库的示例代码?

编辑

我正在使用VB.net,这是我的代码:

Public Function extractZipArchive() As Boolean
    Dim zipPath As String = "c:\example\start.zip"
    Dim extractPath As String = "c:\example\extract"

    Using archive As ZipArchive = ZipFile.OpenRead(zipPath)
        For Each entry As ZipArchiveEntry In archive.Entries
            If entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) Then
                entry.ExtractToFile(Path.Combine(extractPath, entry.FullName))
            End If
        Next
    End Using
End Function
Run Code Online (Sandbox Code Playgroud)

我需要使用哪些导入语句?目前,我添加了以下内容:

Imports System.IO
Imports System.IO.Compression
Run Code Online (Sandbox Code Playgroud)

我收到错误消息:

未定义类型“ ZipArchive”

如何解决此错误?

ta.*_*.is 3

如果您使用的是 Visual Studio 2012 和 .NET Framework 4.5,您可以使用新的压缩库

    //This stores the path where the file should be unzipped to,
    //including any subfolders that the file was originally in.
    string fileUnzipFullPath;
     
    //This is the full name of the destination file including
    //the path
    string fileUnzipFullName;
     
    //Opens the zip file up to be read
    using (ZipArchive archive = ZipFile.OpenRead(zipName))
    {
        //Loops through each file in the zip file
        foreach (ZipArchiveEntry file in archive.Entries)
        {
            //Outputs relevant file information to the console
            Console.WriteLine("File Name: {0}", file.Name);
            Console.WriteLine("File Size: {0} bytes", file.Length);
            Console.WriteLine("Compression Ratio: {0}", ((double)file.CompressedLength / file.Length).ToString("0.0%"));
     
            //Identifies the destination file name and path
            fileUnzipFullName = Path.Combine(dirToUnzipTo, file.FullName);
     
            //Extracts the files to the output folder in a safer manner
            if (!System.IO.File.Exists(fileUnzipFullName))
            {
                //Calculates what the new full path for the unzipped file should be
                fileUnzipFullPath = Path.GetDirectoryName(fileUnzipFullName);
                            
                //Creates the directory (if it doesn't exist) for the new path
                Directory.CreateDirectory(fileUnzipFullPath);
     
                //Extracts the file to (potentially new) path
                file.ExtractToFile(fileUnzipFullName);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)