public void Dispose() { if (this.archiveHandle != IntPtr.Zero) { Unrar64.RARCloseArchive(this.archiveHandle); this.archiveHandle = IntPtr.Zero; } }
private void Extract(string destinationPath, string destinationName) { int result = Unrar64.RARProcessFile(this.archiveHandle, (int)Unrar.Operation.Extract, destinationPath, destinationName); // Check result if (result != 0) { ProcessFileError(result); } }
/// <summary> /// Tests the ability to extract the current file without saving extracted data to disk /// </summary> /// <returns></returns> public void Test() { int result = Unrar64.RARProcessFile(this.archiveHandle, (int)Unrar.Operation.Test, string.Empty, string.Empty); // Check result if (result != 0) { ProcessFileError(result); } }
/// <summary> /// Close the currently open archive /// </summary> /// <returns></returns> public void Close() { // Exit without exception if no archive is open if (this.archiveHandle == IntPtr.Zero) { return; } // Close archive int result = Unrar64.RARCloseArchive(this.archiveHandle); // Check result if (result != 0) { ProcessFileError(result); } else { this.archiveHandle = IntPtr.Zero; } }
/// <summary> /// Reads the next archive header and populates CurrentFile property data /// </summary> /// <returns></returns> public bool ReadHeader() { // Throw exception if archive not open if (this.archiveHandle == IntPtr.Zero) { throw new IOException("Archive is not open."); } // Initialize header struct this.header = new Unrar.RARHeaderDataEx(); header.Initialize(); // Read next entry currentFile = null; int result = Unrar64.RARReadHeaderEx(this.archiveHandle, ref this.header); // Check for error or end of archive if ((Unrar.RarError)result == Unrar.RarError.EndOfArchive) { return(false); } else if ((Unrar.RarError)result == Unrar.RarError.BadData) { throw new IOException("Archive data is corrupt."); } // Determine if new file if (((header.Flags & 0x01) != 0) && currentFile != null) { currentFile.ContinuedFromPrevious = true; } else { // New file, prepare header currentFile = new RARFileInfo(); currentFile.FileName = header.FileNameW.ToString(); if ((header.Flags & 0x02) != 0) { currentFile.ContinuedOnNext = true; } if (header.PackSizeHigh != 0) { currentFile.PackedSize = (header.PackSizeHigh * 0x100000000) + header.PackSize; } else { currentFile.PackedSize = header.PackSize; } if (header.UnpSizeHigh != 0) { currentFile.UnpackedSize = (header.UnpSizeHigh * 0x100000000) + header.UnpSize; } else { currentFile.UnpackedSize = header.UnpSize; } currentFile.HostOS = (int)header.HostOS; currentFile.FileCRC = header.FileCRC; currentFile.FileTime = FromMSDOSTime(header.FileTime); currentFile.VersionToUnpack = (int)header.UnpVer; currentFile.Method = (int)header.Method; currentFile.FileAttributes = (int)header.FileAttr; currentFile.BytesExtracted = 0; if ((header.Flags & 0xE0) == 0xE0) { currentFile.IsDirectory = true; } this.OnNewFile(); } // Return success return(true); }
/// <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); }