Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Everybody, Having a big of a problem. I have to unrar a file and extract some files.
 class Unrar
{
 public const int ERAR_END_ARCHIVE = 10;
 public const int ERAR_NO_MEMORY = 11;
 public const int ERAR_BAD_DATA = 12;
 public const int ERAR_BAD_ARCHIVE = 13;
 public const int ERAR_UNKNOWN_FORMAT = 14;
 public const int ERAR_EOPEN = 15;
 public const int ERAR_ECREATE = 16;
 public const int ERAR_ECLOSE = 17;
 public const int ERAR_EREAD = 18;
 public const int ERAR_EWRITE = 19;
 public const int ERAR_SMALL_BUF = 20;
 public const int RAR_OM_LIST = 0;
 public const int RAR_OM_EXTRACT = 1;
 public const int RAR_SKIP = 0;
 public const int RAR_TEST = 1;
 public const int RAR_EXTRACT = 2;
 public const int RAR_VOL_ASK = 0;
 public const int RAR_VOL_NOTIFY = 1;
 public enum RarOperations
 {
  OP_EXTRACT = 0,
  OP_TEST = 1,
  OP_LIST = 2
 }
 public struct RARHeaderData
 {
  public string ArcName;
  public string FileName;
  public uint Flags;
  public uint PackSize;
  public uint UnpSize;
  public uint HostOS;
  public uint FileCRC;
  public uint FileTime;
  public uint UnpVer;
  public uint Method;
  public uint FileAttr;
  public string CmtBuf;
  public uint CmtBufSize;
  public uint CmtSize;
  public uint CmtState;
 }
 public struct RAROpenArchiveData
 {
  public string ArcName;
  public uint OpenMode;
  public uint OpenResult;
  public string CmtBuf;
  public uint CmtBufSize;
  public uint CmtSize;
  public uint CmtState;
 }
 public struct RAROpenArchiveDataEx
 {
  public string ArcName;
  public string ArcNameW;
  public uint OpenMode;
  public uint OpenResult;
  public string CmtBuf;
  public uint CmtBufSize;
  public uint CmtSize;
  public uint CmtState;
  public uint Flags;
  public uint Reserved;
 }
 public struct RARHeaderDataEx
 {
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  public string ArcName;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  public string FileName;
  public string FileNameW;
  public uint Flags;
  public uint PackSize;
  public uint PackSizeHigh;
  public uint UnpSize;
  public uint UnpSizeHigh;
  public uint HostOS;
  public uint FileCRC;
  public uint FileTime;
  public uint UnpVer;
  public uint Method;
  public uint FileAttr;
  public string CmtBuf;
  public uint CmtBufSize;
  public uint CmtSize;
  public uint CmtState;
  public uint Reserved;
 };

 [DllImportAttribute("unrar.dll")]
 public static extern IntPtr RAROpenArchive(ref RAROpenArchiveData ArchiveData);
 [DllImportAttribute("unrar.dll")]
 public static extern int RARCloseArchive(IntPtr hArcData);
 [DllImportAttribute("unrar.dll")]
 public static extern int RARReadHeader(IntPtr hArcData, ref RARHeaderData HeaderData);
 [DllImportAttribute("unrar.dll")]
 public static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx ArchiveData);
 [DllImportAttribute("unrar.dll")]
 public static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx HeaderData);
 [DllImportAttribute("unrar.dll")]
 public static extern int RARProcessFile(IntPtr hArcData, int Operation, string DestPath, string DestName);
 [DllImportAttribute("unrar.dll")]
 public static extern int RARGetDllVersion();

 public void ExtractArchive(string strFileName)
 {
  IntPtr lHandle;
  int iStatus;
  RARHeaderData uHeader = new RARHeaderData();
  RAROpenArchiveData uRAR = new RAROpenArchiveData();
  uRAR.ArcName = strFileName + "\0";
  uRAR.OpenMode = RAR_OM_EXTRACT;
  uRAR.CmtBuf = string.Empty.PadLeft(16384, ' ');
  uRAR.CmtBufSize = 16384;
  lHandle = RAROpenArchive(ref uRAR);
  if(uRAR.OpenResult != 0)
   MessageBox.Show("Unable to open the Archieve!!!");
  int result = RARProcessFile(lHandle, RAR_EXTRACT, null, null);
  if (0 != result)
   MessageBox.Show("Unable to open the Archieve!!!");

  iStatus = RARCloseArchive(lHandle);
 }

