[chronojump/michrolab] Sessions can be exported and imported compressed (7z)



commit c8a537ef3cbca0cac1fd70b9329e07ff9ba7baac
Author: Xavier de Blas <xaviblas gmail com>
Date:   Tue Sep 27 19:01:11 2022 +0200

    Sessions can be exported and imported compressed (7z)

 src/gui/app1/session/export.cs        | 23 ++++++++++++++++++++++
 src/gui/app1/session/loadAndImport.cs | 37 ++++++++++++++++++++++++++++++++---
 src/util.cs                           | 16 +++++++++++++++
 3 files changed, 73 insertions(+), 3 deletions(-)
---
diff --git a/src/gui/app1/session/export.cs b/src/gui/app1/session/export.cs
index 3e4ef29fe..2199c9615 100644
--- a/src/gui/app1/session/export.cs
+++ b/src/gui/app1/session/export.cs
@@ -70,6 +70,8 @@ public partial class ChronoJumpWindow
                        app1s_fileCopy = app1s_fc.Filename + Path.DirectorySeparatorChar + "chronojump_" + 
currentSession.Name + "_" + UtilDate.ToFile();
 
                        app1s_label_export_destination.Text = app1s_fileCopy;
+                       if (exportImportCompressed)
+                               app1s_label_export_destination.Text += ".7z";
 
                        app1s_button_export_start.Sensitive = true;
                }
@@ -201,6 +203,7 @@ public partial class ChronoJumpWindow
                }
        }
 
+       private const bool exportImportCompressed = true;
        static string app1s_exportText;
        static long app1s_exportElapsedMs;
        //No GTK here!
@@ -247,6 +250,26 @@ public partial class ChronoJumpWindow
                        count ++;
                }
 
+               if (exportImportCompressed)
+               {
+                       //compressing with 7z
+                       app1s_exportText = string.Format("Compressing …");
+                       List<string> parameters = new List<string>();
+                       parameters.Add ("a");
+                       parameters.Add (app1s_fileCopy + ".7z");
+
+                       // option 1 add the folder with the files (better to have a dir that can be 
uncompressed in order to be opened from importer)
+                       parameters.Add (app1s_fileCopy);
+                       // option 2 without the parent folder (cleaner, but do not found how to import)
+                       //parameters.Add (app1s_fileCopy + Path.DirectorySeparatorChar + "*");
+
+                       ExecuteProcess.Result execute_result = ExecuteProcess.run ("7z", parameters, false, 
false);
+                       // delete exported folder
+                       if (execute_result.success)
+                               Util.DirectoryDelete (app1s_fileCopy);
+               }
+
+               //finishing
                sw.Stop();
                app1s_exportElapsedMs = sw.ElapsedMilliseconds;
                LogB.Information("ended app1s_export()");
diff --git a/src/gui/app1/session/loadAndImport.cs b/src/gui/app1/session/loadAndImport.cs
index deaacae1f..95337565f 100644
--- a/src/gui/app1/session/loadAndImport.cs
+++ b/src/gui/app1/session/loadAndImport.cs
@@ -19,6 +19,7 @@
  */
 
 using System;
+using System.IO;
 using Gtk;
 using Glade;
 using GLib; //for Value
@@ -372,12 +373,42 @@ public partial class ChronoJumpWindow
                                                                               "Open",ResponseType.Accept);
 
                FileFilter file_filter = new FileFilter();
-               file_filter.AddPattern ("*.db");
-               file_filter.Name = "Chronojump database (chronojump.db)";
+
+               if (exportImportCompressed)
+               {
+                       file_filter.AddPattern ("*.7z");
+                       file_filter.Name = "Chronojump data (.7z)";
+               } else {
+                       file_filter.AddPattern ("*.db");
+                       file_filter.Name = "Chronojump database (chronojump.db)";
+               }
                filechooser.AddFilter (file_filter);
 
-               if (filechooser.Run () == (int)ResponseType.Accept) {
+               if (filechooser.Run () == (int)ResponseType.Accept)
+               {
+                       if (exportImportCompressed)
+                       {
+                               List<string> parameters = new List<string>();
+                               //parameters.Add ("e");
+                               parameters.Add ("x"); //we need the parent folder
+                               parameters.Add ("-aoa"); //Overwrite All existing files without prompt.
+                               parameters.Add ("-o" + Path.DirectorySeparatorChar + UtilAll.GetTempDir ());
+                               parameters.Add (filechooser.Filename);
+
+                               ExecuteProcess.Result execute_result = ExecuteProcess.run ("7z", parameters, 
false, false);
+                       }
+
                        app1s_import_file_path = filechooser.Filename;
+                       if (exportImportCompressed)
+                       {
+                               app1s_import_file_path = Path.Combine (
+                                               UtilAll.GetTempDir (),
+                                               Util.RemoveExtension (Util.GetLastPartOfPath 
(filechooser.Filename)),
+                                               "database",
+                                               "chronojump.db");
+                               LogB.Information ("import from: " + app1s_import_file_path);
+                       }
+
                        //file_path_import.Text = System.IO.Path.GetFileName (import_file_path);
                        app1s_file_path_import.Text = app1s_import_file_path;
                        app1s_file_path_import.TooltipText = app1s_import_file_path;
diff --git a/src/util.cs b/src/util.cs
index b9236596d..495ec27d2 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -1466,6 +1466,22 @@ public class Util
                return false;
        }
        
+       public static bool DirectoryDelete (string fileName)
+       {
+               LogB.Information("Checking if this directory exists: " + fileName);
+               try {
+                       if(Directory.Exists (fileName)) {
+                               LogB.Information ("Deleting " + fileName + " ...");
+                               Directory.Delete (fileName, true); //recursive
+                               LogB.Information ("Deleted");
+                               return true;
+                       }
+               } catch {
+                       LogB.Error ("Problem deleting");
+               }
+               return false;
+       }
+
        public static bool FileMove(string path, string filenameOrigin, string filenameDestination)
        {
                LogB.Information(string.Format("Going to move: {0} to {1}",


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]