Skip to content

Commit

Permalink
Backup WinRar compression logic wrapped into API class.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambratolm authored and Ambratolm committed Aug 9, 2021
1 parent e3cb819 commit 48f527c
Show file tree
Hide file tree
Showing 13 changed files with 105 additions and 38 deletions.
Binary file modified GestionClient/GestionClient.suo
Binary file not shown.
94 changes: 94 additions & 0 deletions GestionClient/GestionClient/API/Miscellaneous/WinRar.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.IO;

namespace GestionClient
{
/// <summary>
/// Wrapper for the WinRar compression and uncompression command line tools.
/// </summary>
class WinRar : IDisposable
{
private readonly Process _process = new Process();

public string RarPath { get; set; }
public string UnrarPath { get; set; }

/// <summary>
/// Creates a new instance using provided or available WinRar command line tools.
/// </summary>
/// <param name="rarPath">Path for WinRar compression command line tool (Rar). </param>
/// <param name="unrarPath">Path for WinRar uncompression command line tool (Unrar).</param>
public WinRar(string rarPath = null, string unrarPath = null)
{
if (rarPath == null)
{
this.RarPath = @"C:\Program Files\WinRAR\rar.exe";
}
if (unrarPath == null)
{
this.UnrarPath = @"C:\Program Files\WinRAR\unrar.exe";
}
if (!File.Exists(this.RarPath))
{
throw new IOException("Rar compression command line tool not found.");
}
if (!File.Exists(this.UnrarPath))
{
throw new IOException("Unrar uncompression command line tool not found.");
}
_process.StartInfo.CreateNoWindow = true;
_process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
_process.EnableRaisingEvents = true;
}

/// <summary>
/// Creates an archive of compressed file or directory.
/// </summary>
/// <param name="archiveFilePath">Path for the archive to create.</param>
/// <param name="sourceFilesPath">Path of the file or directory to compress.</param>
public void Compress(string archiveFilePath, string sourceFilesPath)
{
_process.StartInfo.FileName = RarPath;
_process.StartInfo.Arguments = String.Format("a -r \"{0}\" \"{1}\"",
archiveFilePath, sourceFilesPath);
_process.Start();
_process.WaitForExit();
}

/// <summary>
/// Extracts an archive in a destination path.
/// </summary>
/// <param name="archiveFilePath">Path for archive to extract.</param>
/// <param name="destinationPath">Path for saving the extracted files.</param>
public void Extract(string archiveFilePath, string destinationPath)
{
_process.StartInfo.FileName = UnrarPath;
_process.StartInfo.Arguments = String.Format("x \"{0}\" \"{1}\"",
archiveFilePath, destinationPath);
_process.Start();
_process.WaitForExit();
}

#region IDisposable-Methods
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_process != null)
{
_process.Close();
}
}
}
#endregion
}
}
37 changes: 10 additions & 27 deletions GestionClient/GestionClient/GUI/Form_Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,37 +195,20 @@ private void menuItem_backup_Click(object sender, EventArgs e)
{
try
{
Process proc = new Process();
// chemin du programme winrar.exe
proc.StartInfo.FileName = @"C:\Program Files\WinRAR\rar.exe"; // ou unrar.exe pour extraire
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.EnableRaisingEvents = true;

//PWD: Password if the file has any
//SRC: The path of your rar file. e.g: c:\temp\abc.rar
//DES: The path you want it to be extracted. e.g: d:\extracted
//ATTENTION: DESTINATION FOLDER MUST EXIST!

// lancement de l'extraction
//proc.StartInfo.Arguments = String.Format("x -p{0} {1} {2}", PWD, SRC, DES);
proc.StartInfo.Arguments = String.Format("a -r \"{0}\" \"{1}\"", saveFileDialog_main.FileName, App.FolderPath + "\\" + App.DatabaseFolderName);
proc.Start();

// attente de la fin de l'extraction
this.Cursor = Cursors.WaitCursor;
proc.WaitForExit();
this.Cursor = Cursors.Default;

if (File.Exists(saveFileDialog_main.FileName))
using (WinRar winrar = new WinRar())
{
QuickMessageBox.ShowInformation(string.Format("{0}{1}",
Language.GetString("MessageBox_DB_Saved_To"), saveFileDialog_main.FileName));
this.Cursor = Cursors.WaitCursor;
winrar.Compress(saveFileDialog_main.FileName, App.DatabaseFolderName);
this.Cursor = Cursors.Default;
}
else

if (!File.Exists(saveFileDialog_main.FileName))
{
throw new Exception(Language.GetString("MessageBox_Erreur"));
throw new FileNotFoundException(Language.GetString("MessageBox_Erreur"));
}

QuickMessageBox.ShowInformation(string.Format("{0}{1}",
Language.GetString("MessageBox_DB_Saved_To"), saveFileDialog_main.FileName));
}
catch (Exception exception)
{
Expand Down
1 change: 1 addition & 0 deletions GestionClient/GestionClient/GestionClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<Compile Include="API\Core\Database.cs" />
<Compile Include="API\Core\Language.cs" />
<Compile Include="API\UI Elements\QuickMessageBox.cs" />
<Compile Include="API\Miscellaneous\WinRar.cs" />
<Compile Include="GUI\Help\Form_About.cs">
<SubType>Form</SubType>
</Compile>
Expand Down
Binary file modified GestionClient/GestionClient/bin/Debug/GestionClient.exe
Binary file not shown.
Binary file modified GestionClient/GestionClient/bin/Debug/GestionClient.pdb
Binary file not shown.

This file was deleted.

Binary file not shown.
Binary file not shown.
Binary file modified GestionClient/GestionClient/obj/x86/Debug/GestionClient.exe
Binary file not shown.
Binary file modified GestionClient/GestionClient/obj/x86/Debug/GestionClient.pdb
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 48f527c

Please sign in to comment.