/// <summary> /// /// </summary> /// <param name="Archive"></param> /// <param name="OutputFolder"></param> /// <returns></returns> public static bool UnpackArchive(string ArchivePath, string OutputFolder, ScriptBuildProgressDelegate ProgressCallback = null) { string installLocation = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); string sevenZipPath = Path.Combine(Path.Combine(Path.Combine(installLocation, "Libraries"), "7zip"), "7za.exe"); ProgressParser Parser = new ProgressParser(); Parser.ParsePartialLines = false; Parser.LineSeperator = "\r"; Parser.AddPattern(@"^\s+(\d+)\% \- (.+)$", new ProgressMatch(ProgressMatchType.Progress, ProgressMatchFormat.Float, 100), new ProgressMatch(ProgressMatchType.CurrentFileName, ProgressMatchFormat.String)); ScriptBuildOutputCallbackDelegate OutputCallback = (string Line) => { Parser.Parse(Line); ProgressCallback?.Invoke("Decompressing " + Parser.CurrentFileName + "...", Parser.Progress); }; int exitCode = RunAndWait(sevenZipPath, installLocation, "x \"" + ArchivePath + "\" -o\"" + OutputFolder + "\" -r -y -bsp1", OutputCallback); return(exitCode == 0); /* * * // SharpCompress is waaaaaaaaaaaaaaaaaaaaaaaaaaaaaay to slow :| * try * { * using (IArchive Archive = ArchiveFactory.Open(ArchivePath)) * { * int BufferLength = 1 * 1024 * 1024; * byte[] Buffer = new byte[BufferLength]; * * long TotalUncompressed = Archive.TotalUncompressSize; * long Uncompressed = 0; * * foreach (IArchiveEntry Entry in Archive.Entries.Where(entry => !entry.IsDirectory)) * { * using (Stream ArchiveStream = Entry.OpenEntryStream()) * { * using (FileStream FileStream = new FileStream(OutputFolder + @"\" + Entry.Key, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)) * { * int BytesRead = 0; * while ((BytesRead = ArchiveStream.Read(Buffer, 0, Buffer.Length)) > 0) * { * FileStream.Write(Buffer, 0, BytesRead); * Uncompressed += BytesRead; * * float Progress = (float)Uncompressed / (float)TotalUncompressed; * ProgressCallback?.Invoke("Unpacking " + Entry.Key + "...", Progress); * } * } * } * } * } * } * catch (Exception Ex) // Should be more specific, but sharpcompress has so little documentation and looking through the code there are so many potential exceptions... * { * Logger.Log(LogLevel.Error, LogCategory.Script, "Failed to extract archive with error: {0}", Ex.Message.ToString()); * return false; * } * * return true; */ }
/// <summary> /// /// </summary> /// <param name="State"></param> /// <param name="Progress"></param> public void ReportProgress(string State, float Progress) { ProgressCallback?.Invoke(State, Progress); }