本文介绍了使用unrar.dll的RARProcessFile提取文件不起作用.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,有很多问题.我必须解压缩文件并解压缩一些文件.

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);
 }


一切正常.调用RARProcessFile中的ExtractArchive时,该函数返回成功,但不会提取任何文件.
请帮忙!!!
为了参考unrar.dll,我使用了以下链接 http://goahomepage.free.fr/article/2000_09_17_unrar_dll/UnRARDLL.html#RARProcessFile [ ^ ]
在此先感谢:-)

将内联代码转换为代码块,链接链接化"-OriginalGriff [/edit]


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]

推荐答案


<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!!!


这篇关于使用unrar.dll的RARProcessFile提取文件不起作用.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 22:44