private bool SetupNewSoft() { bool bIsSetupDone = true; try { string szFile = string.Format("{0}\\{1}", DOWNPATH, m_pFileName); if (File.Exists(szFile)) { Unrar unrar = new Unrar(); unrar.DestinationPath = APPPATH; unrar.Open(szFile, Unrar.OpenMode.Extract); while (unrar.ReadHeader()) { unrar.Extract(); } if (unrar != null) { unrar.Close(); } } } catch (Exception e) { bIsSetupDone = false; AddMsg("解压缩失败原因:" + e.Message); } return(bIsSetupDone); }
private static string GetRARFileCRC(string rarVolumePath, string fileName) { if (!FileHandler.FileExists(rarVolumePath)) { return(string.Empty); } Unrar unrar = new Unrar(rarVolumePath); try { unrar.Open(); while (unrar.ReadHeader() && unrar.CurrentFile.FileName != fileName) { unrar.Skip(); } if (unrar.CurrentFile != null && unrar.CurrentFile.FileName == fileName) { return(unrar.CurrentFile.FileCRC.ToString("X8").ToLower()); } } finally { unrar.Close(); } return(string.Empty); }
/// <summary> /// Attempts to unrar a rar archive and to copy its content into a folder. /// If it succeeds, the resulting images are added to the files to be added into the generation. /// </summary> /// <param name="path">Path of the rar archive to be checked.</param> private void CheckRarArchive(string path) { try { using (Unrar rarFile = new Unrar()) { int pathIndex = Data.Files.IndexOf(path); string rarPath = GenerateArchivePath(path); rarFile.DestinationPath = rarPath; rarFile.Open(path, Unrar.OpenMode.Extract); while (rarFile.ReadHeader()) { rarFile.Extract(); } List <string> newFiles = new List <string>(); ParseExtractedElements(newFiles, new DirectoryInfo(rarPath)); InsertArchiveFiles(pathIndex, newFiles); } } catch { rarErrors.Add(path); Data.Files.Remove(path); } }
public static CFolder GetFolder(string FileName) { if (FileName.Substring(FileName.Length - 4, 4).ToLower() != ".rar") { throw new Exception("File type did not match this Plugins!"); } char[] sp = { '\\' }; string[] TempStrs = FileName.Split(sp); CFolder TempFolder = new CFolder(); if (Directory.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles) + " (x86)")) { Unrar64 rar = new Unrar64(FileName); rar.Open(Unrar64.OpenMode.List); TempFolder.Name = TempStrs[TempStrs.Length - 1]; Array.Clear(TempStrs, 0, TempStrs.Length); while (rar.ReadHeader()) { //if (!rar.CurrentFile.IsDirectory) //{ TempStrs = rar.CurrentFile.FileName.Split(sp); FillFolder(FileName, TempStrs, 0, TempFolder, rar.CurrentFile); //} rar.Skip(); } rar.Close(); rar = null; } else { Unrar rar = new Unrar(FileName); rar.Open(Unrar.OpenMode.List); TempFolder.Name = TempStrs[TempStrs.Length - 1]; Array.Clear(TempStrs, 0, TempStrs.Length); while (rar.ReadHeader()) { //if (!rar.CurrentFile.IsDirectory) //{ TempStrs = rar.CurrentFile.FileName.Split(sp); FillFolder(FileName, TempStrs, 0, TempFolder, rar.CurrentFile); //} rar.Skip(); } rar.Close(); rar = null; } TempFolder.SpecialItem = true; TempFolder.CreatedTime = (new FileInfo(FileName)).CreationTimeUtc; return(TempFolder); }
private bool ExtractRARContent(string rarFilePath, RootPath rootPath, Record record) { bool success = true; SubRecord subRecord = null; _lastRARVolume = string.Empty; Unrar unrar = new Unrar(rarFilePath) { DestinationPath = (EngineSettings.UseSpecificOutputFolder ? Path.Combine(EngineSettings.OutputFolder, FileHandler.GetDeltaPath(Path.GetDirectoryName(rarFilePath), rootPath.Path)) : Path.GetDirectoryName(rarFilePath)) }; unrar.ExtractionProgress += unrar_ExtractionProgress; unrar.MissingVolume += unrar_MissingVolume; unrar.NewVolume += unrar_NewVolume; unrar.PasswordRequired += unrar_PasswordRequired; try { unrar.Open(Unrar.OpenMode.Extract); while (success && unrar.ReadHeader()) { WriteLogEntry("Extracting file, name=" + unrar.CurrentFile.FileName + ", size=" + unrar.CurrentFile.UnpackedSize + ", path=" + unrar.DestinationPath); subRecord = new SubRecord(unrar.DestinationPath, unrar.CurrentFile.FileName, unrar.CurrentFile.UnpackedSize); AddSubRecord(record.ID, subRecord); unrar.Extract(); success = ValidateExtractedFile(Path.Combine(unrar.DestinationPath, unrar.CurrentFile.FileName), unrar.CurrentFile.UnpackedSize, GetRARFileCRC(_lastRARVolume, unrar.CurrentFile.FileName)); if (!success) { WriteLogEntry(LogType.Warning, "Validation FAILED, aborting extraction"); } AddSubRecord(record.ID, (success ? subRecord.Succeed() : subRecord.Fail())); } } catch (Exception ex) { WriteLogEntry("An exception occurred while extracting from RAR file, path=" + rarFilePath + ", destination=" + unrar.DestinationPath, ex); if (subRecord != null) { AddSubRecord(record.ID, subRecord.Fail()); } success = false; } finally { unrar.Close(); unrar.ExtractionProgress -= unrar_ExtractionProgress; unrar.MissingVolume -= unrar_MissingVolume; unrar.NewVolume -= unrar_NewVolume; unrar.PasswordRequired -= unrar_PasswordRequired; } return(success); }
private void CheckFile(FiasFileInfo aFileInfo) { var folder = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted"; if (!Directory.Exists(folder)) { aFileInfo.Extracted = false; return; } var files = Directory.GetFiles(folder).Select(Path.GetFileName).ToList(); using (var unrar = new Unrar()) { unrar.ArchivePathName = aFileInfo.FileName; unrar.Open(Unrar.OpenMode.List); while (unrar.ReadHeader()) { if (stopEvent.WaitOne(0)) { throw new StopException(); } if (OnCheckProgress != null) { OnCheckProgress(0, 0, string.Format("Проверка файла {0}", folder + "\\" + unrar.CurrentFile.FileName)); } if (!files.Contains(unrar.CurrentFile.FileName)) { aFileInfo.Extracted = false; return; } var crc32 = new Crc32(); uint hash; using (var fs = File.Open(folder + "\\" + unrar.CurrentFile.FileName, FileMode.Open)) { var byteHash = crc32.ComputeHash(fs); hash = BitConverter.ToUInt32(byteHash.Reverse().ToArray(), 0); } if (unrar.CurrentFile.FileCRC != hash) { aFileInfo.Extracted = false; return; } unrar.Skip(); } } aFileInfo.Extracted = true; }
public void ConvertToImage(string rarPath, string imageOutputDirPath) { try { Unrar tmp = new Unrar(rarPath); tmp.Open(Unrar.OpenMode.List); string[] files = tmp.ListFiles(); tmp.Close(); int total = files.Length; int done = 0; Unrar unrar = new Unrar(rarPath); unrar.Open(Unrar.OpenMode.Extract); unrar.DestinationPath = imageOutputDirPath; while (unrar.ReadHeader() && !cancelled) { if (unrar.CurrentFile.IsDirectory) { unrar.Skip(); } else { unrar.Extract(); ++done; if (this.ProgressChanged != null) { this.ProgressChanged(done, total); } } } unrar.Close(); if (this.ConvertSucceed != null) { this.ConvertSucceed(); } } catch (Exception ex) { if (this.ConvertFailed != null) { this.ConvertFailed(ex.Message); } } }
private void CheckFile(FiasFileInfo aFileInfo) { var folder = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted"; if (!Directory.Exists(folder)) { aFileInfo.Extracted = false; return; } var files = Directory.GetFiles(folder).Select(Path.GetFileName).ToList(); using (var unrar = new Unrar()) { unrar.ArchivePathName = aFileInfo.FileName; unrar.Open(Unrar.OpenMode.List); while (unrar.ReadHeader()) { if (stopEvent.WaitOne(0)) throw new StopException(); if (OnCheckProgress != null) OnCheckProgress(0, 0, string.Format("Проверка файла {0}", folder + "\\" + unrar.CurrentFile.FileName)); if (!files.Contains(unrar.CurrentFile.FileName)) { aFileInfo.Extracted = false; return; } var crc32 = new Crc32(); uint hash; using (var fs = File.Open(folder + "\\" + unrar.CurrentFile.FileName, FileMode.Open)) { var byteHash = crc32.ComputeHash(fs); hash = BitConverter.ToUInt32(byteHash.Reverse().ToArray(), 0); } if (unrar.CurrentFile.FileCRC != hash) { aFileInfo.Extracted = false; return; } unrar.Skip(); } } aFileInfo.Extracted = true; }
private void openRarFile(string fileName) { using (Unrar unrar = new Unrar()) { unrar.Open(fileName, Unrar.OpenMode.List); while (unrar.ReadHeader()) { if (!unrar.CurrentFile.IsDirectory) { addFile(unrar.CurrentFile); } else { addDirectory(unrar.CurrentFile); } unrar.Skip(); } unrar.Close(); } }
private void UnRarFile(FiasFileInfo aFileInfo) { using (var unrar = new Unrar()) { unrar.Open(aFileInfo.FileName, Unrar.OpenMode.Extract); unrar.DestinationPath = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp"; unrar.ExtractionProgress += unrar_ExtractionProgress; while (unrar.ReadHeader()) { if (stopEvent.WaitOne(0)) { throw new StopException(); } unrar.Extract(); } } Directory.Move(Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp", Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted"); aFileInfo.Checked = false; aFileInfo.Extracted = true; }
protected void unrar(string filename, string destination, List <string> extensions) { Unrar unrar = null; try { // Create new unrar class and attach event handlers for // progress, missing volumes, and password unrar = new Unrar(); //AttachHandlers(unrar); // Set destination path for all files unrar.DestinationPath = destination; // Open archive for extraction unrar.Open(filename, Unrar.OpenMode.Extract); // SEASON_NR_EXTRACTION_PATTERNS_KEY each file with subtitle extension while (unrar.ReadHeader()) { string extension = Path.GetExtension(unrar.CurrentFile.FileName).Substring(1).ToLower().Replace(".", ""); if (extensions.Contains(extension)) { unrar.Extract(); } else { unrar.Skip(); } } } catch (Exception ex) { Logger.Instance.LogMessage("Error during unpack of file: " + filename + " (" + ex.Message + ")", LogLevel.ERROR); } finally { if (unrar != null) { unrar.Close(); } } }
/// <summary> /// Manages the paks. /// </summary> private void ManagePaks(StringBuilder logBuilder) { logBuilder.AppendLine(Logger.BuildLogWithDate("Starting to manage Paks")); FindPaks(logBuilder); logBuilder.AppendLine(Logger.BuildLogWithDate("Searching for actual files in Paks")); var zippedFiles = new List <ModFile>(); foreach (var file in Files) { var parts = file.FileName.Split('\\'); var filename = parts[parts.Length - 3] + "\\" + parts[parts.Length - 2] + "\\" + parts[parts.Length - 1]; var isRAR = CheckForRar(file.FileName, filename, logBuilder); // Replace RAR with ZIP if (isRAR) { logBuilder.AppendLine(Logger.BuildLogWithDate("Replacing RAR with ZIP")); Directory.CreateDirectory(file.FilePath + "\\TEMP_EXTRACT"); using (var rar = new Unrar(file.FileName)) { rar.Open(Unrar.OpenMode.Extract); rar.ReadHeader(); while (rar.CurrentFile != null) { rar.ExtractToDirectory(file.FilePath + "\\TEMP_EXTRACT"); rar.ReadHeader(); } rar.Close(); } logBuilder.AppendLine(Logger.BuildLogWithDate("Extracted RAR!")); ZipFile.CreateFromDirectory(file.FilePath + "\\TEMP_EXTRACT", file.FileName + ".extracted"); Directory.Delete(file.FilePath + "\\TEMP_EXTRACT", true); logBuilder.AppendLine(Logger.BuildLogWithDate("Created ZIP!")); if (File.Exists(file.FileName + ".extracted") && new FileInfo(file.FileName + ".extracted").Length > 30) { logBuilder.AppendLine(Logger.BuildLogWithDate("ZIP seems valid! Replacing RAR")); File.Move(file.FileName, file.FileName + ".backup"); File.Move(file.FileName + ".extracted", file.FileName); logBuilder.AppendLine(Logger.BuildLogWithDate("Replaced RAR!")); } else { logBuilder.AppendLine(Logger.BuildLogWithDate("ZIP does not contain any data!")); logBuilder.AppendLine(Logger.BuildLogWithDate("Critical Error: This should not be the case. Check the file format and perhaps manually convert it to ZIP!")); MessageBox.Show( "Critical Error: ZIP does not contain any data! Check the file format and perhaps manually convert it to ZIP!", "KCDModMerger", MessageBoxButton.OK); } } zippedFiles.AddRange(FindFilesInZIP(file, filename, logBuilder)); } logBuilder.AppendLine(Logger.BuildLogWithDate("Found total of " + zippedFiles.Count, true)); DataFiles = zippedFiles.ToArray(); }
private void UnRarFile(FiasFileInfo aFileInfo) { using (var unrar = new Unrar()) { unrar.Open(aFileInfo.FileName, Unrar.OpenMode.Extract); unrar.DestinationPath = Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp"; unrar.ExtractionProgress += unrar_ExtractionProgress; while (unrar.ReadHeader()) { if (stopEvent.WaitOne(0)) throw new StopException(); unrar.Extract(); } } Directory.Move(Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted.tmp", Path.GetDirectoryName(aFileInfo.FileName) + "\\Extracted"); aFileInfo.Checked = false; aFileInfo.Extracted = true; }