/// <summary> /// /// </summary> /// <param name="folder">Folder to Compress</param> /// <param name="archiveDest">Enter a name without extension</param> /// <param name="cplLvl">Niveau de compression 0/1/2</param> /// <returns></returns> public static bool CompressFolder(string folder, string archiveDest, int cplLvl) { if (!Directory.Exists(folder)) { return(false); } string zipName = archiveDest + ".zip"; // Verification if (File.Exists(zipName)) { //var res = MessageBox.Show(", do you want to delete it or abort ?", "ok", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation); ////var res = MB_Decision.Show("Zip File Exists for this game", "Alert", destination: zipName, buttons: MB_Decision.Mode.NoStop); ////if (res == MB_Decision.Result.Pass) ////{ //// //MessageBox.Show("Zip Compression Aborted", "info", MessageBoxButtons.OK, MessageBoxIcon.Information); //// return false; ////} ////else if (res == MB_Decision.Result.Trash) ////{ //// // Move to Recycle.Bin //// try //// { //// FileSystem.DeleteDirectory(zipName, UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin); //// } //// catch (Exception e) //// { //// Debug.WriteLine(e.Message); //// } ////} } while (true) { try { // Get value from enum CompressionLevel cpLvl = (CompressionLevel)cplLvl; using (Ionic.Zip.ZipFile zipFile = new Ionic.Zip.ZipFile()) { zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression; //zipFile.BufferSize = 4096; zipFile.AddDirectory(folder); zipFile.AddProgress += addProgress; zipFile.SaveProgress += SaveProgress; BoxProgress = new ProgressCompFolder(); BoxProgress.Text = "Compression Zip"; Task.Run(() => zipFile.Save(zipName)); BoxProgress.ShowDialog(); } return(true); } catch (Exception e) { Debug.WriteLine($"Erreur de compression {e.Message}"); var res = MessageBox.Show($"Erreur lors de la compression zip, merci de régler le problème et decliquer sur retry:\n{e}", "Erreur", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Exclamation); // Exit if abort if (res == DialogResult.Abort) { return(false); } } } }
/// <summary> /// /// </summary> /// <param name="folder">Folder</param> /// <param name="archiveDest">Archive name</param> /// <param name="cplLvl">Level of compression</param> /// <returns></returns> public static bool CompressFolder(string folder, string archiveDest, int cplLvl) { if (!Directory.Exists(folder)) { return(false); } _ArchiveName = archiveDest + ".7z"; //string sSeventZipLink = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "x86", "7z.dll"); string sSeventZipLink = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, Environment.Is64BitProcess ? "x64" : "x86", "7z.dll"); //var sevenZipPath = Path.Combine( Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),Environment.Is64BitProcess ? "x64" : "x86", "7z.dll"); if (!File.Exists(sSeventZipLink)) { MessageBox.Show("7z.dll missing", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } SevenZipCompressor.SetLibraryPath(sSeventZipLink); // var k = SevenZipCompressor.CurrentLibraryFeatures; SevenZipCompressor szp = new SevenZipCompressor(); szp.CompressionLevel = CompressionLevel.Ultra; szp.CompressionMode = CompressionMode.Create; szp.ArchiveFormat = OutArchiveFormat.SevenZip; szp.DirectoryStructure = true; try { szp.FilesFound += FilesFound; szp.Compressing += Compressing; szp.FileCompressionStarted += FileCompressionStarted; szp.FileCompressionFinished += FileCompressionFinished; szp.CompressionFinished += CompressionFinished; BoxProgress = new ProgressCompFolder(); BoxProgress.Text = "Compression 7z"; Task t = Task.Run(() => { szp.CompressDirectory(folder, _ArchiveName); } ); BoxProgress.ShowDialog(); } catch (Exception e) { Console.WriteLine(e.Message); } return(true); }