Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

File Compression and Decompression Components for .NET

Tech 1

1. Overview of Compression Formats

Data compression is a process of encoding information using fewer bits than the original representation. Common lossless compression algorithms include LZW, ZIP, RAR, and 7-Zip. Below is a comparison of mainstream formats:

  • ZIP: A widely used format employing the Deflate algorithm. Its a open and free standard, supported by numerous software applications.
  • RAR: A proprietary format known for its high compression ratio, though compression and decompression speeds are generally slower compared to ZIP.
  • 7z: An open-source format supporting a modular architecture that allows various compression algorithms. It typically achieves a higher compression ratio than ZIP.

2. Open-Source .NET Compression Libraries

2.1 DotNetZip

DotNetZip is a .NET library for handling ZIP files. It provides straightforward APIs for creating, extracting, and updating ZIP archives.

// Creating a ZIP file
using (ZipFile zip = new ZipFile())
{
    zip.Password = "securePassword";
    zip.AddFile("document.txt");
    zip.AddDirectory(@"C:\MyProject");
    zip.Comment = "Archive created by DotNetZip";
    zip.Save("output.zip");
}

// Extracting a ZIP file
using (ZipFile archive = ZipFile.Read("archive.zip"))
{
    foreach (ZipEntry item in archive)
    {
        item.Extract(@"C:\ExtractPath", ExtractExistingFileAction.OverwriteSilently);
    }
}

2.2 SevenZipSharp

SevenZipSharp is a .NET wrapper for the 7z.dll library, enabling support for multiple formats including 7z, ZIP, RAR, and GZIP.

// Compressing files
var compressor = new SevenZipCompressor();
compressor.ArchiveFormat = OutArchiveFormat.SevenZip;
compressor.CompressionMode = CompressionMode.Create;
compressor.CompressFiles("compressed.7z", @"C:\file1.txt", @"C:\file2.txt");

// Extracting an archive
using (var extractor = new SevenZipExtractor("archive.7z"))
{
    extractor.ExtractArchive(@"C:\OutputDirectory");
}

Note: The SevenZipExtractor constructor requires the 7z.dll library to be present in the application's execution directory.

2.3 SharpCompress

SharpCompress is a comprehensive library for reading and writing various archive formats such as ZIP, RAR, 7Zip, and TAR.

// Reading and extracting a RAR file
using (var archive = ArchiveFactory.Open("archive.rar"))
{
    foreach (var entry in archive.Entries)
    {
        if (!entry.IsDirectory)
        {
            entry.WriteToDirectory(@"C:\extract", new ExtractionOptions()
            {
                ExtractFullPath = true,
                Overwrite = true
            });
        }
    }
}

3. Practical Implementation Example

The following utility class demonstrates a unified approach for decompressing different archive types using SevenZipSharp.

using SevenZip;
using System;

namespace CompressionUtility
{
    public class ArchiveManager
    {
        public static void DecompressArchive(string sourcePath, string targetDirectory)
        {
            string extension = System.IO.Path.GetExtension(sourcePath).ToLowerInvariant();

            switch (extension)
            {
                case ".zip":
                case ".rar":
                case ".7z":
                case ".tar":
                    ExtractWithSevenZip(sourcePath, targetDirectory);
                    break;
                default:
                    throw new NotSupportedException($"Unsupported archive format: {extension}");
            }
        }

        private static void ExtractWithSevenZip(string archivePath, string outputPath)
        {
            try
            {
                using (var extractor = new SevenZipExtractor(archivePath))
                {
                    extractor.ExtractArchive(outputPath);
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException($"Failed to extract {archivePath}", ex);
            }
        }
    }
}

4. Selection Guidelines and Resources

  • DotNetZip is ideal for projects requiring simple, dedicated ZIP file manipulation.
  • SevenZipSharp offers broad format support and high compression ratios, making it suitable for new applications.
  • SharpCompress provides extensive format compatibility for reading and writing.

Project Links:

Tags: .NETC#

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.