Passer au contenu du pied de page
UTILISER IRONZIP

Comment Extraire Des Fichiers Zip en VB .NET

Zip files are a popular way to compress and package multiple files into a single archive. Working with compressed files, such as ZIP archives, is a common task in software development. In VB.NET, developers often need a reliable and efficient way to extract data from ZIP files. One powerful library that simplifies this process is IronZIP. In this article, we'll explore using IronZIP to extract ZIP files in VB.NET.

How to Extract Zip Files in VB.NET

  1. Install IronZIP Library.
  2. Use IronZipArchive.ExtractArchiveToDirectory() method to extract ZIP files.
  3. Use IronZipArchive.ExtractArchiveToDirectory() method to extract password-protected ZIP files.
  4. Use the GetArchiveEntryNames() method to get all the entries of the ZIP file.

What is IronZIP?

IronZIP, a collection of .NET software components for various development needs. IronZIP stands out for its ease of use, performance, and support for a wide range of compression and decompression features.

Use Cases of IronZIP

Some use cases of IronZIP are:

  1. File compression: You can use IronZIP to reduce the size of your files and save disk space or bandwidth.
  2. File distribution: You can use IronZIP to bundle multiple files into a single archive, making sharing or transferring them easier.
  3. File encryption: You can use IronZIP to protect your files with passwords and permissions, and prevent unauthorized access or modification.
  4. File extraction: You can use IronZIP to open and read ZIP files from various sources, such as local files, streams, or URLs, and extract the contents or metadata.
  5. File management: You can use IronZIP to manipulate ZIP files, such as adding, deleting, or merging entries, changing compression formats, or validating the integrity of the archive.

Getting Started with IronZIP

To begin using IronZIP in your VB.NET project, we'll need to install the IronZIP library. We can easily do this by using NuGet Package Manager Console or Visual Studio's NuGet Package Manager.

Install IronZIP NuGet Package

Write the following command in the NuGet Package Manager Console:

Install-Package IronZip

Press Enter to execute the command. NuGet will download and install the IronZIP package along with its dependencies. You'll see output in the console indicating the progress of the installation.

VB.NET Extract Zip file

Now, let's explore a simple way to extract ZIP files using IronZIP:

Imports IronZip

Module Program
    Sub Main(args As String())
        ' Set paths for the ZIP file and the extraction directory
        Dim extractPath As String = "D:\ExtractedFiles"
        Dim zipPath As String = "D:\Docs.zip"

        ' Extract the ZIP file to the specified directory
        IronZipArchive.ExtractArchiveToDirectory(zipPath, extractPath)
    End Sub
End Module
Imports IronZip

Module Program
    Sub Main(args As String())
        ' Set paths for the ZIP file and the extraction directory
        Dim extractPath As String = "D:\ExtractedFiles"
        Dim zipPath As String = "D:\Docs.zip"

        ' Extract the ZIP file to the specified directory
        IronZipArchive.ExtractArchiveToDirectory(zipPath, extractPath)
    End Sub
End Module
VB .NET

We have used the ExtractArchiveToDirectory method of the IronZipArchive class, which extracts all the entries from a ZIP archive to a specified directory. It passes two arguments to the method: the path of the ZIP file (“D:\Docs.zip”) and the path of the destination directory (“D:\ExtractedFiles”). If different directories are not specified, it will extract files into the default program folder.

Output

How to Extract Zip Files in VB .NET: Figure 1 - The output of the extracted files in the default folder

Similarly, we can also extract Tar, GZip, and BZip2 files.

Extract all the Files from Password-Protected ZIP Archive

To extract all files from a password-protected ZIP archive using IronZIP in VB.NET, we can use the following code snippet:

Imports IronZip

Module Program
    Sub Main(args As String())
        ' Extract password-protected ZIP archive using a password
        IronZipArchive.ExtractArchiveToDirectory("PasswordProtectedZip.zip", "extractedDocs", "myPassw0rd")
    End Sub
End Module
Imports IronZip

Module Program
    Sub Main(args As String())
        ' Extract password-protected ZIP archive using a password
        IronZipArchive.ExtractArchiveToDirectory("PasswordProtectedZip.zip", "extractedDocs", "myPassw0rd")
    End Sub
End Module
VB .NET

The above VB.NET code uses the IronZIP library to extract items of a password-protected ZIP archive. It calls the ExtractArchiveToDirectory method of the IronZipArchive class, which extracts all the entries from a ZIP archive to a specified directory. It passes three arguments to the method: the full path of the ZIP file (“PasswordProtectedZip.zip”), the path of the destination directory (“extractedDocs”), and the password of the ZIP file (“myPassw0rd”).

IronZIP supports AES-128, AES-256, and traditional encryption algorithms. If the password is incorrect or the ZIP file is not encrypted, the method will throw an exception. The extracted files are:

How to Extract Zip Files in VB .NET: Figure 2 - The output of the password-protected extracted files

View Entries of ZIP file

We can view the entries of a compressed file easily by using the IronZIP. The following code opens the ZIP file and displays its entries on the console:

Imports IronZip

Sub Main(args As String())
    ' Specify the ZIP file path
    Dim startPath As String = "D:\Docs.zip"

    ' Open the ZIP archive and list entry names
    Using archive As New IronZipArchive(startPath)
        Dim names As List(Of String) = archive.GetArchiveEntryNames()

        ' Print each entry name to the console
        For Each name As String In names
            Console.WriteLine(name)
        Next name
    End Using
