Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="rarArchive">rar文件地址</param>
        /// <param name="destinationPath">目标地址</param>
        /// <param name="CreateDir">是否创建目录</param>
        public static void DecompressRar(string rarArchive, string destinationPath, bool CreateDir)
        {
            if (File.Exists(rarArchive))
            {
                Unrar tmp = new Unrar(rarArchive);
                tmp.Open(Unrar.OpenMode.List);
                string[] files = tmp.ListFiles();
                tmp.Close();

                Unrar unrar = new Unrar(rarArchive);
                unrar.Open(Unrar.OpenMode.Extract);
                unrar.DestinationPath = destinationPath;

                while (unrar.ReadHeader())
                {
                    if (unrar.CurrentFile.IsDirectory)
                    {
                        unrar.Skip();
                    }
                    else
                    {
                        if (CreateDir)
                        {
                            unrar.Extract();
                        }
                        else
                        {
                            unrar.Extract(destinationPath + Path.GetFileName(unrar.CurrentFile.FileName));
                        }
                    }
                }
                unrar.Close();
            }
        }
Ejemplo n.º 2
0
        private void extractButton_Click(object sender, System.EventArgs e)
        {
            // Get hashtable of selected files
            Hashtable selectedFiles = GetSelectedFiles();

            try
            {
                // Get destination from user
                string directory = Path.GetDirectoryName(rarFileName.Text);
                if (Directory.Exists(directory))
                {
                    folderBrowser.SelectedPath = directory;
                }
                if (folderBrowser.ShowDialog() == DialogResult.OK)
                {
                    this.Cursor = Cursors.WaitCursor;

                    // 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 = folderBrowser.SelectedPath;

                    // Open archive for extraction
                    unrar.Open(rarFileName.Text, Unrar.OpenMode.Extract);

                    // Extract each file found in hashtable
                    while (unrar.ReadHeader())
                    {
                        if (selectedFiles.ContainsKey(unrar.CurrentFile.FileName))
                        {
                            this.progressBar.Value = 0;
                            unrar.Extract();
                        }
                        else
                        {
                            unrar.Skip();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor            = Cursors.Default;
                this.statusBar.Text    = "Ready";
                this.progressBar.Value = 0;
                if (this.unrar != null)
                {
                    unrar.Close();
                }
            }
        }
Ejemplo n.º 3
0
 public static byte[] GetBytes(string rarPath, string filePath)
 {
     lock (_readLock)
     {
         using (Unrar unrar = new Unrar())
         {
             try
             {
                 //Unrar file in temporary directory
                 string tempDir = System.IO.Path.Combine(Environment.CurrentDirectory, "Temp");
                 unrar.DestinationPath = tempDir;
                 unrar.Open(rarPath, Unrar.OpenMode.Extract);
                 // Get destination from user
                 while (unrar.ReadHeader())
                 {
                     if (unrar.CurrentFile.FileName.Equals(filePath))
                     {
                         unrar.Extract();
                         break;
                     }
                     unrar.Skip();
                 }
                 unrar.Close();
                 //
                 string tempPath = System.IO.Path.Combine(tempDir, filePath);
                 byte[] buffer = File.ReadAllBytes(tempPath);
                 File.Delete(tempPath); //Remove file
                 return buffer;
             }
             catch (Exception ex)
             {
                 Console.WriteLine(ex.ToString());
             }
         }
         return new byte[0];
     }
 }
Ejemplo n.º 4
0
        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);

                // Extract 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();
            }
        }