Ankit
Ankit

Reputation: 1887

Extract All gz files inside a folder in C#

I am working on a application and need to extract the gz files inside a folder.

what I need is a c# script that can loop all gz files in a given folder and extract them into the same folder.

I know there are some libraries for this, But I could not able to get them work for gz, I got them working for zip though.

Or if there are any other solution for the same i.e if batch script can be created that can use WinRar command line utility to achieve the same. I don't know just an Idea if possible.

Note: I think I have to drop that second option- WinRar command is able to handle only RAR files.

Thanks

Upvotes: 0

Views: 4913

Answers (4)

Deepak
Deepak

Reputation: 1

@setlocal
@echo off
set path="C:\Program Files\WinRAR\";%path%
for /F %%i in ('dir /s/b *.gz') do call :do_extract "%%i"
goto :eof

:do_extract
echo %1
mkdir %~1.extracted
pushd %~1.extracted
Winrar e %1
popd

Upvotes: -1

Ankit
Ankit

Reputation: 1887

I got it solved. Thanks MichelZ for showing the way to go. I got the 7-zip command line version to done the trick for me.

@REM ------- BEGIN demo.cmd ----------------
@setlocal
@echo off
set path="C:\Program Files\7-Zip\";%path%
for /F %%i in ('dir /s/b *.gz') do call :do_extract "%%i"
for /F %%i in ('dir /s/b *.zip') do call :do_extract "%%i"
goto :eof

:do_extract
pushd %~dp1
7z e %1 -y
popd
REM ------- END demo.cmd ------------------

Upvotes: 1

Yasser Zamani
Yasser Zamani

Reputation: 2500

I can suggest something like below:

using System;
using System.IO;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {

        try
        {
            var files = from file in Directory.EnumerateFiles(@"c:\something",
                            "*.gz", SearchOption.AllDirectories)
                        select new
                        {
                            File = file,
                        };

            foreach (var f in files)
            {
                Process.Start("c:\winrar.exe", f.File);
            }
            Console.WriteLine("{0} files found and extracted!", 
                files.Count().ToString());
        }
        catch (UnauthorizedAccessException UAEx)
        {
            Console.WriteLine(UAEx.Message);
        }
        catch (PathTooLongException PathEx)
        {
            Console.WriteLine(PathEx.Message);
        }
    }
}

NOTE: Please replace paths and winrar.exe parameters yourself with correct one.

Upvotes: 1

MichelZ
MichelZ

Reputation: 4404

Try this as a batch file with winrar's "unrar" commandline freeware:

@REM ------- BEGIN demo.cmd ----------------
@setlocal
@echo off
set path="C:\Program Files\WinRAR\";%path%
for /F %%i in ('dir /s/b *.gz') do call :do_extract "%%i"
goto :eof

:do_extract
echo %1
mkdir %~1.extracted
pushd %~1.extracted
unrar e %1
popd

REM ------- END demo.cmd ------------------

Courtesy of: http://www.respower.com/page_tutorial_unrar

Upvotes: 2

Related Questions