Every thing works fine. When ExtractArchive in RARProcessFile is called, the function returns a success but it does not extract any file.
Please help!!!
For reference on unrar.dll, I used the following link http://goahomepage.free.fr/article/2000_09_17_unrar_dll/UnRARDLL.html#RARProcessFile[^]
Thanks in Advance :-)

[edit]Inline code converted to code block, Link "Linkified" - OriginalGriff[/edit]
Posted
Updated 6-Jun-11 2:43am
v2

Just a guess, given that unrar.Dll is quite old: should those parameters be int or short? If they should be short, it may be that your RAR_EXTRACT operation code is being read as a zero, and that would be RAR_SKIP...
 
Share this answer
 
Comments
varunpandeyengg 7-Jun-11 2:36am    
Thanks for the reply. I directly passed 2 instead of RAR_EXTRACT. Still it is showing returning success and not extracting any thing...
OriginalGriff 7-Jun-11 3:30am    
Passing 2 or a constant that equates to 2 won't make any difference: try changing the parameter type from int (32 bits) to short (16 bits) - older systems used 16 bit integers, so that may be what the DLL is expecting...
I got the Answer... I have to read the header (which is associated with every file). After reading the header... I can extract it.
The revised code is
<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.Runtime.InteropServices;<br />
using System.IO;<br />
namespace UnrarSample<br />
{<br />
  public partial class UnrarSample : Form<br />
  {<br />
    public UnrarSample()<br />
    {<br />
      InitializeComponent();<br />
    }<br />
    private void btn_Browse_Click(object sender, EventArgs e)<br />
    {<br />
      OpenFileDialog objFileDialog = new OpenFileDialog();<br />
      objFileDialog.Filter = objFileDialog.Filter = "Rar files (*.rar)|*.rar";<br />
      if (DialogResult.OK == objFileDialog.ShowDialog())<br />
      {<br />
        txtFileName.Text = objFileDialog.FileName;<br />
        <br />
      }<br />
    }<br />
    private void btn_Unrar_Click(object sender, EventArgs e)<br />
    {<br />
      Unrar unrar = new Unrar();<br />
      unrar.OpenArchieve(txtFileName.Text);<br />
    }<br />
  }<br />
  class Unrar<br />
  {<br />
    public const int ERAR_END_ARCHIVE = 10;<br />
    public const int ERAR_NO_MEMORY = 11;<br />
    public const int ERAR_BAD_DATA = 12;<br />
    public const int ERAR_BAD_ARCHIVE = 13;<br />
    public const int ERAR_UNKNOWN_FORMAT = 14;<br />
    public const int ERAR_EOPEN = 15;<br />
    public const int ERAR_ECREATE = 16;<br />
    public const int ERAR_ECLOSE = 17;<br />
    public const int ERAR_EREAD = 18;<br />
    public const int ERAR_EWRITE = 19;<br />
    public const int ERAR_SMALL_BUF = 20;<br />
    public const int RAR_OM_LIST = 0;<br />
    public const int RAR_OM_EXTRACT = 1;<br />
    public const int RAR_SKIP = 0;<br />
    public const int RAR_TEST = 1;<br />
    public const int RAR_EXTRACT = 2;<br />
    public const int RAR_VOL_ASK = 0;<br />
    public const int RAR_VOL_NOTIFY = 1;<br />
    public enum RarOperations<br />
    {<br />
      OP_EXTRACT = 0,<br />
      OP_TEST = 1,<br />
      OP_LIST = 2<br />
    }<br />
    [StructLayout(LayoutKind.Sequential)]<br />
    public struct RARHeaderData<br />
    {<br />
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]<br />
      public string ArcName;<br />
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]<br />
      public string FileName;<br />
      public uint Flags;<br />
      public uint PackSize;<br />
      public uint UnpSize;<br />
      public uint HostOS;<br />
      public uint FileCRC;<br />
      public uint FileTime;<br />
      public uint UnpVer;<br />
      public uint Method;<br />
      public uint FileAttr;<br />
      public string CmtBuf;<br />
      public uint CmtBufSize;<br />
      public uint CmtSize;<br />
      public uint CmtState;<br />
    }<br />
    [StructLayout(LayoutKind.Sequential)]<br />
    public struct RAROpenArchiveData<br />
    {<br />
      public string ArcName;<br />
      public uint OpenMode;<br />
      public uint OpenResult;<br />
      public string CmtBuf;<br />
      public uint CmtBufSize;<br />
      public uint CmtSize;<br />
      public uint CmtState;<br />
    }<br />
    [StructLayout(LayoutKind.Sequential)]<br />
    public struct RAROpenArchiveDataEx<br />
    {<br />
      public string ArcName;<br />
      public string ArcNameW;<br />
      public uint OpenMode;<br />
      public uint OpenResult;<br />
      public string CmtBuf;<br />
      public uint CmtBufSize;<br />
      public uint CmtSize;<br />
      public uint CmtState;<br />
      public uint Flags;<br />
      public uint Reserved;<br />
    }<br />
    [StructLayout(LayoutKind.Sequential)]<br />
    public struct RARHeaderDataEx<br />
    {<br />
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]<br />
      public string ArcName;<br />
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]<br />
      public string FileName;<br />
      public string FileNameW;<br />
      public uint Flags;<br />
      public uint PackSize;<br />
      public uint PackSizeHigh;<br />
      public uint UnpSize;<br />
      public uint UnpSizeHigh;<br />
      public uint HostOS;<br />
      public uint FileCRC;<br />
      public uint FileTime;<br />
      public uint UnpVer;<br />
      public uint Method;<br />
      public uint FileAttr;<br />
      public string CmtBuf;<br />
      public uint CmtBufSize;<br />
      public uint CmtSize;<br />
      public uint CmtState;<br />
      public uint Reserved;<br />
    };<br />
    [DllImportAttribute("unrar.dll")]<br />
    public static extern IntPtr RAROpenArchive(ref RAROpenArchiveData ArchiveData);<br />
    [DllImportAttribute("unrar.dll")]<br />
    public static extern int RARCloseArchive(IntPtr hArcData);<br />
    [DllImportAttribute("unrar.dll")]<br />
    public static extern int RARReadHeader(IntPtr hArcData, ref RARHeaderData HeaderData);<br />
    [DllImportAttribute("unrar.dll")]<br />
    public static extern IntPtr RAROpenArchiveEx(ref RAROpenArchiveDataEx ArchiveData);<br />
    [DllImportAttribute("unrar.dll")]<br />
    public static extern int RARReadHeaderEx(IntPtr hArcData, ref RARHeaderDataEx HeaderData);<br />
    [DllImportAttribute("unrar.dll")]<br />
    public static extern int RARProcessFile(IntPtr hArcData, int Operation, string DestPath, string DestName);<br />
    [DllImportAttribute("unrar.dll")]<br />
    public static extern int RARGetDllVersion();<br />
    [DllImport("user32.dll")]<br />
    static extern bool CharToOem(string lpszSrc, [Out] StringBuilder lpszDst);<br />
