Friday 14 August 2015

Creating zip file with ZipArchive in C# & VB

It is common for us to develop a function to compress physical files or any content generated from our code into zip archives. To implement the functionality, we may opt to use third party libraries to do so. If your project can target .NET Framework 4.5 and above, then you are able to use ZipArchive for file(s) or content compression.

Here are some stuff that ZipArchive can do:
1. Compress 1 or more files into a new or existing zip file.
2. The name of the file(s) can be changed before compress it to the zip file.
3. Compress content generated in codes, specify the file for the content to be stored into zip file.
4. Delete 1 or more files in the zip file.
5. Extract files.
6. 3 levels from compression (Fastest, Optimal, NoCompression).
7. Compress content or files into stream.

Without further ado, Let us check out how to use the ZipArchive. To use the ZipArchive, you need to have the following references. If you want to do more with ZipArchive, you need to have a reference to the System.IO.Compression.FileSystem namespace.

[C#]
using System.IO.Compression;

[VB]
Imports System.IO.Compression


Next is to initialize the ZipArchive.
  • archiveFileName: The location of the zip file.
  • mode: Action to be taken to the zip file. 
    • 3 Modes
      • Create : For creating a new zip file.
      • Read : For accessing the zip file content.
      • Update: For changing the zip file content.
[C#]
var zipArchive = ZipFile.Open(archiveFileName, mode);

[VB]
Dim zipArchive As ZipArchive = ZipFile.Open(archiveFileName, mode)


After initializing the ZipArchive, you can start compressing, deleting or extracting files. Let's start with extracting files. By executing the following code, all the files in the zip will be extracted and placed in a folder. The location of the folder is based on what you specified in the parameter.
  • destinationDirectoryName: Define the location for extracting the content in the zip file.
[C#]
zipArchive.ExtractToDirectory(destinationDirectoryName);

[VB]
zipArchive.ExtractToDirectory(destinationDirectoryName)


For the following code, it is used to compress physical file into the zip file.
  • sourceFileName: The physical file location. 
  • entryName: The name of the physical file when compress it to zip file. 
  • compressionLevel: Define the CompressionLevel of the file. 
    • 3 CompressionLevel 
      • Fastest: Compress the file quickly.
      • Optimal: Compress the file optimally.
      • NoCompression: No compression done on the file.
[C#]
zipArchive.CreateEntryFromFile(sourceFileName, entryName, compressionLevel);

[VB]
zipArchive.CreateEntryFromFile(sourceFileName, entryName, compressionLevel)


And finally, the following code is to remove the file located inside the zip file.
  • entryName: The name of the file located in the zip file.
[C#]
var zipArchiveEntry = zipArchive.GetEntry(entryName);
zipArchiveEntry.Delete();

[VB]
Dim zipArchiveEntry As ZipArchiveEntry = zipArchive.GetEntry(entryName)
zipArchiveEntry.Delete()

If you want to compress files or content into a stream or stream the compressed content through WCF, you can do so with ZipArchive. Other than that, please remember to dispose of any disposable objects after you have finished using them, this is to prevent any memory leak from happening. You can dispose them by calling the .Dispose() method or use the using statement.

For more details on how to use ZipArchive and how to use stream with ZipArchive, you can check out the sample project here. https://1drv.ms/u/s!Aj2AA7hoIWHmgnaTDEvJCLxcXVtt




No comments:

Post a Comment