Example #1
0
 /// <summary>
 /// Opens archive specified by the ArchivePathName property with a specified mode
 /// </summary>
 /// <param name="openMode">Mode in which archive should be opened</param>
 public void Open(Unrar.OpenMode openMode)
 {
     if (this.ArchivePathName.Length == 0)
     {
         throw new IOException("Archive name has not been set.");
     }
     this.Open(this.ArchivePathName, openMode);
 }
Example #2
0
        /// <summary>
        /// Opens specified archive using the specified mode.
        /// </summary>
        /// <param name="archivePathName">Path of archive to open</param>
        /// <param name="openMode">Mode in which to open archive</param>
        public void Open(string archivePathName, Unrar.OpenMode openMode)
        {
            IntPtr handle = IntPtr.Zero;

            // Close any previously open archives
            if (this.archiveHandle != IntPtr.Zero)
            {
                this.Close();
            }

            // Prepare extended open archive struct
            this.ArchivePathName = archivePathName;
            Unrar.RAROpenArchiveDataEx openStruct = new Unrar.RAROpenArchiveDataEx();
            openStruct.Initialize();
            openStruct.ArcName  = this.archivePathName + "\0";
            openStruct.ArcNameW = this.archivePathName + "\0";
            openStruct.OpenMode = (uint)openMode;
            if (this.retrieveComment)
            {
                openStruct.CmtBuf     = new string((char)0, 65536);
                openStruct.CmtBufSize = 65536;
            }
            else
            {
                openStruct.CmtBuf     = null;
                openStruct.CmtBufSize = 0;
            }

            // Open archive
            handle = Unrar64.RAROpenArchiveEx(ref openStruct);

            // Check for success
            if (openStruct.OpenResult != 0)
            {
                switch ((Unrar.RarError)openStruct.OpenResult)
                {
                case Unrar.RarError.InsufficientMemory:
                    throw new OutOfMemoryException("Insufficient memory to perform operation.");

                case Unrar.RarError.BadData:
                    throw new IOException("Archive header broken");

                case Unrar.RarError.BadArchive:
                    throw new IOException("File is not a valid archive.");

                case Unrar.RarError.OpenError:
                    throw new IOException("File could not be opened.");
                }
            }

            // Save handle and flags
            this.archiveHandle = handle;
            this.archiveFlags  = (Unrar.ArchiveFlags)openStruct.Flags;

            // Set callback
            Unrar64.RARSetCallback(this.archiveHandle, this.callback, this.GetHashCode());

            // If comment retrieved, save it
            if (openStruct.CmtState == 1)
            {
                this.comment = openStruct.CmtBuf.ToString();
            }

            // If password supplied, set it
            if (this.password.Length != 0)
            {
                Unrar64.RARSetPassword(this.archiveHandle, this.password);
            }

            // Fire NewVolume event for first volume
            this.OnNewVolume(this.archivePathName);
        }