/// <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, 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; RAROpenArchiveDataEx openStruct = new 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 = Unrar.RAROpenArchiveEx(ref openStruct); // Check for success if (openStruct.OpenResult != 0) { switch ((RarError)openStruct.OpenResult) { case RarError.InsufficientMemory: throw new OutOfMemoryException("Insufficient memory to perform operation."); case RarError.BadData: throw new IOException("Archive header broken"); case RarError.BadArchive: throw new IOException("File is not a valid archive."); case RarError.OpenError: throw new IOException("File could not be opened."); } } // Save handle and flags this.archiveHandle = handle; this.archiveFlags = (ArchiveFlags)openStruct.Flags; // Set callback Unrar.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) { Unrar.RARSetPassword(this.archiveHandle, this.password); } // Fire NewVolume event for first volume this.OnNewVolume(this.archivePathName); }