1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
| using System;
using System.Collections.Generic;
using System.Text;
using ICSharpCode.SharpZipLib.Zip;
using System.IO;
namespace TDLinkerToToolsTests
{
static class DeZipper
{
static private string StrPathFichier = "";
static private void InitDeZipper(string StrFiles)
{
StrPathFichier = StrFiles;
}
// Fonction Compression
// Action : Compresse un fichier donné en fichier zip
// Entrée : intLvlZip : niveau de compression (de 1 mini à 9 max )
// Entrée : strExt : Extension après la compression (pas obligatoirement .zip donc).
// Exemple : .crz, .zip, .etc
// Sortie : un booléen pour savoir si ça a marché
static public bool Compression(int intLvlZip, string strExt, string StrFiles)
{
InitDeZipper(StrFiles);
bool BolReussi = false; // on initialise la méthode de cryptage à faux
try
{
// declaration des variables
string StrCheminZip;
int IntSize = 4096; // taille du tampon
byte[] data = new byte[4096]; // tampon qui stockera 4096bytes d'info à chaque fois
if (intLvlZip < 1) // on vérifie si c pas un chiffre trop faible pour la compression
intLvlZip = 1;
if (intLvlZip > 9) // on vérifie si c pas un chiffre trop fort pour la compression
intLvlZip = 9;
// on va crée le chemin du fichier zip qui va être généré
StrCheminZip = Path.GetDirectoryName(StrPathFichier) + "\\" + (Path.GetFileNameWithoutExtension(StrPathFichier)) + strExt;
ZipOutputStream MyZipOutputStream = new ZipOutputStream(File.Create(StrCheminZip)); // création du fichier zip vide
MyZipOutputStream.SetLevel(intLvlZip); // Niveau de compression de 0 (faible) à 9 (fort)
FileStream fs = File.OpenRead(StrPathFichier); // lecture du fichier non compréssé
ZipEntry theEntry = new ZipEntry(Path.GetFileName(StrPathFichier)); // prends le nom que du fichier en creant le zip
MyZipOutputStream.PutNextEntry(theEntry); // on selectionne le fichier à l'interieur du zip
while (IntSize > 0) // on lit le fichier jusqu'à la fin par bloc de 4096 bytes
{
IntSize = fs.Read(data, 0, IntSize);
if (IntSize > 0)
{
MyZipOutputStream.Write(data, 0, IntSize);
}
}
MyZipOutputStream.Finish(); // on ferme le flux zip
MyZipOutputStream.Close(); // on ferme le flux zip
fs.Close(); // on ferme le flux d'écriture
BolReussi = true; // on met la variable sur réussi
return BolReussi;
}
catch
{
return BolReussi;
}
}
// Fonction Décompression
// Action : Décompresse avec l'algo zip
// Entrée : rien
// Sortie : un booléen pour savoir si ça a marché
static public bool Decompression(string StrFiles)
{
InitDeZipper(StrFiles);
string StrCheminFichier;
int IntSize = 4096; // taille du tampon
byte[] data = new byte[4096];
bool BolReussi = false; // on initialise la méthode de cryptage à faux
try
{
ZipInputStream MyZipInStream = new ZipInputStream(File.OpenRead(StrPathFichier));
ZipEntry theEntry;
theEntry = MyZipInStream.GetNextEntry();
//StrCheminFichier = Path.GetDirectoryName(StrPathFichier) + "\\" + theEntry.Name; // on prend le chemin du zip et on prend le nom du fichier compressé
FileStream fsFichierDezip = null; //= new FileStream(StrCheminFichier, FileMode.Create); // on crée le fichier à décompresser (donc d'abord vide)
while (theEntry != null) // tant qu'on travaille sur un fichier
{
StrCheminFichier = Path.GetDirectoryName(StrPathFichier) + "\\" + theEntry.Name; // on prend le chemin du zip et on prend le nom du fichier compressé
fsFichierDezip = new FileStream(StrCheminFichier, FileMode.Create);
data = new byte[4096];
while ((IntSize = MyZipInStream.Read(data, 0, IntSize)) > 0)
{
fsFichierDezip.Write(data, 0, IntSize);
}
fsFichierDezip.Flush();
fsFichierDezip.Close();
theEntry = MyZipInStream.GetNextEntry();
IntSize = 4096;
}
MyZipInStream.Close(); // On ferme le flux de lecture du zip
fsFichierDezip.Close();
BolReussi = true; // on met la variable sur réussi
return BolReussi;
}
catch
{
return BolReussi;
}
}
}
} |
Partager