Archives

De Banane Atomic
Aller à la navigationAller à la recherche

ZipFile

Pas de gestion des mots de passe
Csharp.svg
using System.IO.Compression; // System.IO.Compression.FileSystem

// archiver un dossier
ZipFile.CreateFromDirectory(@"c:\FolderToZip", @"c:\Archive.zip");

// désarchiver
ZipFile.ExtractToDirectory(@"c:\Archive.zip", @"c:\Destination");

SharpZipLib

À partir du framework 4.5 il est possible d'utiliser ZipFile

Memory

Csharp.svg
ZipFile zipFile = ZipFile.Create("path to zip");
zipFile.BeginUpdate();

zipFile.Add("path to file1", Path.GetFileName("path to file1"));
zipFile.Add("path to file2", Path.GetFileName("path to file2"));

zipFile.CommitUpdate();
zipFile.Close();

Stream

Csharp.svg
using (var zipOutputStream = new ZipOutputStream(File.Create(compressedFilePath)))
{
    zipOutputStream.SetLevel(9); // 0-9, 9 being the highest compression
    zipOutputStream.Password = password;

    byte[] buffer = new byte[4096];
    var entry = new ZipEntry(Path.GetFileName(filePathToInclude));
    entry.DateTime = DateTime.Now;
    zipOutputStream.PutNextEntry(entry);
    using (FileStream fs = File.OpenRead(filePathToInclude))
    {
        int sourceBytes;
        do
        {
            sourceBytes = fs.Read(buffer, 0, buffer.Length);
            zipOutputStream.Write(buffer, 0, sourceBytes);
        } while (sourceBytes > 0);
    }
    zipOutputStream.Finish();
    zipOutputStream.Close();
}

GZipStream

Csharp.svg
var filePath = "/chemin/archive.gz";

using (FileStream fileStream = File.OpenRead ("/chemin/archive.gz")) {
	var decompressedFilePath = Path.Combine ("/tmp", Path.GetFileNameWithoutExtension (filePath));

	using (FileStream decompressedFileStream = File.Create (decompressedFilePath)) {
		using (GZipStream decompressionStream = new GZipStream (fileStream, CompressionMode.Decompress)) {
			decompressionStream.CopyTo (decompressedFileStream);
		}
	}
}