public void Dispose() { if (this.archiveHandle != IntPtr.Zero) { Unrar.RARCloseArchive(this.archiveHandle); this.archiveHandle = IntPtr.Zero; } }
/// <summary> /// Moves the current archive position to the next available header /// </summary> /// <returns></returns> public void Skip() { int result = Unrar.RARProcessFile(this.archiveHandle, (int)Operation.Skip, string.Empty, string.Empty); // Check result if (result != 0) { ProcessFileError(result); } }
/// <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; // 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; }
/// <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 = Unrar.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 RARHeaderDataEx(); header.Initialize(); // Read next entry int result = Unrar.RARReadHeaderEx(this.archiveHandle, ref this.header); // Check for error or end of archive if ((RarError)result == RarError.EndOfArchive) { return(false); } else if ((RarError)result == RarError.BadData) { throw new IOException("Archive data is corrupt."); } // New file, prepare header currentFile = new RARFileInfo(); currentFile.FileName = header.FileNameW.ToString(); if ((header.Flags & 0xE0) == 0xE0) { currentFile.IsDirectory = true; } // Return success return(true); }
public override StringDictionary GetProperties(string source) { if (File.Exists(source)) { string outputFile = GlobalData.OutputFile;//contains the list of files 1 for each line switch (Path.GetExtension(source).ToLower()) { case ".rar": try { Unrar unrar = new Unrar(source); unrar.Open(Unrar.OpenMode.List); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing unrar.WriteFileList(ref sw); sw.Close(); unrar.Close(); fileProperties["content"] = outputFile; } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid rar archive"); #endif } break; case ".zip": try { FileStream fs = File.OpenRead(source); //Open the archieve file stream ZipFile zipFile = new ZipFile(fs); //Create a zip Object from the opened stream StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing foreach (ZipEntry zipEntry in zipFile) if (zipEntry.IsFile) sw.WriteLine(zipEntry.Name); sw.Close(); zipFile.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid zip archive"); #endif } break; case ".tar": try { FileStream fs = File.OpenRead(source); TarArchive ta = TarArchive.CreateInputTarArchive(fs); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message) { if (!entry.IsDirectory) sw.WriteLine(entry.Name); }; ta.ListContents(); //List the contents of tar archieve sw.Close(); ta.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid tar archive"); #endif } break; case ".gz": try { FileStream fs = File.OpenRead(source); TarArchive ta = TarArchive.CreateInputTarArchive(new GZipInputStream(fs)); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message) { if (!entry.IsDirectory) sw.WriteLine(entry.Name); }; ta.ListContents(); //List the contents of gz2 archieve sw.Close(); ta.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid gz archive"); #endif } break; case ".bz2": try { FileStream fs = File.OpenRead(source); TarArchive ta = TarArchive.CreateInputTarArchive(new BZip2InputStream(fs)); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message) { if (!entry.IsDirectory) sw.WriteLine(entry.Name); }; ta.ListContents(); //List the contents of bz2 archieve sw.Close(); ta.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid bz2 archive"); #endif } break; } return base.GetProperties(source); } else if (Win32Helper.PathExist(source)) { RemoveFileSpecificKeys(); return base.GetProperties(source); } return null; }
public override StringDictionary GetProperties(string source) { if (File.Exists(source)) { string outputFile = GlobalData.OutputFile;//contains the list of files 1 for each line switch (Path.GetExtension(source).ToLower()) { case ".rar": try { Unrar unrar = new Unrar(source); unrar.Open(Unrar.OpenMode.List); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing unrar.WriteFileList(ref sw); sw.Close(); unrar.Close(); fileProperties["content"] = outputFile; } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid rar archive"); #endif } break; case ".zip": try { FileStream fs = File.OpenRead(source); //Open the archieve file stream ZipFile zipFile = new ZipFile(fs); //Create a zip Object from the opened stream StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing foreach (ZipEntry zipEntry in zipFile) { if (zipEntry.IsFile) { sw.WriteLine(zipEntry.Name); } } sw.Close(); zipFile.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid zip archive"); #endif } break; case ".tar": try { FileStream fs = File.OpenRead(source); TarArchive ta = TarArchive.CreateInputTarArchive(fs); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message) { if (!entry.IsDirectory) { sw.WriteLine(entry.Name); } }; ta.ListContents(); //List the contents of tar archieve sw.Close(); ta.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid tar archive"); #endif } break; case ".gz": try { FileStream fs = File.OpenRead(source); TarArchive ta = TarArchive.CreateInputTarArchive(new GZipInputStream(fs)); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message) { if (!entry.IsDirectory) { sw.WriteLine(entry.Name); } }; ta.ListContents(); //List the contents of gz2 archieve sw.Close(); ta.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid gz archive"); #endif } break; case ".bz2": try { FileStream fs = File.OpenRead(source); TarArchive ta = TarArchive.CreateInputTarArchive(new BZip2InputStream(fs)); StreamWriter sw = new StreamWriter(outputFile); //The outputFile will be Created and opened for writing ta.ProgressMessageEvent += delegate(TarArchive archive, TarEntry entry, string message) { if (!entry.IsDirectory) { sw.WriteLine(entry.Name); } }; ta.ListContents(); //List the contents of bz2 archieve sw.Close(); ta.Close(); fileProperties["content"] = outputFile; fs.Close(); } catch { RemoveFileSpecificKeys(); #if Log Console.WriteLine(source + " => not a valid bz2 archive"); #endif } break; } return(base.GetProperties(source)); } else if (Win32Helper.PathExist(source)) { RemoveFileSpecificKeys(); return(base.GetProperties(source)); } return(null); }