End Sub
Imports IronZip

Sub Main(args As String())
    ' Specify the ZIP file path
    Dim startPath As String = "D:\Docs.zip"

    ' Open the ZIP archive and list entry names
    Using archive As New IronZipArchive(startPath)
        Dim names As List(Of String) = archive.GetArchiveEntryNames()

        ' Print each entry name to the console
        For Each name As String In names
            Console.WriteLine(name)
        Next name
    End Using
End Sub
VB .NET

The above Visual Basic .NET code snippet uses the IronZIP library to extract the names of the entries in a ZIP archive. The code does the following:

  • It creates a new instance of the IronZipArchive class, passing the path of the ZIP file (“D:\Docs.zip”) as a parameter. This opens the ZIP file and loads its contents into memory. The instance is assigned to a variable named archive and is wrapped in a Using statement, which ensures that the ZIP file is closed and disposed of when the code block ends.

  • It calls the GetArchiveEntryNames method of the archive object, which returns a list of strings containing the names of all the entries in the ZIP archive. The list is assigned to a variable named names.

  • It iterates over the names list using a For Each loop and prints each name to the console using the Console.WriteLine method. This displays the names of the files and folders in the ZIP archive.

How to Extract Zip Files in VB .NET: Figure 3 - Output for the previous code example

Conclusion

In conclusion, IronZIP emerges as a powerful tool for handling ZIP archives in VB.NET, offering a rich set of features for compression, extraction, and manipulation. Its versatility extends beyond the basics, allowing developers to seamlessly integrate functionalities like password protection, encryption, and efficient file management. By exploring the examples provided, you can unlock the full potential of IronZIP in your VB.NET projects.

As demonstrated, the library simplifies the extraction process, enabling developers to effortlessly handle password-protected ZIP archives, view file entries, and perform various operations with ease. Moreover, IronZIP's compatibility with a range of compression formats and support across multiple platforms makes it a valuable asset for developers working in diverse environments. IronZIP provides a free trial option that aligns perfectly with their requirements.

Questions Fréquemment Posées

Comment puis-je extraire des fichiers ZIP en VB.NET ?

Vous pouvez utiliser la méthode ExtractArchiveToDirectory() d'IronZIP pour extraire des fichiers ZIP en VB.NET. Cette méthode vous permet de spécifier le chemin du fichier ZIP et le répertoire de destination où les fichiers seront extraits.

IronZIP peut-il gérer les fichiers ZIP protégés par mot de passe ?

Oui, IronZIP peut gérer les fichiers ZIP protégés par mot de passe. Vous pouvez utiliser la méthode ExtractArchiveToDirectory() et fournir un paramètre de mot de passe pour extraire ces fichiers avec succès.

Quelle méthode puis-je utiliser pour lister les entrées dans un fichier ZIP avec VB.NET ?

Vous pouvez utiliser la méthode GetArchiveEntryNames() d'IronZIP pour lister toutes les entrées dans un fichier ZIP. Cette méthode renvoie une liste de noms d'entrées, que vous pouvez imprimer ou enregistrer pour référence.

Quels sont les avantages de l'utilisation de IronZIP pour la gestion des fichiers ZIP ?

IronZIP offre une facilité d'utilisation, des performances élevées et des fonctionnalités étendues pour la compression et la décompression. Il prend en charge la compression de fichiers pour économiser de l'espace, la distribution en regroupant des fichiers, le chiffrement pour la sécurité et les opérations de gestion de fichiers comme l'ajout ou la suppression d'entrées dans les fichiers ZIP.

IronZIP prend-il en charge différents algorithmes de chiffrement ?

Oui, IronZIP prend en charge divers algorithmes de chiffrement, y compris AES-128, AES-256 et des algorithmes de chiffrement traditionnels, assurant une gestion sécurisée des fichiers dans les archives ZIP.

Une version d'essai de IronZIP est-elle disponible pour les développeurs ?

Oui, IronZIP propose une version d'essai gratuite, permettant aux développeurs d'explorer ses fonctionnalités et capacités avant de prendre une décision d'achat.

IronZIP peut-il gérer différents formats de compression ?

Oui, IronZIP prend en charge plusieurs formats de compression comme ZIP, Tar, GZip et BZip2, offrant la flexibilité nécessaire pour répondre à divers besoins de gestion de fichiers.

Comment puis-je installer IronZIP dans un projet VB.NET ?

Vous pouvez installer IronZIP dans votre projet VB.NET en utilisant la console du gestionnaire de packages NuGet avec la commande : Install-Package IronZIP.

IronZIP peut-il extraire des fichiers de différentes sources en VB.NET ?

Oui, IronZIP peut extraire des fichiers de diverses sources, y compris des fichiers locaux, des flux ou des URL, offrant des capacités d'extraction polyvalentes dans les projets VB.NET.

Curtis Chau
Rédacteur technique

Curtis Chau détient un baccalauréat en informatique (Université de Carleton) et se spécialise dans le développement front-end avec expertise en Node.js, TypeScript, JavaScript et React. Passionné par la création d'interfaces utilisateur intuitives et esthétiquement plaisantes, Curtis aime travailler avec des frameworks modernes ...

Lire la suite