Home > C#, utils > Create ZIP file

Create ZIP file

A class to create ZIP file from another file.
This class uses the free C# ZIP library (http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx) is great for reading and creating ZIP files.
The parameter source is full path to source file.
The parameter destination is full path to destination result zip file.

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;

namespace Tools
{
  public class Zip
  {
   public static void create(string source, string destination, string comment)
    {
      Crc32 crc = new Crc32();
      ZipOutputStream s = new ZipOutputStream(File.Create(@destination));
      s.SetLevel(9); // 0 - store only to 9 - means best compression
      FileStream fs = File.OpenRead(@source);
      byte[] buffer = new byte[fs.Length];
      fs.Read(buffer, 0, buffer.Length);
      ZipEntry entry = new ZipEntry(ZipEntry.CleanName(@source));
      entry.DateTime = DateTime.Now;
      entry.Comment = comment;
      entry.ZipFileIndex = 1;
      entry.Size = fs.Length;
      fs.Close();
      crc.Reset();
      crc.Update(buffer);
      entry.Crc = crc.Value;
      s.PutNextEntry(entry);
      s.Write(buffer, 0, buffer.Length);
      s.Finish();
      s.Close();
    }
  }
}

Unique visitors to post: 4

Categories: C#, utils Tags: ,
  1. No comments yet.
  1. No trackbacks yet.
Google Analytics integration offered by Wordpress Google Analytics Plugin