SoFunction
Updated on 2025-03-07

Example of C# method to implement file compression and decompression [ZIP format]

This article describes the exampleC#Methods to implement file compression and decompression。 Share it for your reference, as follows:

During the development of enterprises, you often encounter file compression and decompression. Although many popular compressed file formats on the Internet are RAR, since RAR is not an open standard, ZIP has become the choice of more people. If you don't want to develop it yourself, you can choose an open source project, for exampleSharpZipLibIt's a good choice.

The use of components is relatively simple, please refer to the code below. Click to downloadProject source code

/*
 * Gary Zhang -- cbcye@
 * 
 * 
 * 
 */
using System;
using ;
using ;
using ;
using ;
using ;
using ;
namespace TestConsole
{
  class Program
  {
    static void Main()
    {
      //CreateZipFile(@"d:\", @"d:\");
      UnZipFile(@"d:\");
      ();
    }
    private static void CreateZipFile(string filesPath, string zipFilePath)
    {
      if (!(filesPath))
      {
        ("Cannot find directory '{0}'", filesPath);
        return;
      }
      try
      {
        string[] filenames = (filesPath);
        using (ZipOutputStream s = new ZipOutputStream((zipFilePath)))
        {
          (9); // Compression level 0-9          // = "123"; //Zip compressed file password          byte[] buffer = new byte[4096]; //Buffer size          foreach (string file in filenames)
          {
            ZipEntry entry = new ZipEntry((file));
             = ;
            (entry);
            using (FileStream fs = (file))
            {
              int sourceBytes;
              do
              {
                sourceBytes = (buffer, 0, );
                (buffer, 0, sourceBytes);
              } while (sourceBytes > 0);
            }
          }
          ();
          ();
        }
      }
      catch (Exception ex)
      {
        ("Exception during processing {0}", ex);
      }
    }
    private static void UnZipFile( string zipFilePath)
    {
      if (!(zipFilePath))
      {
        ("Cannot find file '{0}'", zipFilePath);
        return;
      }
      using (ZipInputStream s = new ZipInputStream((zipFilePath)))
      {
        ZipEntry theEntry;
        while ((theEntry = ()) != null)
        {
          ();
          string directoryName = ();
          string fileName = ();
          // create directory
          if ( > 0)
          {
            (directoryName);
          }
          if (fileName != )
          {
            using (FileStream streamWriter = ())
            {
              int size = 2048;
              byte[] data = new byte[2048];
              while (true)
              {
                size = (data, 0, );
                if (size > 0)
                {
                  (data, 0, size);
                }
                else
                {
                  break;
                }
              }
            }
          }
        }
      }
    }
  }
}

For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.