0

I was trying to make a program to clean out some directories on my NAS and I noticed that a lot of folders contained nested rar and zip files and I have plenty of space to unpack them. The program should ask the user for a directory to be cleaned then unpack all rars then delete all of the rars. I'm trying to use UnRAR DLL and I cant even get the rars to unpack. I realize I'm having an issue where visual studio 2022 is refusing to recognize the Unrar DLL in the "using" command. Because of that I've been unable to unpack a single file. This is one my first useful programs so if im missing something basic I understand.

This is my initial attempt:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using UnRAR;



namespace Cleaning
{

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Directory To Be Cleaned");

            string rar_path = Console.ReadLine();

            string[] Rars = Directory.GetFiles(rar_path, "*.rar", SearchOption.AllDirectories);



            foreach (string rar in Rars)
            {

                string source = rar;
                string dest = "C:\\Users\\Kaleb\\OneDrive\\Desktop\Test Area";
                UnRAR unrar = new UnRAR();
                unrar.Password = "password_of_myarchive";
                unrar.Open(@source, UnRAR.OpenMode.Extract);
                while (unrar.ReadHeader())
                {
                    unrar.ExtractToDirectory(@dest);
                }
                unrar.Close();
            }
            

            
        }

       

        
    }
}

For reference I have added the UnRAR DLL to the project folder.

4
  • Which unrar assembly are you using? The ones I'm aware of are native DLLs and can't be referenced directly in a c# project without creating a wrapper. Projects that can be referenced directly I've used that should work include: nunrar, SevenZipSharp. These can be added as a NuGet package reference to your project. Commented Apr 5, 2022 at 21:23
  • stackoverflow.com/questions/5564396/… Commented Apr 5, 2022 at 22:38
  • Thanks Hans and Jesse I realized that first unrar.dll that comes from WinRAR is not really supported. Also when I tried using SevenZip and others before the problem I was running into was in .NET 6.0 and not 5.0 which is as far as is supported. Hopefully changing that up will work. If this doesn't work I'll try a wrapper again ive had mixed results though. Thank for your help.
    – Apolloin
    Commented Apr 6, 2022 at 1:21
  • If you find the solution to solve the problem from the comment above, you can consider to answer it by yourself and change its status to Answered. See can I answer my own question.., Just a reminder :) Commented Apr 6, 2022 at 1:44

1 Answer 1

0

SO I was able to get it working with the source code from the great people over at SharpCompress and utilizing their source I've got the following stable build.

using SharpCompress.Archives;
using SharpCompress.Archives.Rar;
using SharpCompress.Common;
using System;
using System.IO;
using System.Linq;
using System.Globalization;



namespace ConsoleApp3
{

    public class Program
    {
        static void Main(string[] args)
        {
            for (; ; )
            {
                Console.WriteLine("Enter E to extract all directories in file path");
                Console.WriteLine("Enter D to delete all Archives in file path");
                Console.WriteLine("REMEMBER TO ALWAYS EXTRACT BEFORE DELETING");
                string option = Console.ReadLine();


                if (option == "e" || option == "E")
                {

                   
                        Console.WriteLine("Enter Directory To Be Cleaned");
                    //as a warning this will extract all files from any rar in the slected driectory one at a time in order.
                    //if a rar is broken it will halt the program until the offendin rar is deleted best way to find is to see what has been extracted so far and go from there
                    //or one could also limit the directory in order to refine the number of rars to look for

                        string rar_path = Console.ReadLine();

                        string[] Rars = Directory.GetFiles(rar_path, "*.rar", SearchOption.AllDirectories);




                        foreach (string rar in Rars)
                        {
                            var DirectoryFinal = Path.GetDirectoryName(rar);
                            using (var archive = RarArchive.Open(@rar))
                            {
                                foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory))
                                {
                                    entry.WriteToDirectory(@DirectoryFinal, new ExtractionOptions()
                                    {
                                        ExtractFullPath = true,
                                        Overwrite = true
                                    });
                                }

                            };
                        }
                    
                   
                }


                else if (option == "d" || option == "D")
                {
                    Console.WriteLine("Enter Directory To Be Cleaned");
                    //be careful with this i would recomend extracting and then chekcing everything first

                    string rar_path = Console.ReadLine();
                    string[] TobeDeleted = Directory.GetFiles(rar_path, "*.r*", SearchOption.AllDirectories);
                    foreach (string rarstobedeleted in TobeDeleted)
                    {
                        File.Delete(rarstobedeleted);
                    }
                }
                else
                {
                    Console.WriteLine("Thats not an option try again");
                   
                }
                Console.WriteLine("Cleaning Complete.");

                ;
            }
        }
    



    }
}

This work effectively for rar files only for the time being but will effectively clean up any directories where someone may have downloaded a large amount of files stored in separated rars

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.