压缩和解压文件

7

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

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

Imports System.IO
Imports System.IO.Compression

我遇到了一个错误:

类型 'ZipArchive' 未定义

我该如何解决这个错误?

它是内置于框架中的:http://www.codeproject.com/Articles/381661/Creating-Zip-Files-Easily-in-NET-4-5 - ta.speot.is
5个回答

3
如下所述https://msdn.microsoft.com/zh-cn/library/system.io.compression.zipfile(v=vs.110).aspx 您可以使用 ZipFile.ExtractToDirectoryCreateFromDirectory 这是示例:

Imports System.IO

Imports System.IO.Compression

Module Module1

Sub Main()

   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)

End Sub

End Module

请确保引用了 System.IO.Compression.FileSystem 以使用此功能。

这是我在2023年使用的答案。原始答案使用了自此问题首次发布以来已更改的Microsoft对象和方法名称。对于我目前使用的Framework 4.7.2,.ExtractToDirectory方法是可用且有效的。Samuel Surya提到的最后一点非常重要:确保在项目引用中启用了Compression.FileSystem的dll。 - undefined

3

虽然有一段时间没有回答了,但我仍然想为任何使用关键字搜索到这里的人提供我的意见...

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

ZipFile类有两个静态方法,使得简单的压缩/解压变得非常简单: CreateFromDirectoryExtractToDirectory。您还可以选择不压缩、最快速度和最优压缩。

你发帖中让我印象深刻的一件事是文件(甚至存档文件)中包含的文件。现在,通过ZipArchiveZipArchiveEntry类,您可以...

ZipArchive:

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支持!


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

谢谢。实际上我正在使用 VB.net(抱歉我应该说明一下)。你能看一下我编辑后的帖子吗? - Darryl Janecek
还有,我可以请你帮忙编写代码来创建一个zip文件并将文件添加到其中吗? - Darryl Janecek
你可以通过代码转换器运行他的代码,它会为你完成大部分工作(虽然它们不总是100%准确,但它们会帮助你完成大部分工作)。我在将博客文章片段转换时使用这个:http://converter.telerik.com/ - b.pell

2

你可能没有引用System.IO.Compression。勾选该程序集引用的框即可消除错误。


-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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接