压缩和解压缩文件

时间:2012-08-18 05:53:50

标签: vb.net visual-studio-2012 zipfile

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

如何解决此错误?

5 个答案:

答案 0 :(得分:2)

虽然前一段时间没有回答,所以我仍然会将其0.02美元存入其中,以便其他任何人在关键词上点击这个...

VB 2012(.Net 4.5)为System.IO.Compression( System.IO.Compression.FileSystem.dll )添加了新功能,可以执行您想要的操作。我们之前只有过GZip。当然,您仍然可以使用免费的DotNetZipSharpZipLib

ZipFile 类有2个静态方法,可以使简单的压缩/解压缩简单: CreateFromDirectory ExtractToDirectory 。你也可以选择 NoCompression 最快 Optimal

让我了解你的帖子的一件事是档案中的文件(甚至是档案)的概念。使用 ZipArchive ZipArchiveEntry 类,您现在可以

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支持的谈话

答案 1 :(得分:1)

如果您使用的是Visual Studio 2012和.NET Framework 4.5 you can use the new compression library

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

答案 2 :(得分:1)

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

答案 3 :(得分:1)

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

中所述

您可以使用 ZipFile.ExtractToDirectory CreateFromDirectory

这是一个例子:

  

Imports System.IO

     

导入System.IO.Compression

     

模块模块1

     

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

     

结束模块

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

答案 4 :(得分:-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