5

我是一名使用 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

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

Imports System.IO
Imports System.IO.Compression

我收到错误消息:

类型“ZipArchive”未定义

我该如何解决这个错误?

4

5 回答 5

3

没有回答,虽然不久前,所以我仍然会把我的 0.02 美元放在那里,给任何在关键字上点击它的人......

VB 2012 (.Net 4.5) 为 System.IO.Compression ( System.IO.Compression.FileSystem.dll ) 添加了新功能,可以满足您的需求。我们以前只有 GZip。当然,您仍然可以使用免费的DotNetZipSharpZipLib

ZipFile类有 2 个静态方法可以让简单的压缩/解压缩变得简单:CreateFromDirectoryExtractToDirectory。Yo 也有NoCompressionFastestOptimal的压缩选项。

关于你的帖子让我印象深刻的一件事是档案中的文件(甚至档案)的概念。使用ZipArchiveZipArchiveEntry类,您现在可以

压缩存档:

Using zippedFile as ZipArchive = ZipFile.Open("foo.zip", ZipArchiveMode.Read)
    For Each ntry as ZipArchiveEntry In zippedFile.Entries
        Debug.Writeline("entry " & ntry.FullName & " is only " & ntry.CompressedLength.ToString)
    Next
End Using

您的问题还与添加到现有档案有关。您现在可以这样做:

Using zippedFile as ZipArchive = ZipFile.Open("foo.zip", ZipArchiveMode.Update)
    zippedFile.createEntry("bar.txt", CompressionLevel.Fastest)
    ' likewise you can get an entry already in there...
    Dim ntry As ZipArchiveEntry = zippedFile.GetEntry("wtf.doc")
    ' even delete an entry without need to decompress & compress again!
    ntry.Delete() ' !
End Using

同样,这是不久前的事了,但我们中的很多人仍在使用 2012,并且由于此更改在未来的版本中不会发生任何变化,因此如果有人点击关键字/标签搜索,它应该仍然有助于向前推进。 .

...我们甚至没有谈论UTF-8 支持!

于 2014-04-29T16:34:05.553 回答
2

如果您使用 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);
        }
    }
}
于 2012-08-18T05:57:16.073 回答
2

您可能没有引用System.IO.Compression. 选中该程序集引用的框,它应该消除错误。

于 2014-08-07T22:11:36.383 回答
1

https://msdn.microsoft.com/en-us/library/system.io.compression.zipfile(v=vs.110).aspx中所述

您可以使用ZipFile.ExtractToDirectoryCreateFromDirectory

这是一个例子:

导入系统.IO

导入 System.IO.Compression

模块模块1

子主()

   Dim startPath As String = "c:\example\start"
   Dim zipPath As String = "c:\example\result.zip"
   Dim extractPath As String = "c:\example\extract"

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

结束子

端模块

确保您已引用System.IO.Compression.FileSystem以使用此功能。

于 2017-12-04T10:24:37.040 回答
-1
    Dim fileStream As Stream = File.OpenRead("your file path")

    Using zipToOpen As FileStream = New FileStream(".......\My.zip", FileMode.CreateNew)
        Using archive As ZipArchive = New ZipArchive(zipToOpen, ZipArchiveMode.Create)
            Dim readmeEntry As ZipArchiveEntry = archive.CreateEntry("your file path")
            fileStream.CopyTo(readmeEntry.Open())
        End Using
    End Using
于 2018-02-01T14:24:08.207 回答