<br />
    public void OpenArchieve(string strFileName)<br />
    {<br />
      IntPtr lHandle;<br />
      int iStatus;<br />
      RAROpenArchiveData uRAR = new RAROpenArchiveData();<br />
      RARHeaderData uHeader = new RARHeaderData();<br />
      uRAR.ArcName = strFileName + char.MinValue;<br />
      uRAR.OpenMode = RAR_OM_EXTRACT;<br />
      uRAR.CmtBuf = null;<br />
  <br />
      string ExtractDir = Path.GetDirectoryName(strFileName);<br />
      StringBuilder sbDir = new StringBuilder();<br />
      CharToOem("C:\\Documents and Settings\\Administrator\\Desktop\\New Folder", sbDir); <br />
      lHandle = RAROpenArchive(ref uRAR);<br />
      if(uRAR.OpenResult != 0)<br />
        MessageBox.Show("Unable to open the Archieve!!!");<br />
      while (RARReadHeader(lHandle, ref uHeader) == 0)<br />
      {<br />
        int result = RARProcessFile(lHandle, 2, sbDir.ToString(), null);<br />
        if (0 != result)<br />
          MessageBox.Show("Unable to open the Archieve!!!");<br />
      }<br />
      <br />
      <br />
      iStatus = RARCloseArchive(lHandle);<br />
    }<br />
  }<br />
}<br />

This might not be optimized... But At least it is working... :-)
Thanks Viewers
CHEERS!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900