0

I'm new to c# as well as this website so I'll probably make some mistakes but hopefully I'll learn as well.

I'm trying to develop an app to extract from a .7z a file, read it and delete it before moving to the next (there is about 12k files in there, which take up a lot of space).

Now, I'm using .NET 2.0 and I haven't found easy solutions to extract single files from an archive. I came across a post explaining that you could use 7za.exe to do so from the command line, and so I did. Now, the problem is that if I try to do the same in my app 7za throws the "cannot use absolute pathnames" error. However, as I said, the same parameters work at the command prompt.

My idea is to get a list of all the files in the compressed folder and put it in a textfile. From there it is as easy as get the name of the first one, unzip it, read it, delete it and move to next. This is what I got to get the list:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"C:\Users\yadayada\Desktop\7za.exe"; //just testing
string parameters= "-y l " + path_file7z + " > " + path_file_destination + @"\file.txt"; 
//needeless to say, path_file7z and path_file_destination are strings with the correct path like "C:\Users\yadayada\Desktop"
startInfo.Arguments = parameters
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();

As I said, as the process ends I can see a console opening and closing. I took a screenshot to check what it said and the error I got was the one I said at the begining of the question.

Now, does anyone know why I get this error and how can I fix this?

SOLUTION

Ended un going for a not too elegant (being gentle) solution that works.

To list the names:

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = @"cmd.exe";
string parameters= "/k -y l " + path_file7z + " > " + path_file_destination + @"\file.txt"; 
//needeless to say, path_file7z and path_file_destination are strings with the correct path like "C:\Users\yadayada\..."
startInfo.Arguments = parameters
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.Start();
Thread.Sleep(1000);
process.Kill();

Now, once this has been done the extract part is tanken care of by a method similar to the one provided by Igor's links:

public void ExtractFile(string source, string element, string destination)
{
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = /*7zG.exe's path*/;
startInfo.Arguments = @"-y x " + source + " -o" + destination + " " + element + " -r";
process.StartInfo = startInfo;
process.StartInfo.UseShellExecute = false;
process.Start();
process.WaitForExit();            
}

I think it is working well now. If I have further issues I'll update this with the solutions I come up with with the help provided. Thanks again!

int i
  • 1
  • 2
  • Have you looked into this [SDK](http://www.7-zip.org/sdk.html)? – t0mm13b Nov 09 '15 at 22:33
  • 1
    Only 7zip is allowed? Has many easy ways using rar o zip. You can use another compression method? – Igor Quirino Nov 09 '15 at 22:38
  • 1
    Any particular reason you're tied to .NET 2.0? You're missing out on a lot of goodies that came with the newer versions (LINQ, async/await, etc). – Tim Nov 09 '15 at 22:38
  • 1
    Its like use Internet Explorer 6.0. Upgrade your framework. Tim is really right! – Igor Quirino Nov 09 '15 at 22:44
  • @IgorQuirino yes, this application will be used by someone who recieves the files in ".7z" and he doesn't want to convert those. I'm using .NET 2.0 because I have to make sure the computers at the office have it, and I am not in charge of changing that. – int i Nov 10 '15 at 11:03
  • Upgrading Framework version can be a problem to you? I think this is a good thing to do. – Igor Quirino Nov 10 '15 at 11:07
  • You dont need to fill startinfo two times. You are using a Max execution time of one second? I think you will need more time. Use instead WaitForExit and remove thread.Sleep – Igor Quirino Nov 10 '15 at 11:28

1 Answers1

0

Use this tutorial for 7zip: https://stick2basic.wordpress.com/2013/04/25/how-to-extract-7z-file-to-a-folder-c-net/

Use this library if rar and zip are allowed: http://dotnetzip.codeplex.com/

private void MyExtract()
  {
      string zipToUnpack = "C1P3SML.zip";
      string unpackDirectory = "Extracted Files";
      using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
      {
          // here, we extract every entry, but we could extract conditionally
          // based on entry name, size, date, checkbox status, etc.  
          foreach (ZipEntry e in zip1)
          {
            e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
          }
       }
    }

EDIT

This can help you: Unzip a file in c# using 7z.exe

Happy to help you!

Community
  • 1
  • 1
Igor Quirino
  • 1,187
  • 13
  • 28