Diese Methode packt alle übergebenen Dateien in ein ZIP Archiv.
Nach dem Download der ICSharpCode.SharpZipLib.dll von http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx muss dem Projekt ein Verweis zu dieser DLL hinzugefügt werden.
Die folgenden Usings müssen verwendet werden:
using System;
using System.IO;
using System.Collections.Generic;
using ICSharpCode.SharpZipLib.Checksums;
using ICSharpCode.SharpZipLib.Zip;
/// <summary>
/// Writes the zip file.
/// </summary>
/// <param name="filesToZip">The files to zip.</param>
/// <param name="path">The destination path.</param>
/// <param name="compression">The compression level.</param>
private static void WriteZipFile(List<string> filesToZip, string path, int compression)
{
if (compression < 0 || compression > 9)
throw new ArgumentException("Invalid compression rate.");
if (!Directory.Exists(new FileInfo(path).Directory.ToString()))
throw new ArgumentException("The Path does not exist.");
foreach (string c in filesToZip)
if (!File.Exists(c))
throw new ArgumentException(string.Format("The File{0}does not exist!", c));
Crc32 crc32 = new Crc32();
ZipOutputStream stream = new ZipOutputStream(File.Create(path));
stream.SetLevel(compression);
for (int i = 0; i < filesToZip.Count; i++)
{
ZipEntry entry = new ZipEntry(Path.GetFileName(filesToZip[i]));
entry.DateTime = DateTime.Now;
using (FileStream fs = File.OpenRead(filesToZip[i]))
{
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, 0, buffer.Length);
entry.Size = fs.Length;
fs.Close();
crc32.Reset();
crc32.Update(buffer);
entry.Crc = crc32.Value;
stream.PutNextEntry(entry);
stream.Write(buffer, 0, buffer.Length);
}
}
stream.Finish();
stream.Close();
}
3 Kommentare zum Snippet