예제 #1
0
파일: Engine.cs 프로젝트: willman0982/code
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
    public void CheckPass()
    {
        Unrar tmpRar = UnrarObj;

        //Unrar tmpRar = new Unrar(UnrarObj.ArchivePathName);
        //tmpRar.DestinationPath = @"E:\";
        //tmpRar.Open(UnrarObj.DestinationPath, Unrar.OpenMode.Extract);

        while (!IsCompleted || tmpRar.Password != "DONE")
        {
            try
            {
                tmpRar.Password = PasswdList.GetNextPasswd();
                while (UnrarObj.ReadHeader())
                {
                    tmpRar.Extract();
                }
                System.Diagnostics.Debug.WriteLine("Pass found:" + tmpRar.Password);
                IsCompleted = true;
            }
            catch
            {
                System.Diagnostics.Debug.WriteLine("Wrong pass:" + tmpRar.Password);
                if (tmpRar != null)
                {
                    tmpRar.Close();
                }
                continue;
            }
        }
    }
예제 #4
0
        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);
        }
예제 #5
0
파일: Engine.cs 프로젝트: willman0982/code
        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);
        }
예제 #6
0
        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);
                }
            }
        }
예제 #7
0
        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();
            }
        }
        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();
                }
            }
        }
예제 #9
0
        /// <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();
        }