/// <summary> /// Compress al inputFiles to destination sevenZip archive /// </summary> /// <param name="inputPath">Full path of a folder to be compressed</param> /// <param name="destinationFile">Path of where to create 7z archive</param> public static void Compress(string inputPath, string destinationFile) { SevenZipWorking wrk = new SevenZipWorking(); try { if (Directory.Exists(inputPath)) { wrk.CompressDirectory(inputPath, destinationFile); } if (File.Exists(inputPath)) { var file = new string[1] { inputPath }; wrk.CompressFiles(file, destinationFile); } } catch (Exception e) { log.Error(e.Message, e); throw; } finally { wrk = null; GC.Collect(); } }
/// <summary> /// Compress al inputFiles to destination sevenZip archive /// </summary> /// <param name="inputFiles">Array of full path of input files</param> /// <param name="destinationFile">Path of where to create 7z archive</param> public static void Compress(string[] inputFiles, string destinationFile) { SevenZipWorking wrk = new SevenZipWorking(); try { wrk.CompressFiles(inputFiles, destinationFile); } catch (Exception e) { log.Error(e.Message, e); throw; } finally { wrk = null; GC.Collect(); } }
/// <summary> /// Decompress file contained in inputFile inside a destinationDir /// </summary> /// <param name="inputFile">Path to 7z archive</param> /// <param name="destinationDir">Directory where to put the archive content</param> /// <returns>Return true if extraction is succeded, false otherwise</returns> public static bool Decompress(string inputFile, string destinationDir) { SevenZipWorking wrk = new SevenZipWorking(); try { return(wrk.ExtractFile(inputFile, destinationDir)); } catch (Exception e) { log.Error(e.Message, e); throw; } finally { wrk = null; GC.Collect(); //wait max 1 sec for file to be released int i = 0; while (i < 10) { FileStream fs = null; try { fs = File.Open(inputFile, FileMode.Open, FileAccess.Read, FileShare.None); fs.Close(); i = 10; } catch (Exception) { fs?.Close(); Thread.Sleep(100); i++; } } } }