Beispiel #1
0
        public static RARFileInfo[] GetFiles(string rarPath)
        {
            lock (_readLock)
            {
                List<RARFileInfo> rarFileInfoList = new List<RARFileInfo>();
                using (Unrar unrar = new Unrar())
                {
                    try
                    {
                        unrar.Open(rarPath, Unrar.OpenMode.List);

                        while (unrar.ReadHeader())
                        {
                            if (!unrar.CurrentFile.IsDirectory)
                                rarFileInfoList.Add(unrar.CurrentFile);
                            unrar.Skip();
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                    return rarFileInfoList.ToArray();
                }
            }
        }
Beispiel #2
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();
            }
        }
Beispiel #3
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();
                }
            }
        }
Beispiel #4
0
        private void OpenRarFile(string fileName)
        {
            try
            {
                this.Cursor           = Cursors.WaitCursor;
                testButton.Enabled    = false;
                extractButton.Enabled = false;
                fileList.BeginUpdate();
                fileList.Items.Clear();

                // Create new unrar class and open archive for listing files
                unrar = new Unrar();
                unrar.Open(fileName, Unrar.OpenMode.List);

                // Read each header, skipping directory entries
                while (unrar.ReadHeader())
                {
                    if (!unrar.CurrentFile.IsDirectory)
                    {
                        ListViewItem item = new ListViewItem(unrar.CurrentFile.FileName);
                        item.SubItems.Add(unrar.CurrentFile.UnpackedSize.ToString());
                        item.SubItems.Add(unrar.CurrentFile.PackedSize.ToString());
                        item.SubItems.Add(unrar.CurrentFile.FileTime.ToString());
                        item.Checked = true;
                        fileList.Items.Add(item);
                    }
                    unrar.Skip();
                }

                // Cleanup and enable buttons if no exception was thrown
                unrar.Close();
                this.unrar            = null;
                testButton.Enabled    = true;
                extractButton.Enabled = true;
            }
            catch (Exception ex)
            {
                testButton.Enabled    = false;
                extractButton.Enabled = false;
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.Cursor = Cursors.Default;
                fileList.EndUpdate();
                if (this.unrar != null)
                {
                    unrar.Close();
                }
            }
        }
Beispiel #5
0
        private void testButton_Click(object sender, System.EventArgs e)
        {
            // Get hashtable of selected files
            Hashtable selectedFiles = GetSelectedFiles();

            try
            {
                this.Cursor = Cursors.WaitCursor;

                // Create new unrar class and attach event handlers for
                // progress, missing volumes, and password
                unrar = new Unrar();
                AttachHandlers(unrar);

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

                // Test each file found in hashtable
                while (unrar.ReadHeader())
                {
                    if (selectedFiles.ContainsKey(unrar.CurrentFile.FileName))
                    {
                        this.progressBar.Value = 0;
                        unrar.Test();
                    }
                    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();
                }
            }
        }
Beispiel #6
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];
     }
 }
        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();
            }
        }