Home
Map
ZipFile ExampleUse the ZipFile class to compress a directory and expand the compressed file.
VB.NET
This page was last reviewed on Jul 13, 2021.
ZipFile. This can compress an entire directory. It then can expand the compressed file into a new directory. We use the CreateFromDirectory and ExtractToDirectory methods.
Simpler solution. ZipFile is simpler than developing custom methods. The code is heavily-tested, as it is part of .NET, so less development burden is created.
File
Example. To use this program, please create a folder called "source" in the same directory as the program executable. You can add files to it.
Also Make sure that a "destination" folder does not yet exist. If it exists, you can delete it in Windows Explorer.
Detail Upon execution, the program will take all the files in the source directory and add them to an archive called "destination.zip."
Then It will expand "destination.zip" into a folder called "destination." We specify CompressionLevel.Optimal.
Imports System.IO.Compression Module Module1 Sub Main() ' Create ZIP from "source" directory (in program folder). ZipFile.CreateFromDirectory("source", "destination.zip", CompressionLevel.Optimal, False) ' Extract ZIP to "destination" folder. ZipFile.ExtractToDirectory("destination.zip", "destination") End Sub End Module
Notes, add reference. You may need to add a reference in Visual Studio. Go to Add Reference and select System.IO.Compression.FileSystem. ZipFile was not in the older versions of .NET.
Compression levels. The Optimal compression level helps reduce the size of the archive. But Optimal will likely cause the program to slow down.
A summary. We used the ZipFile type in VB.NET. We compressed all the files in one directory into a single ZIP file. We then expanded that file into the original form.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jul 13, 2021 (edit).
Home
Changes
© 2007-2024 Sam Allen.